Say you have some code like this:
obviously, in real production code you have to check somehow that you';re not creating an infinity. I have used the following code to check for such a problem:
However! I have recently seen cases where that still produces a NaN error, at least in the editor. So today I do this
So, does anyone decisively understand, exactly, what the difference is between IsNan, IsInfinity, and so on? What's the best idiom to use in this situation?
If so, thank you!
Exactly, NaN(Not a Number) is not the same as infinity. floats have actually a lot different "error types", but they all result in NaN. Infinity is an actual number, at least the representation. NaN actually says something is wrong because the result is not a number. The squareroot of a negative value will produce NaN since the result it's not defined within the real numbers.
NaN also "infects" calculations and always produce NaN as result.
This will result in NaN because C is NaN. When ever you use a NaN value in a calculation the calculation will also result in NaN.
Furthermore NaN can't be compared to any value, even to itself.
This will result in "false" because you can't compare NaN to anything.
So if your operands don't contain a NaN value, something went wrong in the calculation.
answered Aug 17 '12 at 03:33 PM
NAN as far as I know is 'Not A Number' and I suppose if the number got too large, or too small, it could result in an NAN exception.
answered Aug 17 '12 at 03:18 PM
When compiled, the code will be attempted to execute a potential divide by either zero, or a very near zero number, which will result in vx being set to NAN. Typically if you end up doing something like this, it is unintentional and therefore does not default to setting it to infinity.
You can always check the precondition and postcondition of your variables and deliberately set to infinity if you'd like/need, which can be represended by the mathematical representation of infinity, and can be helpful. I believe the isInfinity or isNegativeInfinity checks deal check for and compare against these.