-1

why print(2 == 2.) returns True

As per my understanding 2 is integer and 2.0 is float. They both are stored differently in memory.

I think it is returning True because they both belong to same class that is integer class?

I have gone through similar stack overflow question but concept is still not clear to me. Can someone please explain?

Rima
  • 1,447
  • 1
  • 6
  • 12
  • Because it’s explicitly supported: https://docs.python.org/3/library/stdtypes.html#typesnumeric – deceze Jun 17 '21 at 16:22

2 Answers2

1

Memory representation doesn't really have anything to do with it (or at least, it doesn't prevent certain integers from comparing equal to equivalent real numbers).

2 == 2. is implemented by int.__eq__(2, 2.), which is defined to handle integer/float comparisons by making appropriate type conversions. Since int is a "narrower" type than float, 2 is first converted to a floating point value. (As an implementation detail, I suspect this is done by int.__eq__ calling float.__eq__(2., 2) and the conversion is done there, rather than the int type knowing any particular details about how floating-point values are represented.)

Note that not every int can be represent exactly as a floating-point value, as int uses arbitrary precision but float is fixed. For example, on my machine

>>> 2000000000000000000000000000000000 == 2000000000000000000000000000000000.
False

(I copy-pasted the integer to produce the floating-point literal, so the 0s line up.)

chepner
  • 497,756
  • 71
  • 530
  • 681
0

This depends on how python implements the equality operator between float and int. I would imagine that in this case the int is cast to float before performing the comparison under the hood.

sev
  • 1,500
  • 17
  • 45
  • 1
    It's not implementation-dependent, it's specified by the language. See the linked question. – Barmar Jun 17 '21 at 16:22
  • 1
    It's implementation-dependent in the sense that Python doesn't specify any particular implemantion of floating-point values; it leaves it up to the machine's native type. – chepner Jun 17 '21 at 16:29
  • @chepner: [The Python 3 documentation](https://docs.python.org/3/library/stdtypes.html#typesnumeric) says “A comparison between numbers of different types behaves as though the exact values of those numbers were being compared.” So the floating-point type is irrelevant; an integer number and a floating-point number will compare equal if and only if they represent the same value, regardless of what values either type can represent. (It is possible that the floating-point – Eric Postpischil Jun 17 '21 at 16:51
  • 1
    How is the floating-point type irrelevant to the set of values it can represent? – chepner Jun 17 '21 at 17:08
  • The problem is that not every floating-point *literal* produces a unique floating-point *value*. Consider something like `2000000000000000000000000000001. == 2000000000000000000000000000002.`. You could get `True` or `False`, depending on how much precision your underlying floating-point representation has available to store the mantissa. – chepner Jun 17 '21 at 17:13
  • @chepner: The floating-point type is irrelevant to the result the `==` operation produces. For numeric operands, `==` evaluates to true if and only if its two operands represent the same value, regardless of what values either type can represent. – Eric Postpischil Jun 17 '21 at 19:50
  • @chepner: Re “The problem is that not every floating-point literal produces a unique floating-point value”: This has nothing to do with how `==` behaves. Conversion of source text to types is a separate step. – Eric Postpischil Jun 17 '21 at 19:51