g++/gcc : Floating point issue
miridian
miridian@nerdshack.com
Fri Jul 28 19:56:00 GMT 2006
abuzer.salik@tcs.com wrote:
> I have an application which uses double values a lot. If I try to put say
> 401.74 in a double variable it is getting stored as 401.74000000000001
> which creates problem in the calculations down the line. ( I am checking
> these values using gdb debugger).
>
> So, I am wondering if there is a way by which I can store the correct
> values (i.e. 401.74) in the double.
No.
Only dyadic rationals (a rational number of the form a/(2^b) where a is
an integer and b is a natural number) have a finite binary expansion.
40174/100 = 200087/50 (and 50 is not 2^b for a natural number b)
See the links:
http://en.wikipedia.org/wiki/Binary_numeral_system#Representing_real_numbers
http://en.wikipedia.org/wiki/Dyadic_fraction
> Do we need to look for another compiler/hardware or current setup can
> handle this?
You can use an arbitrary precision numeric library, for ex. GMP
http://www.swox.com/gmp/
Regars,
Luca.
More information about the Gcc-help
mailing list