Subject: Re: Unexpected "double" behaviour
To: Matthew Fincham <matthewf@cat.co.za>
From: Dr. Rene Hexel <rh@vip.at>
List: netbsd-help
Date: 09/26/2001 10:06:59
Matthew Fincham wrote:

> I would appreciate any ideas on why this is like this. I can get around the
> problem by using a float, but there is either a gap in my understanding, or
> a some other problem that would be nice to know about :-) .

  Take a look at the binary representation of 4.52 and you will see that
it requires an infinite number of digits to the right of the decimal
(err, binary :-) point.  Float and double representations, however, have
to cut off after some finite number of digits (i.e., you get something
like 4.5199998).  If you multiply the resulting number by 100, you also
multiply the difference between the exact and the rounded-off
representation by 100, giving you (approximately) 451.99998 for float
and the same with a few more 9's for double.

  If you use "%f" to print the result, the number will be rounded at
some digit, giving you 452.00000 (or 451.99998 for float).  However, if
you convert the number to integer, it will simply be chopped of at the
decimal point, giving you 451.

  This only happens with numbers that cannot be converted exactly (given
a finite number of digits) between decimal and binary formats.

  Cheers
      ,
   Rene