Re: double to int conversion yields strange results

From: Michael Mair (Michael.Mair_at_invalid.invalid)
Date: 02/14/05


Date: Mon, 14 Feb 2005 12:07:46 +0100


Christian Kandeler wrote:
> Michael Mair wrote:
>
>
>> n = (double) (1.0 / d); /*difference*/
>
> How does this differ from "n = 1.0 / d"?

1.0/d may have been computed with higher precision and the result
may be there in higher precision, too. By telling the compiler
explicitly that I want the result converted to double (before
it is converted to int), I would expect the excess precision
to disappear.
In fact, if I try to calculate ***_EPSILON with gcc
and test
  fltepsilon = 1.0;
  while(1.0F < 1.0+fltepsilon)
    fltepsilon /= 2.0;
  fltepsilon *= 2.0;
I find fltepsilon==LDBL_EPSILON but if I insert a cast
  while(1.0F < (float)(1.0+fltepsilon))
I arrive at the right result. The same does not hold for double
and DBL_EPSILON unless I force gcc to do so.

In my understanding, inserting the cast should lead to the same
result as storing into an intermediate variable of the type we cast
to and using this variable for the next operation.

Maybe I understand something wrong but with the cast I would
expect the original example to work as expected by the OP on a
conforming implementation.

Cheers
  Michael

-- 
E-Mail: Mine is a   gmx dot de   address.


Relevant Pages