Skip to content

Both float examples aren't WTF #8

Open

Description

@FlorianUekermann

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.

Activity

  1. njt1982 commented on Sep 19, 2021

    @njt1982
    Contributor

    @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.

  2. FlorianUekermann commented on Sep 19, 2021

    @FlorianUekermann
    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.

  3. njt1982 commented on Sep 20, 2021

    @njt1982
    Contributor

    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 :) 👍🏻

  4. FlorianUekermann commented on Sep 20, 2021

    @FlorianUekermann
    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 !=, 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.

  5. Ozaq commented on Jan 31, 2023

    @Ozaq

    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.

  6. cD1rtX3 commented on Jan 25, 2026

    @cD1rtX3

    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.

    Minor nitpick: This implies != is a comparison according to IEEE 754. It is not. It is the inverse of the == operator. 0 != NaN is true.

  7. Ozaq commented on Jan 26, 2026

    @Ozaq

    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.

    Minor nitpick: This implies != is a comparison according to IEEE 754. It is not. It is the inverse of the == operator. 0 != NaN is true.

    Good catch!

  8. FlorianUekermann commented on Jan 26, 2026

    @FlorianUekermann
    Author

    Minor nitpick: This implies != is a comparison according to IEEE 754...

    Thx, fixed the original comment by replacing

    except for NaN!=NaN

    with

    except for !=

  9. Ozaq commented on Jan 26, 2026

    @Ozaq

    Now this only needs to be updated in the page as well :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions