I'm writing a 3D game using OpenGL in D and I'm having trouble with floating points. When I divide zero with another float value (non zero) it will always be NaN rather than 0. This is causing troubles sense the NaN will propagate throughout the code sense everything else that is divided/multiplied with it also becomes NaN. I really don't want to wrap all my divisions with a if value != 0.0f
check.
A simple check like float value = getCurrentSpeed() / getMaxSpeed()
will return NaN if getCurrentSpeed()
return 0.0f.
When dividing, say x / y
, you can get four outcomes (providing that neither x
nor y
is NaN, +Inf, Inf
):
0 when y != 0 -- <- expected
+Inf x > 0 and y == 0
-Inf x < 0 and y == 0
NaN x == 0 and y == 0 -- <- actual
So in your case - NaN
when computing
float value = getCurrentSpeed() / getMaxSpeed()
we can conclude that both getCurrentSpeed()
and getMaxSpeed()
are zeros and you have to test getMaxSpeed()
for being 0
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments