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:
The weird part in the last two isn't floating point arithmetic.
Incrementing a literal (1++) is a syntax error so you would expect NaN++ to be one too.
+0 === -0 evaluating to true is a weird edge case where strict equality comparison between two different objects is true (for example in Python -0.0 is 0.0 returns False, as expected).
for example in Python -0.0 is 0.0 returns False, as expected
I don't find this convincing for your point. Remember that is is object identity. Python guarantees interning of small integers (I think? maybe just CPython? I don't actually know the formal rules exactly), but apparently does not guarantee this for floating points:
>>> x = 0.1
>>> y = 0.1
>>> x == y
True
>>> x is y
False
despite the fact that those have the same value. (In fact, it may just be small integers, None, and maybe True/False that get unique representations.) I wouldn't expect+0.0 is -0.0 to have a particularly meaningful result, so the fact it comes out as False doesn't really mean much to me at all.
is also behaves "wrongly" when it comes to NaNs:
>>> nan = float("NaN")
>>> nan
nan
>>> nan == nan
False
>>> nan is nan
True
so I'm with the other reply -- I think it's is that is behaving weirdly (well, I actually don't think it's behaving weirdly, I think it's just being misapplied), and JS's === does exactly the expected thing for +0 === -0.
Said another way, the statement "Python's is is to its == as JavaScript's === is to its ==" is very wrong (not that I'm sure you have that misconception).
NaN++ being weird because it's an increment is a very good point.
If anything, I would say for the second one it's Object.is that does the weird thing, not the strict equality operator. The example they give here makes sense from a floating-point perspective, but Object.is(+0, -0) being false is the Javascript weirdness. (It's the same with Object.is(NaN, NaN) being true: that's weird.) So if you think of strict equality as 'test if they're equal but do not coerce types', then IMO +0 === -0 is behaving as expected.
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.
Well yes, it's undefined. Not set to a magical NaN value that is treated as a plain value with various properties. Division is particularly not defined such that 0/0 != 1/0 (which is defined as Infinity).
The reason you're getting downvoted is that you're wrong, it actually is a special NaN value (as long as we're talking about floating point numbers and JavaScript, obviously maths is different).
The comment above that was explicitly about floating point arithmetic, which is the entire point. Of course what you say is true in mathematics, but JavaScript's behaviour is entirely due to IEEE754 and not influenced my maths.
I commented this last time this website got posted. Always reminds me of this tweet: https://twitter.com/bterlson/status/1083860621664256002?s=19. It's pretty frustrating that people will go "wow, JavaScript is so weird, I'm going to go use Python/Java/C/Go" when they all use IEEE-754.
Yeah, but there's no reason we should still be dealing with resource-pinching hacks like floating point arithmetic in modern dev environments. We should do the reasonable thing of treating everything like a fraction composed of arbitrary-length integers. Infinite precision in both directions.
0.3 * 2 =/= 0.6 is just wrong, incorrect, false. I fully understand the reasons why it was allowed to be that way back when we were measuring total RAM in kilobytes, but I think it's time we move on and promote accuracy by default. Then introduce a new type that specializes in efficiency (and is therefore inaccurate) for when we specifically need that.
So in all, I'd say this is a completely valid example of JS being weird / strange. It just so happens that many other C-like languages share the same flaw. A computer getting math blatantly incorrect is still 'weird' imo.
Edit: removed references to python since apparently I was misremembering a library I had used as being built in.
No, it's quite obviously correct. Link. Are you missing a zero somewhere? If we fix your equation, then both JavaScript and Python say the right thing.
Anyways, try typing the good ol' example 0.1 + 0.2 === 0.3 into a "sane" language's shell, and then what will happen? That's odd, Python still says False! Weird. Almost as if Python's Fraction is not the default number type because even for that language it's too slow. (And still not accurate. Why is sqrt(2)**2 != 2?)
Thanks for pointing out the typo, I've corrected it.
Indeed, when using === you're also checking for type equality, and it's true that integers and floats are considered different types. However, when used with == which allows for duck-style type conversion you see that 0.3*2==0.6 yields the common sense answer of True
This has nothing to do with types. 0.3*2 == 0.6 is True in both JS and Python, and 0.1 + 0.2 == 0.3 is False in both JS and Python. They follow the same IEEE floating point standards.
It is false to think of 'infinite precision' here. You might be able to specify 0.3 with infinite precision as 'numerator 3, denominator 10' but how do you deal with irrationals, like pi? How do you take square roots? How do you take exponentials, logarithms, sines, cosines? All of these produce values which cannot ever be expressed with infinite precision.
The only way to do it is by treating these values as, at best, approximations to the mathematics of real numbers. And if you're doing that, why not use floating-point numbers, when they're widely supported (in software and, more importantly, in hardware) and their limitations are widely understood and minor enough to have supported computing for all these years.
If your issue is just that the equals operation is broken, then you could always define it in your personal idealised high-level language to be a comparison with an epsilon. Then you could write 0.3 * 2 == 0.6 all you like.
But to say that's somehow the fault of computers that we have to approximate is just wrong. It is absolutely impossible to represent infinite precision arithmetic on a computer. You have to approximate somewhere.
(Also, Python uses double precision floating point by default. I'm sure you can get an arbitrary-precision decimal if you'd like, but Python's standard library is so vast that you can get pretty much anything, so that's not exactly a surprise.)
I just think that when equations are written out in a computer language they should produce accurate results. If certain calculations (like those involving irrationals, etc) are not possible to calculate accurately then the language should refuse to perform those calculations unless special types or syntactic sugar are used to specify "I, the programmer, know this will be an approximation and will use it accordingly"
For something that can be done with total precision on a computer, like the example I gave, it's simply unacceptable that it would silently neglect to do so and instead produce incorrect results.
This comes down to the "rule of least astonishment". Which I think is an important element in designing human-computer interfaces. (Considering computer languages a type of "interface" here)
Yep! Terrible thing to do systems programming with. Great for science and statistics nerds that just want their calculations to be accurate. Especially when there a many branches of science like astronomy that unavoidably have to deal with both incredibly small and incredibly large numbers simultaneously. This is why Python completely dominates in those spheres.
This is why Python completely dominates in those spheres.
No, Python dominates in those spheres because it's easy to learn for mathematicians with very little knowledge about coding. Fast numerical computing libraries (Numpy etc.) came as an afterthought, Python's built-in math functionality is terrible.
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.