0

There is a confusing description of built-in function divmod(),i post below:

If x is very close to an exact integer multiple of y, it’s possible for x//y to be one larger than (x-x%y)//y due to rounding. In such cases, Python returns the latter result, in order to preserve that divmod(x,y)[0] * y + x % y be very close to x.

x//yshould be equal to (x-x%y)//y ,according to the identity x == (x//y)*y.I wonder how can x//y be larger than (x-x%y)//y when rounding occurred.

wow yoo
  • 855
  • 8
  • 26

1 Answers1

0

As a quick "for instance" to explore this corner case, in an Idle shell, I entered: x,y=1.0,0.1

and then: x/y;x//y;x%y;x-x%y;(x-x%y)//y;x//y==(x-x%y)//y.

For most values of x that are multiples of y, the final result is True (e.g., for x == 0.1 thru 0.9), but in the case of x == 1.0, it is False.

x,y=0.9,0.1:

9.0
8.0
0.09999999999999998
0.8
8.0
True

x,y = 1.0,0.1:

10.0
9.0
0.09999999999999995
0.9
8.0
False
Firstrock
  • 931
  • 8
  • 5