281
you are viewing a single comment's thread
view the rest of the comments
[-] threeonefour@piefed.ca -3 points 16 hours ago

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 29 points 15 hours 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 15 points 16 hours ago

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 11 points 16 hours ago

NaN is a error

Error.isError(NaN)
// False
[-] lime@feddit.nu 13 points 15 hours ago* (last edited 15 hours 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 5 points 15 hours ago* (last edited 15 hours ago)

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 4 points 11 hours ago
[-] bss03@infosec.pub 2 points 8 hours 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 5 points 14 hours ago

You talk like error handling isn't a thing.

[-] anewfox@lemmy.zip 3 points 14 hours ago* (last edited 14 hours ago)

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 5 points 11 hours 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.

[-] treadful@lemmy.zip -2 points 13 hours ago

NaN is a primitive value, not an Error.

>> typeof 1 / "a"
"number" 
[-] lemonsorrel@lemmy.world 0 points 13 hours ago

newcomers to shitting on javascript sometimes learn to blame the tool

this post was submitted on 14 Nov 2026
281 points (97.6% liked)

Programmer Humor

34135 readers
2088 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