this post was submitted on 14 Nov 2026
372 points (97.2% liked)

Programmer Humor

34135 readers
1590 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 3 years ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] ThunderComplex@lemmy.today 73 points 1 day ago (4 children)

I think this, and the fact that NaN == NaN is false, make perfect sense.
NaN is the result of mathematically undefined behavior. You’re not going to get a NaN outside of a number context, so tying it together in the Number scope is fine.

[–] LillyPip@lemmy.ca 4 points 1 day ago (1 children)

Same reason NULL != NULL. It makes sense if you understand how these values work.

[–] SpaceNoodle@lemmy.world 17 points 1 day ago (2 children)
[–] LillyPip@lemmy.ca 9 points 1 day ago* (last edited 1 day ago) (1 children)
[–] SpaceNoodle@lemmy.world 17 points 1 day ago (1 children)

Ah, my brain was in C mode, where NULL is functionally 0. This is about the concept, which doesn't apply equally to all languages.

[–] LillyPip@lemmy.ca 8 points 1 day ago* (last edited 1 day ago)

Yep. There are some languages where this applies, because it’s a mathematical concept. The point is it’s the same ‘no value’ concept, which isn’t ~~shared by~~ implemented the same way by all languages.

C is strongly typed, and uses pointers, so NULL sort of intrinsically has a value in C.

edit: my wording was a bit off

[–] djmikeale@feddit.dk 5 points 1 day ago* (last edited 1 day ago)

You can kinda think of it as null meaning undefined. In this case, "undefined does not equal undefined" seems easier for me to wrap my head around

[–] VAK@lemmy.world -4 points 1 day ago

It feels silly that a high level language has NaN at all

[–] threeonefour@piefed.ca -4 points 1 day ago (3 children)

The result of mathematically undefined behaviour should be an error, but sure, go ahead and return Keleven. 1/0 = keleven fuck it.

[–] bss03@infosec.pub 34 points 1 day ago

NaN is one of the ways IEEE maths signals an erroneous calculation when the only outbound communication channel is the returned IEEE value. Reading the history and rationale of the floating-point standard will likely convince you this was and probably is a good compromise.

Because IEEE values often have hardware acceleration that is a significant factor in performance, languages rarely (if ever) choose to map all IEEE error values to language-specific error reporting (e.g. a thrown exception or error-tracking algebraic data type).

JS is hardly an outlier here.

I do think it probably violates the principle of least surprise, but I don't think I understand the scale of "momentum" change that would need to take place across society (industry, academia, etc.) to align it with naive expectations.

[–] ThunderComplex@lemmy.today 18 points 1 day ago (1 children)

NaN is a error and it’s your responsibility to handle it gracefully. No need to halt the entire program when you fuck up some maths.

[–] threeonefour@piefed.ca 10 points 1 day ago (2 children)

NaN is a error

Error.isError(NaN)
// False
[–] lime@feddit.nu 16 points 1 day ago* (last edited 1 day ago)

nan is a ieee754-defined error. what javascript calls errors these days (it tends to change a lot) has little to nothing to do with that. it's on every developer to familiarise themself with ieee754, it's one of the most important documents in the profession.

while nan is an invalid floating-point number, it is a valid value for a float-typed field. making it into an error is like adding a bit labelled "crash" to every number in your program.

[–] Lupie@sopuli.xyz 7 points 1 day ago* (last edited 1 day ago) (2 children)

No, it should not be an error. If you're making a calculator and your user enters 1/0, your program should not have the propensity to crash; the result should simply be NaN and you should choose how you display that result to your user.

[–] daychilde@lemmy.world 5 points 1 day ago (1 children)
[–] bss03@infosec.pub 2 points 1 day ago

fault v. error v. failure

In common language they can be synonyms, but often aren't in places (like computing) where there's a useful difference. A page fault isn't an error; a handled error doesn't result in a failure; fault-tolerant systems fail less often than their components fault.

[–] treadful@lemmy.zip 6 points 1 day ago (1 children)

You talk like error handling isn't a thing.

[–] anewfox@lemmy.zip 1 points 1 day ago* (last edited 1 day ago) (3 children)

fun fact: getting NaN from a numeric calculation is the error that you handle. if this wasn't obvious, please don't write code for anyone but yourself. hope this helps.

[–] daychilde@lemmy.world 8 points 1 day ago

if this wasn’t obvious,

If this wasn't obvious, be glad of the chance to learn.

If we all stopped as soon as something simple wasn't obvious, nobody would learn and nobody would do anything.

What a terrible reply to someone. It cuts off curiosity and says "You are too stupid to do anything."

Yet I bet there was a time that even with that very specific thing, you didn't think of it that way.

[–] lemonsorrel@lemmy.world 1 points 1 day ago

newcomers to shitting on javascript sometimes learn to blame the tool

[–] treadful@lemmy.zip -2 points 1 day ago

NaN is a primitive value, not an Error.

>> typeof 1 / "a"
"number"