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

Chris Angelico rosuav at gmail.com
Wed Sep 17 18:37:50 CEST 2014


On Thu, Sep 18, 2014 at 2:33 AM, cool-RR <ram.rachum at gmail.com> wrote:
> On Wednesday, September 17, 2014 6:30:04 PM UTC+3, Chris Angelico 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.
>> ChrisA
>
> Chris, why is this invariant `div*y + mod == x` so important? Maybe it's more important to return a mathematically reasonable result for the the floor-division result than to maintain this invariant?

It's important because it's documented :) You can expect whatever you
like, but if something's a documented invariant, that's more important
than your expectations.

Now, why was divmod documented as returning values adhering to that
invariant? You'd have to ask the developers, I've no idea.

ChrisA



More information about the Python-list mailing list