Why `divmod(float('inf'), 1) == (float('nan'), float('nan'))`

Ian Kelly ian.g.kelly at gmail.com
Wed Sep 17 12:30:23 EDT 2014


On Wed, Sep 17, 2014 at 9:40 AM, Chris Angelico <rosuav at gmail.com> wrote:
> On Thu, Sep 18, 2014 at 1:34 AM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
>> On Wed, Sep 17, 2014 at 9:29 AM, Chris Angelico <rosuav at gmail.com> wrote:
>>> On Thu, Sep 18, 2014 at 1:16 AM, cool-RR <ram.rachum at gmail.com> wrote:
>>>> I didn't ask for the modulo, I agree it should remain NaN. I'm talking about the floor division.
>>>>
>>>
>>> Invariant: div*y + mod == x.
>>>
>>> If mod is NaN, there is no possible value for div that will make the
>>> invariant true, ergo it too has to be NaN.
>>
>> That still doesn't make the invariant true. By this argument div could
>> be 42, and the invariant would hold equally well (i.e. not at all).
>
> Nothing can possibly make it true, so there are only two
> possibilities: return NaN, or raise an exception.

Actually, I think we're focusing on the wrong element of the tuple.
The fact is that there are values that can make the invariant true.
For example, divmod(inf, 1) could return (inf, 0).  inf * 1 + 0 = inf.
(inf, 1), or (inf, <any finite value>) would work equally well.  The
*only* reason the invariant can't be maintained is because we're
committed to returning nan for the second element for some reason.



More information about the Python-list mailing list