Search code examples
pythonfloating-pointprecision

How do calculators handle floating point errors?


I am writing a calculator interface in Python using Tkinter. I am aware of the floating point precision issues that can arise in computers, but I have never had to deal with them directly.

In my calculator for example, I am multiplying two floats together, say 3.2 and 3.0. Instead of resulting in 9.6, however, I get 9.60000000000001 (or something). Normally, I would just round this number. However, under circumstances where I would need that extra precision, I wish to keep it. What is the standard practice for dealing with this issue?


Solution

  • However, under circumstances where I would need that extra precision, I wish to keep it.

    Your precision needs exceed what is readily available with standard binary floating point.

    Use decimal or use higher precision binary math to reduce the occurrence of the perceived imprecision.

    What is the standard practice for dealing with this issue?

    Common calculators always use more internal precision (decimal or not) than they show to avoid such floating point artifacts.

    I recommend internal math that is at least 2-3 decimal places (8-10 bits) more than the significant maximum number of digits displayed.