int(a), b = divmod(x, y)

Tim Peters at
Fri Jul 27 18:39:43 EDT 2001

[Stephen Horne]
> I don't know why the first output in the tuple isn't an int anyway.
> After all, it *is* a member of the set of integers even if it is
> currently represented using a float.
> A quick example...
> >>> divmod(5.1,2.0)
> (2.0, 1.0999999999999996)
> So why doesn't this return...
> >>> divmod(5.1,2.0)
> (2, 1.0999999999999996)
> If the 2 is used anywhere a float is needed it will be cast to float
> implicitly anyway once PEP238 goes through.

float divmod() always returns floats today; that's all there is to it.  It
can't always return (short) ints, because e.g.,

>>> divmod(1e98, 2)
(5e+97, 0.0)

That is, 5e97 is outside the range of Python ints on any platform today.

It could always return a long (unbounded) int for the quotient, but that
would be time- and space-inefficient, and it's hard to think of a case where
that be more useful than a float.

More information about the Python-list mailing list