Have you performed simple arithmetic operations like 0.1 + 0.2? You might have gotten something strange: 0.1 + 0.2 = 0.30000000000000004.

  • luciole (he/him)@beehaw.org
    link
    fedilink
    arrow-up
    18
    ·
    1 day ago

    Floating-point arithmetic is important to understand at least vaguely since it’s a pretty leaky abstraction. Fortunately, we don’t need a “✨Member-only story” on Medium to get acquainted with the underlying concepts.

  • karlhungus@lemmy.ca
    link
    fedilink
    arrow-up
    10
    arrow-down
    1
    ·
    1 day ago

    Ugh, i thought this was a question, not a link. So i spent time googling for a good tutorial on floats (because I didn’t click the link)…

    Now i hate myself, and this post.

  • RagingHungryPanda@lemm.ee
    link
    fedilink
    arrow-up
    8
    arrow-down
    1
    ·
    1 day ago

    It’s how CPUs do floating point calculations. It’s not just javascript. Long story short, a float is stored in the format of one bit for the +/-, some bits for a base value (mantissa), and some bits for the exponent. As a result, some numbers aren’t quite representable exactly.

  • Zagorath
    link
    fedilink
    arrow-up
    5
    arrow-down
    1
    ·
    1 day ago

    A good way to think of it is to compare something similar in decimal. .1 and .2 are precise values in decimal, but can’t be represented as perfectly in binary. 1/3 might be a pretty good similar-enough example. With a lack of precision, that might become 0.33333333, which when added in the expression 1/3 + 1/3 + 1/3 will give you 0.99999999, instead of the correct answer of 1.

    • toasteecup@lemmy.world
      link
      fedilink
      English
      arrow-up
      1
      arrow-down
      7
      ·
      1 day ago

      Python has no issues representing

      1/3 + 1/3 + 1/3

      as 1. I just opened a python interpreter, imported absolutely no libraries and typed

      1/3 + 1/3 + 1/3 enter and got 1 as the result. Seems like if python could do that, JavaScript should be able to as well.

      • Zagorath
        link
        fedilink
        arrow-up
        5
        arrow-down
        1
        ·
        1 day ago

        I thought it was a rather simple analogue, but I guess it was too complicated for some?

        I said nothing about JavaScript or Python or any other language with my 1/3 example. I wasn’t even talking about binary. It was an example of something that might be problematic if you added numbers in an imprecise way in decimal, the same way binary floating point fails to accurately represent 1/10 + 1/5 from the OP.

        • ericjmorey@programming.dev
          link
          fedilink
          arrow-up
          1
          ·
          edit-2
          1 day ago

          Perhaps the following rewording of your last sentence would be easier for readers to follow along:

          With a lack of precision, 1/3 might become 0.33333333. When evaluating the expression 1/3 + 1/3 + 1/3, using 0.33333333 as an approximate representation of 1/3 will return a result of 0.99999999, instead of the correct answer of 1.

        • toasteecup@lemmy.world
          link
          fedilink
          English
          arrow-up
          2
          arrow-down
          1
          ·
          1 day ago

          I’ll pass on the js interpreter. I don’t feel like learning the arcane runes.

          To your point, Python handles those by giving you 0.300000004 might have missed a zero but valid point nonetheless

  • thingsiplay@beehaw.org
    link
    fedilink
    arrow-up
    1
    ·
    1 day ago

    If you are adding 0.1 + 0.2, then it means you can cut off anything after the first digit (after the dot off course). Because the rest of the 0.1 is only 0 and the rest of 0.2 is 0. That can help with rounding errors on floating point calculations. I don’t program JavaScript, so no idea what the best way to go about it would be.