Why can't computers add 0.1 and 0.2?

LucasVDOP

Seriously, 0.30000000000000004? How does anything work?

2

Comments 5

Join the discussion

Already have an account? Log in

  1. EmmaCodesreplies: 1

    Floats are stored in binary, and 0.1 has no exact binary representation (like 1/3 in decimal). Tiny errors appear. For most science and engineering it doesn't matter; you just format the output. For money, use decimal or integer cents.

    5
    1. LucasVDOP

      Like 1/3 = 0.333... in decimal. That analogy clicked.

      2
      1. HenryWrites

        This happens in every language, not just Python. Try it in JavaScript.

        5