atof returns a value that is slightly greater than theoriginal string

Marco Craveiro soupdragon@clix.pt
Mon Sep 10 16:29:00 GMT 2001


On 10 Sep 2001 17:09:09 -0400, Charles Wilson wrote:
> Marco Craveiro wrote:
> 
> > Hello Cygwinners,
> > 
> > I'm having some strange problems with atof. basically, it returns a
> > converted value that is 0.000024 bigger than the one on the string.
> 
> 
> Welcome to the wonderful world of floating point representation.  This 
> type of error is quite common with floats -- because the float format is 
> intrinsically incapable of representing every real number with infinite 
> precision.  Unfortunately, that leads to "roundoff" errors that are a 
> bit non-intuitive, like your 0.000024.  (It's actually rounding off to a 
>   nice "round" number -- in the bitshifted, base-2, fixed-point notation 
> used for the mantissa in the float data format.  It just doesn't look 
> like a roundoff error when printed out in "normal" base-10 notation).
> 
> Workarounds: use a bigger float. (double?  long double?)  You'll still 
> have this sort of problem, but you'll only be off by 0.00000000000xx 
> instead of 0.000024.
> 
> Or used fixed-point math (e.g. what the previous poster suggested).
> 
> --Chuck
> 
> 
> 

Thanks everyone for the quality and quickness of the replies. I'm going
to find my notes on floating point representation. I knew I shouldn't
have missed those classes when I was back at uni :-)))

cheers!

marco


--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/



More information about the Cygwin mailing list