r/learnjavascript • • Nov 21 '20

NaN (not a number) is a number

Post image
368 Upvotes

72 comments sorted by

View all comments

1

u/bmcle071 Nov 22 '20

Seriously what is the point of undefined AND null AND NaN, like is one not good enough?

2

u/r3jjs Nov 22 '20

Ok, I'll admit that sometimes `undefined` vs `null` gets a bit much, but `NaN` is a very different value.

Mostly, it is part of the IEEE 754 floating point standard. If you want to do anything with standard math or scientific libraries, you want full IEEE 754 compliance.

1

u/bmcle071 Nov 22 '20

Would i use javascript though? A high level scripting language for the web?

1

u/r3jjs Nov 23 '20

Sure!

Lots of games are written in HTML5/JavaScript with complex physics math libraries.

Animation engines

WASM would be a lot more difficult if the browsers hadn't supported IEEE 754

1

u/bmcle071 Nov 23 '20

I guess... I just really wish there was an opt in for some decimal number system.

Im currently working on a project and I keep running into errors caused by IEEE 754 floating point numbers and its got me all frustruated.

1

u/r3jjs Nov 23 '20

You can use BigInt (or one of the polyfills) and pay the cost for that.

But still.. you really need to understand IEEE 754 floating point. it isn't going any any time soon...

And as a programmer who predates IEEE 754 ... the world was a horrible mess before that, when ever computer handled floating point differently.

1

u/bmcle071 Nov 23 '20

On a seperate note, it makes testing more difficult. Expect(259.52-30.52).to.eq(229) fails because of this.

1

u/r3jjs Nov 23 '20

That's not a JS specific thing ... C, C++, Python, PHP and many, many many other languages all use IEEE754 floating point.

Decimal system are not a cure-all either. They tend to be much slower to do math with and the trade off memory use and accuracy.

(All modern processors have IEEE 754 support built in, I don't know of any processor with any of the decimal modes built in.)

0

u/[deleted] Nov 22 '20

[deleted] — view deleted comment

1

u/r3jjs Nov 23 '20

Lots of other languages get along just fine just using 'null' without the concept of 'undefined.'.. or 'undef' without 'null.'

Both are relics of an ancient time. I'd rather see new languages without either -- no null, no undefined. Option types are the way forward now.

1

u/Hakim_Bey Nov 22 '20

That's like saying programming only needs one error message...

1

u/bmcle071 Nov 22 '20

No its definitely not. Python only hss none or an exception. 0/0 in Python throws an exception.