This is the mail archive of the
cygwin@cygwin.com
mailing list for the Cygwin project.
Re: atof returns a value that is slightly greater than the original string
- To: Marco Craveiro <soupdragon at clix dot pt>
- Subject: Re: atof returns a value that is slightly greater than the original string
- From: Charles Wilson <cwilson at ece dot gatech dot edu>
- Date: Mon, 10 Sep 2001 17:09:09 -0400
- CC: cygwin at cygwin dot com
- References: <1000153353.1101.8.camel@darkroom>
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/