atof returns a value that is slightly greater than the original string

Charles Wilson cwilson@ece.gatech.edu
Mon Sep 10 14:09:00 GMT 2001


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



--
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