Home >Backend Development >Python Tutorial >How to Mitigate Floating Point Errors in Python?
Avoiding Floating Point Errors in Python
In the realm of programming, it's essential to understand the intricacies of floating point calculations, as they can introduce unexpected errors if not handled properly. This article explores a practical example that highlights the pitfalls of floating point arithmetic.
The Square Root Problem
Consider a Python function designed to approximate square roots:
<code class="python">def sqrt(num): root = 0.0 while root * root < num: root += 0.01 return root
Using this function, we encounter surprising results:
>>> sqrt(4) 2.0000000000000013 >>> sqrt(9) 3.00999999999998</code>
Floating point arithmetic explains these inaccuracies. When representing fractional numbers, computers store them as a combination of integer and exponent. Due to limitations in this representation, certain decimal values cannot be represented exactly, leading to approximations.
Understanding the Error
In the code above, the issue lies in the increment used to increase the root value. While we intend to add a value of 0.01, the actual value stored in the floating point register is slightly different and greater than 0.01.
Addressing the Error
To avoid floating point errors, various approaches can be employed:
<code class="python">from decimal import Decimal as D def sqrt(num): root = D(0) while root * root < num: root += D("0.01") return root
Now, the function returns precise results, such as: ``` >>> sqrt(4) Decimal('2.00') >>> sqrt(9) Decimal('3.00') ``` </code>
The above is the detailed content of How to Mitigate Floating Point Errors in Python?. For more information, please follow other related articles on the PHP Chinese website!