Adding *float + float seems to be not possible. [egcs-2.91.66]

Geoff Keating geoffk@envy.cygnus.com
Fri Dec 31 20:54:00 GMT 1999


Michal Bartczak <mikerro@ihh.pi.com.pl> writes:

> --Boundary-=_nWlrBbmQBhCDarzOwKkYHIDdqSCD
> Content-Type: text/plain
> Content-Transfer-Encoding: 8bit
> 
> gcc version:
> egcs-2.91.66
> system type:
> red-hat 6.0
> Linux 2.2.5-15 i686 unknown (pentium II)
> 
> options for compiler:
> gcc -Wall <source_name> -lm ;)
> 
> Sources included. Output is:
> /*************** snip   *****************/
> 2, 0.000000, mult=10.000000  whole:2.000000
> 2, 2.000000, mult=10.000000  whole:22.000000
> Dot 1, 22.000000, mult=10.000000  whole:(Adding 0.100000 * 1.000000 = 0.100000)  Adding 22.000000 + 0.100000 = 22.100000   22.100000
> Dot 1, 22.100000, mult=100.000000  whole:(Adding 0.010000 * 1.000000 = 0.010000)  Adding 22.100000 + 0.010000 = 22.110001   22.110001
> /* rest is not important, because bug is visible here */
> 
> how can be 22.1 + 0.01 = 22.110001 ? Accumulating error? 

This is perfectly normal IEEE floating-point behavior.

((float)22.1) is actually 22.1000003815, 0x1.61999a0000p+4
((float)0.01) is actually  0.0099999998, 0x0.0028f5c280p+4

their sum is 22.1100006104 (it gets rounded up), 0x1.61c2900000p+4.

Under systems that implement the latest C standard (particularly with
glibc 2.1), you can use

printf("%a\n", (float)22.11);

to see the floating-point value in hexadecimal, which is helpful for
checking this sort of thing.


-- 
- Geoffrey Keating <geoffk@cygnus.com>



More information about the Gcc-bugs mailing list