Half of this is just floating-point stuff, which isn't JavaScript's fault, and is stuff everyone should know about anyway. Not to excuse the other half, which is definitely JS being weird-ass JS, but if you're blaming JS for 0.1 + 0.2 != 0.3, or NaN + 1 being NaN, then you need to go learn more about floating-point arithmetic.
Ok, half was an exaggeration. There are 6 of the 25 that are direct consequences of floating-point arithmetic. If you can't work out which 6, then yes, you should go learn more about floating-point arithmetic.
To save you the trouble of going back through the quiz, the six are:
No, floating-point division by zero is completely well-defined. Division by zero always gives an (appropriately signed) infinity, except for 0/0 and NaN/0 which are NaN.
Floating-point arithmetic is not real mathematics. Quantities like 'infinity' and 'NaN' are well-defined values, with well-defined behaviours. Of course, these behaviours are chosen to capture the spirit of real mathematics, but it can be a trap to think too closely to mathematics in how something like division by zero behaves. IMO it's probably best to just think of it as a special case.
these behaviours are chosen to capture the spirit of real mathematics
Right, and that's why 0/0 is undefined instead of Infinity.
IMO it's probably best to just think of it as a special case.
Regardless, there's no floating point arithmetic going on in that example. There arguably is in 1/0, but not 0/0. There is zero arithmetic happening in 0/0.
Right, and that's why 0/0 is undefined instead of Infinity.
NaN is not 'undefined'. It is a well-defined possible value that a floating-point type can take. If 0/0 were truly undefined, then the entire program would become meaningless as soon as that expression was evaluated. That's the case in mathematics: if you have 0/0 appear in a mathematic proof (and you've not taken great pains to define exactly what that means) then your proof is meaningless. That's not true in JavaScript: if you have 0/0 appear, it just evaluates to an appropriate NaN and execution continues.
Regardless, there's no floating point arithmetic going on in that example.
Yes there is. Writing 0/0 in JavaScript is a double-precision floating-point operation. It is the division of positive zero by positive zero.
Writing 0/0 in JavaScript is a double-precision floating-point operation. It is the division of positive zero by positive zero.
The point is it's not actually doing ANY FP arithmetic. There's zero oddness arising from loss of precision or other weird quirks of the actual arithmetic as in the others. If you could perfectly describe the behavior of FP numbers in a computer, you'd still have the exact same problem.
No, there's a very fundamental difference between 0/0 in the mathematics of real numbers, where such an object just does not exist, and in floating-point arithmetic, where it evaluates to NaN which is simply one possible value a floating-point number can take, and is not fundamentally different to 0.0 or 1.0 or infinity. NaN is not some 'error', it is really (despite its name) just another number. That only comes from the way floating-point is defined, not from any fundamental mathematical truth.
37
u/stalefishies Jun 28 '21
Half of this is just floating-point stuff, which isn't JavaScript's fault, and is stuff everyone should know about anyway. Not to excuse the other half, which is definitely JS being weird-ass JS, but if you're blaming JS for 0.1 + 0.2 != 0.3, or NaN + 1 being NaN, then you need to go learn more about floating-point arithmetic.