[Subject Prev][Subject Next][Thread Prev][Thread Next][Subject Index][Thread Index]

Re: int(x) is .ne. to x when x is whole number.

Dear Rai,

On Wed, Nov 29, 2000 at 02:20:04PM +0530, H.S.Rai typed:

> calculations. It is simply adding 0.2 to intial number 0.0 a number of
> times. I want to know root couse of problem as I assume such behaviour
> shhould not be there.

One can never guarantee that any real number can be accurately
rpresented by the "float" or "double" datatypes. Quoting from 
"info libc" :

   Floating-point numbers can represent a finite subset of the real
   numbers.  While this subset is large enough for most purposes, it is
   important to remember that the only reals that can be represented
   exactly are rational numbers that have a terminating binary expansion
   shorter than the width of the mantissa.  Even simple fractions such as
   1/5 can only be approximated by floating point.

In your specific case, the number 0.2 is not exactly representable by
the "float" data type. Because the operation you're doing is a repeated
addition, the error in representation of 0.2 gets accumulated over each
operation  (as you can observe from the results you'd quoted). In fact
if you had used a multiply operator instead of the repeated addition
(i.e "N*0.2" instead of adding 0.2 N times), the error would have been
lower and non-cumulative. (But of course still with no guarantee that
the comparison would return a success :):) )