

- #JQUERY CYCLE THROUGH NASA PICTURE OF THE DAY HOW TO#
- #JQUERY CYCLE THROUGH NASA PICTURE OF THE DAY CODE#
#JQUERY CYCLE THROUGH NASA PICTURE OF THE DAY HOW TO#
Here’s a cheat sheet for how to check for whether some value x is NaN in a few popular programming languages: For example, in JavaScript, you can use the function isNaN like this: > isNaN(Math.sqrt(-1)) true Most programming languages that support floating point calculations provide functions to check for NaN values.
#JQUERY CYCLE THROUGH NASA PICTURE OF THE DAY CODE#
If you care about the poor slob who will have to maintain the code later, you can write something that’s not so cryptic and weird. Ask whether a variable is not equal to itself, and if the answer is yes, it’s Not a Number.īut there is a better way. Some programmers have used this behavior to detect NaN by comparing a variable with itself: > x = x false > x != x true > if (x != x) console.log('Not a number!') Not a number!

It’s because whenever you ask whether NaN is equal to anything, even another NaN, the answer is no. Something like this: > let x = 0/0 undefined > x NaN > x = NaN false For example, back in JavaScript, we could ask whether a variable is equal to NaN. Now that we are aware of NaN problems, how do we guard against bugs involving it?Īt first, one might be tempted to check whether a numeric value is NaN using a simple comparison. For example, if x is NaN, then sqrt(x*x + 1) will also be NaN.

NaN can bite you in languages with strict types also. In C or Java, the compiler would notice the undefined symbol and would fail with an error message.īut don’t be too smug. This particular problem, blithely accessing a nonexistent property, can’t happen in a language with compile time checking of types. Ha ha, that’s what you get for using a language without static type analysis. Thus when I asked if the value of diff was unacceptably large, the unit test did not enter the if statement and the unit test “passed.” Not Just a JavaScript Problem NaN has some quirks: > diff > 3.0 false > diff diff = 3.0 falseĪs you can see above, if you ask whether NaN is greater than, less than, or equal to some numeric value, the answer is always no ( false). In JavaScript, subtracting a number from undefined results in NaN, a value that means “Not a Number.” $ node > const polar = undefined > const diff = polar.distance - 5.0 undefined > diff NaN So why does this botched test think the answer is correct? Let’s drop into the Node.js command line and see what’s going on. It doesn’t matter what ridiculous value the Polar function calculates and stores in radius, because the unit test never looks at that value. The object returned by this function doesn’t even contain a distance property! The unit test should have checked the radius property instead. The if statement on line 5 is supposed to verify that converting the vector (3, 4) to polar coordinates results in a distance very close to 5. This is a contrived unit test for a function called Polar. I’ll illustrate what happened using much simpler code. On a hunch, I used a debugger to step through the JavaScript code and watch how it worked. How could all my carefully crafted unit tests say everything was fine in JavaScript, when there was such an obvious problem in Python? Both algorithms were doing the exact same thing. I was stumped because I couldn’t find any mistakes in my translation from JavaScript to Python. The Python version crashed trying to access a nonexistent property. A few days later I started to port the allegedly working JavaScript code to Python. Like a good developer should, I wrote unit tests that compared the results with expected values. I wrote some code that performed some complicated floating point calculations.
