Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What defines floating point precision in python?

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

like image 474
dariopy Avatar asked Mar 02 '11 08:03

dariopy


People also ask

What is the floating point precision in Python?

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.

What is the precision value of floating point?

According to this standard, floating point numbers are represented with 32 bits (single precision) or 64 bits (double precision).

What is precision in Python?

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.

Is Python float single or double precision?

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.


1 Answers

Please check with the Python documentation:

http://docs.python.org/tutorial/floatingpoint.html

like image 151
Andreas Jung Avatar answered Sep 22 '22 02:09

Andreas Jung