In MSDN article, it mentions when fp:fast mode is enabled, operations like additive identity (a±0.0 = a
, 0.0-a = -a
) are unsafe. Is there any example that a+0 != a
under such mode?
EDIT: As someone mentioned below, this sort of issue normally comes up when doing comparison. My issue is from comparison, the psedocode looks like below:
for(i=0;i<v.len;i++)
{
sum+=v[i];
if( sum >= threshold) break;
}
It breaks after adding a value of 0
(v[i]
). The v[i]
is not from calculation, it is assigned. I understand if my v[i]
is from calculation then rounding might come into play, but why even though I give v[i]
a zero value, I still have this sum < threshold
but sum + v[i] >= threshold
?
The reason that it's "unsafe" is that what the compiler assumes to be zero may not really end up being zero, due to rounding errors.
Take this example which adds two floats on the edge of the precision which 32 bit floats allows:
float a = 33554430, b = 16777215;
float x = a + b;
float y = x - a - b;
float z = 1;
z = z + y;
With fp:fast, the compiler says "since x = a + b, y = x - a - b = 0, so 'z + y' is just z". However, due to rounding errors, y actually ends up being -1, not 0. So you would get a different result without fp:fast.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With