Float addition promoted to double?
Posted
by Andreas Brinck
on Stack Overflow
See other posts from Stack Overflow
or by Andreas Brinck
Published on 2009-12-03T11:01:48Z
Indexed on
2010/05/04
20:48 UTC
Read the original article
Hit count: 301
c++
I had a small WTF moment this morning. Ths WTF can be summarized with this:
float x = 0.2f;
float y = 0.1f;
float z = x + y;
assert(z == x + y); //This assert is triggered! (Atleast with visual studio 2008)
The reason seems to be that the expression x + y
is promoted to double and compared with the truncated version in z
. (If i change z
to double
the assert isn't triggered).
I can see that for precision reasons it would make sense to perform all floating point arithmetics in double precision before converting the result to single precision. I found the following paragraph in the standard (which I guess I sort of already knew, but not in this context):
4.6.1.
"An rvalue of type float
can be converted to an rvalue of type double
. The value is unchanged"
My question is, is x + y
guaranteed to be promoted to double or is at the compiler's discretion?
UPDATE: Since many people has claimed that one shouldn't use ==
for floating point, I just wanted to state that in the specific case I'm working with, an exact comparison is justified.
Floating point comparision is tricky, here's an interesting link on the subject which I think hasn't been mentioned.
© Stack Overflow or respective owner