Error adding floats

Hi all.
I'm doing some maths to extract some float values stored as ascii in a byte array from another system.
When I extract (say) the 10ths value from the array and add it to the current integer total I get an erroneous value from the add function.
1000.000 + 0.900000 = 1000.900024

                  float a=1000, b=0.9;
                  Serial.printf("Sanity Check %f\n",a+b);

Why would this be happening, and how can I get the results I need?
Cheers,
Jim

Read the topic:
"How to get the best out of this forum

And clicking on < code> in the toolbar, post your complete code.
This way we will better understand your difficulty and be able to help you better.

Which arduino are you using?

Hello jimbo182

Welcome back.

The Arduino is an MCU and has no FP unit on board.

All FP operations are emulated, which leads to "errors".

hth

Have a nice day and enjoy coding in C++.

1 Like

Float values are not exact and you can expect only 6 to 7 of the digits in the decimal representation to be useful.

Here, the digits "24" are simply an artifact of how Serial.print() converts the stored binary value to decimal, and must be ignored.

1000.000 + 0.900000 = 1000.900024

3 Likes

Hi - I'm using the ESP32 in this case, programming in the Arduino environment using VS code.
I did post the offending code using the code section.

No, the results of the software and hardware calculations are identical.

2 Likes

Thanks for all the replies.
I am only interested in three places of accuracy - is there a cheap way to round the results when performing addition etc so as to avoid the errors?

Cheers, Jim

More detail than you want, but should be required reading for anyone using floating point.

1 Like

Use Serial.print(value, 3)

or

Serial.printf("Sanity Check %8.3f\n",a+b);
1 Like

Thanks.

Where's the code that uses the results? How do you use the results?

The cheapest way to do it is to let it calculate as it will and use just the three significant figures/ three decimal places, or whatever at the end of the calculation.

If your calculations depends on/is affected by the least significant bits of error, you may need to pay more attention to numerical methods

Errors in floating point operations cannot be avoided. They must be understood, and your program has to be written accordingly.

If you need higher accuracy in your calculations, use type double instead of float, where you get 15 to 16 digits of accuracy in the decimal representation. AVR-based Arduinos like the Uno, Mega, etc. do not support type double, by default.

1 Like

On a ESP32, convert it to double as soon as possible and do all the calculations with double.

double a=1000.0;
double b=0.9;

void setup() 
{
  Serial.begin(115200);
  Serial.printf("Sanity Check %f\n",a+b);
  Serial.print(a+b,16);
}

void loop() {}

Result:

Sanity Check 1000.900000
1000.8999999999999772

This is not a fair test, because the compiler might do the calculation.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.