Vaguely related question about divmod

Tim Peters tim.one at home.com
Tue Jul 24 21:53:51 EDT 2001


[David Eppstein]
> divmod(3.1415926535, 2.718281828) returns (1.0, 0.4233108255) almost as
> I would expect.  But, why is the first return value a float instead
> of an int?

float divmod is defined in terms of float operations; that's all.  A
consequence:

>>> divmod(2.**64, 17)
(1.0851025925711501e+018, 1.0)
>>> divmod(2L**64, 17)
(1085102592571150095L, 1L)
>>>

IOW, while 2.**64 (and 2.**1000 ...) are exactly representable as IEEE
doubles, Python doesn't generate (potentially) multi-hundred digit longs for
float divmod's results.  The audience for float divmod wouldn't appreciate
it if Python did, either <wink>.

BTW, a more useful divmod(x, y) for positive floats would return floats q, r
such that r <= y/2.





More information about the Python-list mailing list