Calculator on Arduino and size for numbers

Hello, I'm making the calculator using Arduino, own keyboard and HD44780 LCD. Firstly, I'm typing the first number and I store it in string: for example, "121":

   // pressing "1" key on keyboard:
   lcd.write("1");  
   delay(200); 
   buforS += "1";
   // pressing "2" key on keyboard:
   lcd.write("2");  
   delay(200); 
   buforS += "2";
   // pressing "1" key on keyboard:
   lcd.write("1");  
   delay(200); 
   buforS += "1";

Next, I'm pressing + button and this string is converting to a double and the code clears the string:

      buforF = buforF + buforS.toFloat(); // buforF is a double
      buforS = "";
      dzialFlag = 1;
      lcd.setCursor(0, 1); 
      lcd.write("+");     
      delay(300);

i have the first number (a + b = c), so I'm typing now the second one: (code is like "121" above) and I'm converting the string to double and adding it to double buforF (the first number):

      buforF = buforF + buforS.toFloat();
      buforS = "";      
      dzialFlag = 0;
      lcd.clear();
      lcd.print(buforF);
      delay(300);

It works, but...

It works only for small numbers and the numbers have only 2 digits after comma (for example, 100,511 + 100,001 will show 100,51, not 100,512). How to fix that?

1 Like
      buforF = buforF + buforS.toFloat(); // buforF is a double

buforF is a double, and buforS is a String. You REALLY need better names. REALLY.

How to fix that?

Have you looked at the documentation for the Print class? The part that shows that, by default, floats are printed to 2 decimal places? The part that shows that an optional argument allows you to control how many digits are printed?

OK, thanks - now I have more precision but I have problem with wrong answer: 2 + 0,001 = 2,000999

I've change my code from lcd.print(buforF); to lcd.print(buforF,7);

How to fix this problem?

but I have problem with wrong answer:

Given the level of accuracy of the float variables on the Arduino, that answer looks right to me.

How to fix this problem?

Quit using floats.

How?

How?

How what?

How to quit using floats? How to operate on big numbers without using doubles or floats?

What are you trying to accomplish? If the floats being added have three decimal places, why are you printing the result to 7 places?

Look at the documentation for float.

Floats have only 6-7 decimal digits of precision. That means the total number of digits, not the number to the right of the decimal point.

Printing a value that has 6 digits of precision using 8 characters is silly. Doing so does not make the result more accurate.

You also need to understand that data is stored in binary form, and that 0.001 does not translate to binary as a nice round number. When you convert 0.001 to binary, and add 0.001 to it, the result is not exactly 0.002. Although, if you print the result to 3 decimal places it will print as 0.002.

So, we need to understand what you are really trying to do before we can recommend an approach.