I learnt of the "exactly equal to" operator in Erlang, which compares not only values, but also data types of numbers, and I was curious about how things work in Python and its lone "equals to" operator. So after making sure that
>>> 1 == 1.0
True
I wondered about the floating point precision, and got to this
>>> 0.9999999999999999 == 1
False
>>> 0.99999999999999999 == 1
True
>>>
Could someone explain how floating point precision is determined here? It works the same in both 2.7.1 and 3.1.2
In python float precision to 2 floats in python, and python float precision to 3. There are many ways to set the precision of the floating-point values.
According to this standard, floating point numbers are represented with 32 bits (single precision) or 64 bits (double precision).
Python can handle the precision of floating point numbers using different functions. Most functions for precision handling are defined in the math module. So to use them, at first we have to import the math module, into the current namespace. import math. Now we will see some of the functions for precision handling.
Python's built-in float type has double precision (it's a C double in CPython, a Java double in Jython). If you need more precision, get NumPy and use its numpy.
Please check with the Python documentation:
http://docs.python.org/tutorial/floatingpoint.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With