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.
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.
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.
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.
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.
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.
Same reason NULL != NULL. It makes sense if you understand how these values work.
The result of mathematically undefined behaviour should be an error, but sure, go ahead and return Keleven. 1/0 = keleven fuck it.
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.
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.
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.
please do everyone a favor and stop writing code.
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.
You talk like error handling isn’t a thing.
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.
NaNis a primitive value, not anError.>> typeof 1 / "a" "number"