This has nothing to do with Python - you will see the same behavior in any language using your hardware binary floating point arithmetic. Read the docs first.
After you read this, you will realize that you are not adding one hundredths to your code. This is exactly what you add:
>>> from decimal import Decimal >>> Decimal(.01) Decimal('0.01000000000000000020816681711721685132943093776702880859375')
This line shows the exact decimal value of the binary floating ("double precision" in C) approximation to the exact decimal value of 0.01. What you really add is a little more than 1/100.
Floating point numerical error management is a field called numerical analysis and is a very large and complex topic. As long as you are frightened by the fact that floats just come close to decimal values, use the decimal module. This will remove the world of "minor problems" for you. For example, given this small modification to your function:
from decimal import Decimal as D def sqrt(num): root = D(0) while root * root < num: root += D("0.01") return root
then
>>> sqrt(4) Decimal('2.00') >>> sqrt(9) Decimal('3.00')
This is not entirely accurate, but may be less unexpected in simple examples, because now it adds exactly one hundredth. A.
An alternative is to stick with a float and add something exactly represented as a binary float: values โโof the form I/2**J For example, instead of adding 0.01, add 0.125 (1/8) or 0.0625 (1/16).
Then find the โNewton methodโ to calculate the square roots; -)
Tim peters
source share