When is divmod(a,b)[0] == floor(a/b)-1 ?

kj no.email at please.post
Thu Sep 24 23:15:22 CEST 2009


In <mailman.429.1253825725.2807.python-list at python.org> Robert Kern <robert.kern at gmail.com> writes:

>On 2009-09-24 14:40 PM, kj wrote:
>>
>> The docs for divmod include the following:
>>
>>      divmod(a, b)
>>      ...For floating point numbers the result is (q, a % b), where q
>>      is usually math.floor(a / b) but may be 1 less than that. ...
>>
>> I know that floating point math can sometimes produce "unexpected"
>> results, so the above caveat is not entirely surprising.  Still,
>> I would find it helpful to see a specific example where
>> divmod(a, b)[0] is equal to math.floor(a/b)-1.  Does anybody know
>> one?

>In [21]: a = 10.0

>In [22]: b = 10.0 / 3.0

>In [24]: divmod(a, b)[0]
>Out[24]: 2.0

>In [25]: math.floor(a / b) - 1.0
>Out[25]: 2.0

Wow.  To me this stuff is just black magic, with a bit of voodoo
added for good measure...  Maybe some day I'll understand it.

Thanks!

kynn



More information about the Python-list mailing list