0

I have read that python avoids the loss of data in Implicit Type Conversion.

Isn't below code converts float literal to integer and then compare it through list? Isn't it loss of data value?

list1 = [10, 20, 30]
print(30.0000000000000000000000000000000001 in list1)

Above code prints true

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    It's not converting float to integer. The problem is that float doesn't have that much precision, so the fraction is lost. – Barmar Aug 12 '23 at 05:32
  • 1
    Try just `print(30.0000000000000000000000000000000001)` and you'll see that it prints `30.0` because the `1` is rounded off. – Barmar Aug 12 '23 at 05:32
  • 1
    Python doesn't have any implicit type conversions. But when you compare an int to a float, they'll compare equal if the float has a zero fraction. – Barmar Aug 12 '23 at 05:33
  • There is data lost, it happens when the float is parsed. They have about 15 decimal digits of precision. – Barmar Aug 12 '23 at 05:34
  • "I have read that python avoids the loss of data in Implicit Type Conversion." - no; it avoids *doing those conversions*, so there won't be a loss of data. But it is still possible to compare an integer to a float, as you can see (there is no error raised). " Isn't it loss of data value?" No, because the information was already not there. The `float` type cannot tell the difference between `30.0` and `30.0000000000000000000000000000000001`. Please see the linked duplicate for details. – Karl Knechtel Aug 12 '23 at 05:36

0 Answers0