2

Why so?

>>> max(2, 2.01)
2.0099999999999998
vaultah
  • 44,105
  • 12
  • 114
  • 143
Jibin
  • 3,054
  • 7
  • 36
  • 51

4 Answers4

5

The number 2.01 represented in binary is:

b10.00000010100011111100001010001111110000101000111111000010100011111100...

The computer uses only a finite number of digits to store floating-point values, but the binary representation of 2.01 requires infinitely many digits; as a result, it is rounded to the closest representable value:

b10.000000101000111111000010100011111100001010001111110

Expressed in decimal, this number is exactly:

2.0099999999999997868371792719699442386627197265625

When you print it out, it is rounded a second time to seventeen decimal digits, giving:

2.0099999999999998
Stephen Canon
  • 103,815
  • 19
  • 183
  • 269
3

Floating point numbers do not encode exact values, but approximations. The result is essentially the next nearest floating point number to the real number you entered.

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

colechristensen
  • 475
  • 1
  • 3
  • 11
Ocaso Protal
  • 19,362
  • 8
  • 76
  • 83
2

because:

>>> 2.01
2.0099999999999998

it's the way floating point numbers are stored

dting
  • 38,604
  • 10
  • 95
  • 114
0

Floating point roundoff. Its trying to say 2.01 but can't express it exactly as a floating point number so its doing the best it can.

verstapp
  • 23
  • 6