TDPL: Overloading template functions
Rainer Schuetze
r.sagitario at gmx.de
Wed Jul 28 13:56:46 PDT 2010
Andrej Mitrovic wrote:
> Woops, I got confused. I was thinking about structs, not arrays.
>
> But yeah, in this case a float gets compared to a double and it seems to
> evaluate to false.
2.4 has no exact representation as a floating point value, as it is
2^^2*0.6, so the mantissa is 3/5. It is rounded after 24 bits for float,
but after 53 bits for a double. As a result
float f = 2.4; // approximately 2.4000001
double d = 2.4; // approximately 2.3999999999999999
assert(f == d);
fails, because the comparison is done after converting the truncated
float to double (as long as the compiler does not issue SSE instructions
with single precision).
In contrast, this
assert(2.4 == 2.4f);
passes, as the compiler keeps real precision until writing the value to
memory and evaluates the expression at compile time.
More information about the Digitalmars-d
mailing list