[Issue 1756] New: comparing a constant to artihmetic expression with floating point types fails

Jarrett Billingsley kb3ctd2 at yahoo.com
Mon Dec 31 06:39:22 PST 2007


<d-bugmail at puremagic.com> wrote in message 
news:bug-1756-3 at http.d.puremagic.com/issues/...
> http://d.puremagic.com/issues/show_bug.cgi?id=1756

> the direct comparison of a constant to an arithmetic expression does not 
> work
> correctly when using variables:
>
> // float
> float a = 0.6f, 0.8f;
> float c = a / b;
> assert(c == 0.6f / 0.8f); // ok
> assert(c == a / b); // fails
> assert(0.75f == 0.6f / 0.8f); // ok
> assert(0.75f == a / b); // fails
>
> // double
> double a = 0.6, b = 0.8;
> double c = a / b;
> assert(c == 0.6 / 0.8); // fails
> assert(c == a / b); // fails
> assert(0.75 == 0.6 / 0.8); // ok
> assert(0.75 == a / b); // fails

You forgot one.

// real
real a = 0.6, b = 0.8;
real c = a / b;
assert(c == 0.6 / 0.8); // ok
assert(c == a / b); // ok
assert(0.75 == 0.6 / 0.8); // ok
assert(0.75 == a / b); // ok

They all pass when you use real because the compiler does constant folding 
by casting all floating point constants to real.  When you then compare it 
to a float or double, it fails because the constant is more precise than the 
float or double. 




More information about the Digitalmars-d-bugs mailing list