Pages: [1]   Go Down
Author Topic: Calculator on Arduino and size for numbers  (Read 754 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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":

Code:
 
   // 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:
Code:
     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):
Code:
     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?
« Last Edit: January 13, 2013, 08:12:42 am by bialasik » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
      buforF = buforF + buforS.toFloat(); // buforF is a double
buforF is a double, and buforS is a String. You REALLY need better names. REALLY.

Quote
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?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.

Quote
How to fix this problem?
Quit using floats.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
How?
How what?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Quote
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.
Logged

Pages: [1]   Go Up
Jump to: