Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Both float examples aren't WTF #8

Open
FlorianUekermann opened this issue Sep 18, 2021 · 5 comments
Open

Both float examples aren't WTF #8

FlorianUekermann opened this issue Sep 18, 2021 · 5 comments

Comments

@FlorianUekermann
Copy link

Please remove both floating point examples. NaN == NaN and 0.1 + 0.2 == 0.3 should be false. Following IEEE 754 is the sane thing to do.
If you disagree with IEEE 754 that's a valid opinion, but not a JS issue.

@njt1982
Copy link
Contributor

njt1982 commented Sep 19, 2021

@FlorianUekermann Interesting - which bit of IEEE 754 is relevant here?

Part of the humour of this site isn't about highlighting technical bugs, but rather highlighting how JS many not behave how you expect it to behave.

Unless you understand the deep logic of computing (and what leads to 0.3 being very difficult to represent as a floating point value), then a lot of programmers would expect 0.1 + 0.2 to equal 0.3, like it does in the real world.

Likewise, even when you understand the intricacies of a language, it's very unintuitive that something is not equal to itself.

So while they are not "bugs"... I think they are still valid WTF's. Thats how most people react to them when they first discover them.

@FlorianUekermann
Copy link
Author

@FlorianUekermann Interesting - which bit of IEEE 754 is relevant here?

I don't recall which paragraph details how NaN works, but for the accuracy issue it's kinda the whole thing.
For context: IEEE 754 is a standard describing how floating point numbers work. Almost every processor follows that standard. And as a result, pretty much every programming language does as well. They don't have much of a choice.
The Wikipedia page for it is a pretty good summary.

Part of the humour of this site isn't about highlighting technical bugs, but rather highlighting how JS many not behave how you expect it to behave.

Fair point. I would argue though that this is expected. I would be extremely surprised in any language if I found that 64bit floating point numbers behaved differently.

Unless you understand the deep logic of computing (and what leads to 0.3 being very difficult to represent as a floating point value), then a lot of programmers would expect 0.1 + 0.2 to equal 0.3, like it does in the real world.

See my point above. Many other examples on the site are not just surprising to people without prior programming experience, but good examples of real language problems (the === operator was invented, because everyone realizes by now that the non-transitivity you show in the amazing holy trinity comparison is a side effect of an insane definition of ==).

Likewise, even when you understand the intricacies of a language, it's very unintuitive that something is not equal to itself.

Again, this has very little to do with the language. NaN comparisons always return false with ==, < and > in practically every language.

So while they are not "bugs"... I think they are still valid WTF's. Thats how most people react to them when they first discover them.

This may be true for people who are new to programming in general. For people who have worked with floating point in any language it would definitely be more of a WTF situation if JS did not behave like this.

Anyway, thanks for the fun website, especially the trinity image is great. I think I have made my case, please close if you disagree.

@njt1982
Copy link
Contributor

njt1982 commented Sep 20, 2021

I think, maybe, the solution here would be to explain on the page why 0.1 + 0.2 != 0.3 to in floating point mathematics. Expand upon the:

(Clearly this is a normal floating point problem, but still!)

Likewise, on the Nan page I could add some of the content you've provided above to explain why NaN is a special case.

Thanks for raising the issue :) 👍🏻

@FlorianUekermann
Copy link
Author

They still really aren't WTFs imo... ...but I can see how this may be different for other people. I guess especially JS is written by lots of people who don't have much of a tech background.

Here is an idea: Mark them very clearly as non-WTF (maybe even in green instead of red) and add a little more explanation like you suggested. This might really help some people understand floating point better.

Maybe like this.

Not WTF: NaN is a strange number

This is expected behavior in any programming language, because according to IEEE 754, all comparisons involving NaN return false, except for NaN!=NaN, which return true. See https://en.wikipedia.org/wiki/NaN#Comparison_with_NaN for details.

Not WTF: Floating Point Math

add this line in the code block 0.1 + 0.2 // 0.30000000000000004

This is expected behavior in any programming language using 64bit floating point numbers. Many decimal numbers can not be accurately represented as binary floating point numbers. Additionally, the result of any floating point operation is rounded to a representable number. These inaccuracies must be taken into account when comparing results of floating point operations.
See https://en.wikipedia.org/wiki/Double-precision_floating-point_format for details on 64 bit floating point numbers.

@Ozaq
Copy link

Ozaq commented Jan 31, 2023

I just wanted to open an Issue about those two points...

@FlorianUekermann is absolutely right, those are not javascript WTFs but well defined floating point behaviour.

Removing them would be a good thing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants