Floating point math errors

I have an application using an ESP8266 with an ADS1115 and acs711 to measure current. Using the following formula I get incorrect current readings.

current = (avg - 1650) / 45 * 100

where avg = average of several mV readings from the A/D converter

Some PROOF that there are floating point calculations happening would be important.
Some PROOF that the results are wrong is ABSOLUTELY MANDATORY.

I have an application using an ESP8266, ADS1115 A/D converter and ACS711 to measure current. I have programmed the ESP with Arduino IDE 1.8.5. When I use the following formula I get an incorrect result for small currents. All variables are floats.

current = (avg - 1650) / 45 * 100;

where avg = the average of several mV readings from the A/D converter
1650 = ~ 1/2 the supply voltage
45 = mv/Amp
100 = scaling factor, I transfer the current X 100 as an integer and divide by 100 to get 2 decimals

However, if I use the following formula I get the expected result.

current = (avg - 1650) * 100 / 45;

It appears the single precision floats cannot accurately represent small negative numbers (eg. -0.1).

What am I missing?

John

PS sorry for the double post. I hit some key and the message window closed

Don't double-post!
http://forum.arduino.cc/index.php?topic=545220.0

You haven't provided nearly enough information to say anything useful.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Try,

current = (avg - 1650.0) * 100.0 / 45.0;

What is the size of the 711 and what range of currents do you want to measure, in particular, how low?

Tom... :slight_smile:

The ACS711 is a ratiometric sensor.
The ADS1115 is for voltage measurements.
Not a good combination.
Leo..

Hi,
Just looked at your post after a coffee.

If you do both equations with 6 decimal places you might find a difference.

if
avg = 1000

first equation; 1000-1650 = -650.0
-650.0/45.0 = -14.444444
-14.444444 * 100.0 = -1444.444000

second equation 1000-1650 = -650.0
-650.0 *100.0 = -65000.0
-65000.0 /45.0 = -1444.444444

I think thats right...

Tom... :slight_smile:

Yes but the op was using int's.

Mark

holmes4:
Yes but the op was using int's.

Not if avg was decalred to be float. The ints get promoted.

It appears the single precision floats cannot accurately represent small negative numbers (eg. -0.1).

It's more likely that your code to print the value does not print out the precision that you want.

Always treat a floating-point value as being an approximation. Floats can only exactly represent numbers that are exact multiples of powers of 2. 3.0/256 can be represented exactly. 0.1 cannot.

In addition to the above, a float variable can contain only 6-7 accurate decimal digits, so there is no difference between the internal representations for 1.0000001 and 1.0000002. So, a major problem arises when you try to subtract nearly equal numbers, for example.

In general C/C++ supports type "double" than can represent 15-16 accurate decimal digits, but Arduino does not implement it.

Do not double post