|
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!
(comments are locked)
|
|
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. Some more concrete examples:
Aug 17 '12 at 03:37 PM
Bunny83
:D Well infinity is a special value, not just the highest / lowest possible value. They are treated mathematically correctly, so when you divide a number by infinity it becomes 0.0f and so on. NaN is actually the result of an error. It depends on your formula for which of those values you want to check and what you do in those cases.
Aug 17 '12 at 04:02 PM
Bunny83
(comments are locked)
|
|
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. chad, I totally hear you, but what we need here is EXTREMELY DECISIVE INFORMATION I found it hard to find any exact, compelling doco on this online....
Aug 17 '12 at 03:21 PM
Fattie
Use IsNaN to determine whether a value is not a number. It is not possible to determine whether a value is not a number by comparing it to another value equal to NaN.
Aug 17 '12 at 03:38 PM
ChadM
(comments are locked)
|
|
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. Matt - in computing, be careful about comparing your divisor to zero !!! the problem is not zero, but any relatively very small number. Checking divisor == 0.0 never works. You should edit your answer in case someone gets the wrong info in the future !
Aug 17 '12 at 03:46 PM
Fattie
fixed, cheers
Aug 17 '12 at 03:55 PM
Weitzel
(comments are locked)
|
