0

for the code below, why is the first result a small decimal number but the second results in 0? My main question is why that extra decimal place makes the second result a 0. Does python have a limit in its decimal places when calculating floats?

x =5.000000000000001
y =5
print(x-y)
x =5.0000000000000001
y =5
print(x-y)
snakecharmerb
  • 47,570
  • 11
  • 100
  • 153
55gus
  • 1
  • 1

1 Answers1

0

Float is not always precise because of how the data is stored. They need to be rounded to work properly in practice.

x =5.000000000000001
y =5
print(round(x-y))
x =5.0000000000000001
y =5
print(x-y)
Matthew Gaiser
  • 4,558
  • 1
  • 18
  • 35