6 ms·
Incidentally, I found a cool way of checking whether NaN occurred: Just do: void some_function(float number) { if(number != number) number was passed in
by laythea 9y ago
Incidentally, I found a cool way of checking whether NaN occurred:
Just do:
void some_function(float number)
{
if(number != number)
number was passed in as <NaN>
}
Took me a little bit to get my head around it but if the number is not equal to the number, then a problem occurred. I used this to stop camera code (driven by floats) from crashing when receiving non-sense input coordinates. It still doesn't work properly, but it doesn't crash now either! (at least not for that :)
- dvh 9y agoI thought JavaScript has isNaN function
- jcoffland 9y agoThat's not JavaScript.
- deleted 9y ago[deleted]
- ranit 9y agoJavaScript does have isNaN() function: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- idbehold 9y agoBetter to use Number.isNaN(): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isNaN https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- deleted 9y ago[deleted]
- jcoffland 9y agoYou should use isnan() or isfinite(). It will make your code more readable.
- aurelian15 9y agoBe careful though when compiling your code with -ffinite-math-only (which is part of -ffast-math and -Ofast). The compiler will likely replace your function with a constant expression returning false, since it assumes that NaNs cannot occur in your code. I found that the only safe way to actually check for NaNs when using these flags is to memcpy the float into a byte array (to avoid aliasing issues) and manually compare against the NaN bitmask.
- akrasuski1 9y agoI wouldn't be surprised if the future compilers would optimize it away as well, since ffast-math basicly makes NaN's equivalent to undefined behaviour.
- pwinnski 9y ago$deity preserve me from "cool ways" of doing things, and lead me into understand of isnan().