Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Python compare 'int' to 'float' objects?

The documentation about numeric types states that:

Python fully supports mixed arithmetic: when a binary arithmetic operator has operands of different numeric types, the operand with the “narrower” type is widened to that of the other, where integer is narrower than floating point, which is narrower than complex. Comparisons between numbers of mixed type use the same rule.

This is supported by the following behavior:

>>> int.__eq__(1, 1.0)
NotImplemented
>>> float.__eq__(1.0, 1)
True

However for large integer numbers something else seems to happen since they won't compare equal unless explicitly converted to float:

>>> n = 3**64
>>> float(n) == n
False
>>> float(n) == float(n)
True

On the other hand, for powers of 2, this doesn't seem to be a problem:

>>> n = 2**512
>>> float(n) == n
True

Since the documentation implies that int is "widened" (I assume converted / cast?) to float I'd expect float(n) == n and float(n) == float(n) to be similar but the above example with n = 3**64 suggests differently. So what rules does Python use to compare int to float (or mixed numeric types in general)?


Tested with CPython 3.7.3 from Anaconda and PyPy 7.3.0 (Python 3.6.9).

like image 397
a_guest Avatar asked Jan 31 '20 14:01

a_guest


People also ask

Can we compare float and int in Python?

You shouldn't, in general, compare floats for equality without some care, but comparing floats for relativity (> or <) is perfectly fine. Your update is due to precision errors - they are a fact of computing, and shouldn't matter to you in 99.9% of cases as they difference is too small to care about.

How does integer compare to float?

Integers and floats are two different kinds of numerical data. An integer (more commonly called an int) is a number without a decimal point. A float is a floating-point number, which means it is a number that has a decimal place. Floats are used when more precision is needed.

How does Python compare float?

How To Compare Floats in Python. If abs(a - b) is smaller than some percentage of the larger of a or b , then a is considered sufficiently close to b to be "equal" to b . This percentage is called the relative tolerance. You can specify the relative tolerance with the rel_tol keyword argument of math.

Can we use == to compare two float values in Python?

In the case of floating-point numbers, the relational operator (==) does not produce correct output, this is due to the internal precision errors in rounding up floating-point numbers. In the above example, we can see the inaccuracy in comparing two floating-point numbers using “==” operator.


1 Answers

The language specification on value comparisons contains the following paragraph:

Numbers of built-in numeric types (Numeric Types — int, float, complex) and of the standard library types fractions.Fraction and decimal.Decimal can be compared within and across their types, with the restriction that complex numbers do not support order comparison. Within the limits of the types involved, they compare mathematically (algorithmically) correct without loss of precision.

This means when two numeric types are compared, the actual (mathematical) numbers that are represented by these objects are compared. For example the numeral 16677181699666569.0 (which is 3**34) represents the number 16677181699666569 and even though in "float-space" there is no difference between this number and 16677181699666568.0 (3**34 - 1) they do represent different numbers. Due to limited floating point precision, on a 64-bit architecture, the value float(3**34) will be stored as 16677181699666568 and hence it represents a different number than the integer numeral 16677181699666569. For that reason we have float(3**34) != 3**34 which performs a comparison without loss of precision.

This property is important in order to guarantee transitivity of the equivalence relation of numeric types. If int to float comparison would give similar results as if the int object would be converted to a float object then the transitive relation would be invalidated:

>>> class Float(float):
...     def __eq__(self, other):
...         return super().__eq__(float(other))
... 
>>> a = 3**34 - 1
>>> b = Float(3**34)
>>> c = 3**34
>>> a == b
True
>>> b == c
True
>>> a == c  # transitivity demands that this holds true
False

The float.__eq__ implementation on the other hand, which considers the represented mathematical numbers, doesn't infringe that requirement:

>>> a = 3**34 - 1
>>> b = float(3**34)
>>> c = 3**34
>>> a == b
True
>>> b == c
False
>>> a == c
False

As a result of missing transitivity the order of the following list won't be changed by sorting (since all consecutive numbers appear to be equal):

>>> class Float(float):
...     def __lt__(self, other):
...         return super().__lt__(float(other))
...     def __eq__(self, other):
...         return super().__eq__(float(other))
... 
>>> numbers = [3**34, Float(3**34), 3**34 - 1]
>>> sorted(numbers) == numbers
True

Using float on the other hand, the order is reversed:

>>> numbers = [3**34, float(3**34), 3**34 - 1]
>>> sorted(numbers) == numbers[::-1]
True
like image 82
a_guest Avatar answered Sep 23 '22 19:09

a_guest