classification
Title: math.sqrt function wrong
Type: performance Stage: resolved
Components: Windows Versions: Python 3.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Mick Press, paul.moore, steve.dower, tim.golden, vstinner, zach.ware
Priority: normal Keywords:

Created on 2017-08-09 09:54 by Mick Press, last changed 2017-08-09 09:58 by vstinner. This issue is now closed.

Files
File name Uploaded Description Edit
PyCuda - [C__Users_mick_PyScripts_PyCuda] - C__Users_mick_.PyCharmCE2017.2_config_scratches_scratch_5.py - PyCharm Community Edition 2017.2 09_08_2017 10_44_31.png Mick Press, 2017-08-09 09:54 Screen shot
Messages (2)
msg299983 - (view) Author: Mick Press (Mick Press) Date: 2017-08-09 09:54
math.sqrt for very large numbers returns wrong value. Attached is screen shot showing my sqrt function result and math.sqrt function result.
msg299984 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-08-09 09:58
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
History
Date User Action Args
2017-08-09 09:58:42vstinnersetstatus: open -> closed

nosy: + vstinner
messages: + msg299984

resolution: not a bug
stage: resolved
2017-08-09 09:54:19Mick Presscreate