Message299984
The math module uses the float type which has a limited precision: it's 64-bit IEEE.
For better precision, you can use the decimal module which has configurable precision.
Example:
---
import decimal
decimal.getcontext().prec = 5
root = decimal.Decimal('123').sqrt()
print(root)
decimal.getcontext().prec = 50
root = decimal.Decimal('123').sqrt()
print(root)
---
Output:
---
11.091
11.090536506409417162051600102609932918463376742454
---
Said differently: the behaviour that you noticed is not a bug, but a known limitation of Python, of the Python float type to be exact.
https://docs.python.org/3/tutorial/floatingpoint.html |
|
Date |
User |
Action |
Args |
2017-08-09 09:58:42 | vstinner | set | recipients:
+ vstinner, paul.moore, tim.golden, zach.ware, steve.dower, Mick Press |
2017-08-09 09:58:42 | vstinner | set | messageid: <1502272722.9.0.472366433672.issue31157@psf.upfronthosting.co.za> |
2017-08-09 09:58:42 | vstinner | link | issue31157 messages |
2017-08-09 09:58:42 | vstinner | create | |
|