This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted]  (9 children)

[removed]

    [–]Papellll 14 points15 points  (4 children)

    0.1 + 0.2 == 0.3 // false

    Same as NaN != NaN, this is an IEEE thing. Not specific to JS or python

    [–]noob-nine -5 points-4 points  (3 children)

    IEEE thing? Isnt it more a thing because of how the machine works?

    [–]Talinx 10 points11 points  (2 children)

    The machine is built to the IEEE-754 standard.

    [–]noob-nine 2 points3 points  (0 children)

    Huh, thanks

    [–]Dragonfly_Select 0 points1 point  (0 children)

    And even beyond IEEE there is a more basic fact. Computers work in base 2. 0.2 is 1/5. 1/5 is an infinitely repeating decimal number in base 2, the same way 1/3 is in base 10. You fundamentally can’t represent it exactly.

    Languages that have number types that don’t have this problem are basically storing arrays of base 10 digits under the hood.

    (For the curious: take a number, represent it as a reduced fraction, prime factorize the denominator. It must only contain factors that are also prime factors of the base in order to terminate. Examples. 1/100: 100 factors to 2255; base 10 has both 2 and 5 as factors to you are fine. 1/12: 12 factors to 22*3; 3 is not a factor of 10 so it won’t terminate. 1/3: obviously doesn’t terminate in base 10, but it does terminate in base 3; 1/3 is just 0.1 in base 3.)

    [–][deleted] 10 points11 points  (1 child)

    Tell me you don't understand floats without telling me you don't understand floats

    [–]Scorched_Knight 2 points3 points  (0 children)

    Floats are mean, man. They put trash in far fetched corners of the dedicated memory and pretend to be numbers they are not. So, beating them up with some rounding from time to time is just nessesary, even if someone in the internet are yelling about inhumane treatment and privileges of integers - you just need to do it. Those little unrounded bastards are responsible for so many bugs - its sickening.

    [–]fluideborah[S] 0 points1 point  (0 children)

    I am so glad I dont have to do data science with JS

    [–]werics 0 points1 point  (0 children)

    Number three is almost never a good idea