Slide 1

Slide 1 text

NaN

Slide 2

Slide 2 text

"NaN" stands for: Not a Number

Slide 3

Slide 3 text

What kinds of things give us NaN?

Slide 4

Slide 4 text

Fuzzy math console.log( 0 / 0, Infinity / Infinity, 0 * Infinity, Infinity - Infinity, Math.pow(1, Infinity) ); > NaN NaN NaN NaN NaN NaN

Slide 5

Slide 5 text

Complex Numbers console.log( Math.sqrt(-1), Math.log(-1), Math.acos(2), Math.asin(2) ); > NaN NaN NaN NaN

Slide 6

Slide 6 text

Turning things into Numbers console.log( parseInt('hello'), parseFloat('world'), Number(undefined), Number({}), +{}, +undefined, +new Date('hello') ); > NaN NaN NaN NaN NaN NaN NaN

Slide 7

Slide 7 text

What is NaN? (in JavaScript)

Slide 8

Slide 8 text

"Not a Number" is... console.log(NaN); > NaN ... a particular JavaScript value. (very particular)

Slide 9

Slide 9 text

"Not a Number" is... console.log(typeof NaN); > number ...a Number.

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

"Not a Number" is... console.log(NaN === NaN); > false ...not "Not a Number".

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

"Not a Number" is... var assert = require('assert'); assert.equal(NaN, NaN); > AssertionError: NaN == NaN ...tricky to test.

Slide 14

Slide 14 text

"NaN" actually stands for: Not a NaN

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

So how do we tell if something is NaN?

Slide 17

Slide 17 text

Easy! Just use isNaN: console.log(isNaN(NaN)); > true

Slide 18

Slide 18 text

Or maybe not... console.log(isNaN('hello'), isNaN(['hello']), isNaN({})); > true true true console.log(typeof 'hello', typeof ['hello'], typeof {}); > string object object

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

So let's just make our own: function myIsNaN(x) { return typeof x === 'number' && isNaN(x); } console.log(myIsNaN(NaN), isNaN(NaN), myIsNaN('hello'), isNaN('hello'), myIsNaN(['hello']), isNaN(['hello']), myIsNaN({}), isNaN({}) ); > true true false true false true false true

Slide 21

Slide 21 text

Or we can recall "Not a NaN": function myIsNaN(x) { return x !== x; } console.log(myIsNaN(NaN), isNaN(NaN), myIsNaN('hello'), isNaN('hello'), myIsNaN(['hello']), isNaN(['hello']), myIsNaN({}), isNaN({}) ); > true true false true false true false true

Slide 22

Slide 22 text

This works because NaN is the only non-reflexive value in JavaScript.

Slide 23

Slide 23 text

Number.isNaN was added recently: console.log(Number.isNaN(NaN), isNaN(NaN), Number.isNaN('hello'), isNaN('hello'), Number.isNaN(['hello']), isNaN(['hello']), Number.isNaN({}), isNaN({}) ); ...and it does what we want: > true true false true false true false true

Slide 24

Slide 24 text

But NaN isn't just a JavaScript thing...

Slide 25

Slide 25 text

NaN is actually defined by the IEEE754 floating-point standard.

Slide 26

Slide 26 text

Bit representation of a float32 value: 4 1-bit sign 4 8-bit exponent 4 23-bit significand 0 10000000 01000000000000000000000 -> 2.5 Note: the significand is actually 24 bits, but only 23 are explicitly stored.

Slide 27

Slide 27 text

Bit representations of special values: 0 11111111 00000000000000000000000 -> Infinity 1 11111111 00000000000000000000000 -> -Infinity 0 11111111 10000000000000000000000 -> NaN NaN values have a maximized exponent and a nonzero significand.

Slide 28

Slide 28 text

So these are also all NaN: 1 11111111 10000000000000000000000 -> NaN (quiet, negative) 0 11111111 10000000000000000000001 -> NaN (quiet, but different) 0 11111111 00000000000000000000001 -> NaN (signaling) 0 11111111 00000000000000000000010 -> NaN (signaling, but different) 0 11111111 00000000000000000000011 -> NaN (we can start counting!) How many NaNs are there, really?

Slide 29

Slide 29 text

2^24 - 2 = 16777214

Slide 30

Slide 30 text

And that's just with a float32! What about a double64?

Slide 31

Slide 31 text

2^52 - 2 = 4503599627370494

Slide 32

Slide 32 text

That's 4.5 * 10^15, or 4.5 quadrillion. 4.5 petabytes is about 10,000 years worth of music.

Slide 33

Slide 33 text

If there are so many different possible NaNs, then it only seems reasonable...

Slide 34

Slide 34 text

...that one NaN is unlikely to be the same as another NaN!

Slide 35

Slide 35 text

Thus, NaN !== NaN.

Slide 36

Slide 36 text

Some Related Links 4 http://ariya.ofilabs.com/2014/05/the-curious-case- of-javascript-nan.html 4 http://www.2ality.com/2012/02/nan-infinity.html 4 https://en.wikipedia.org/wiki/NaN

Slide 37

Slide 37 text

Who are you and where can I find the slides? 4 I'm Lewis J Ellis: @lewisjellis on Twitter and GitHub 4 My website is LewisJEllis.com. 4 Slides available at GitHub.com/LewisJEllis/nantalk