where in Nan defined

Chris Angelico rosuav at gmail.com
Thu Jan 8 21:07:01 EST 2015


On Fri, Jan 9, 2015 at 12:46 PM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> Chris Angelico wrote:
>
>> On Fri, Jan 9, 2015 at 1:50 AM, Jussi Piitulainen
>> <jpiitula at ling.helsinki.fi> wrote:
>>>    >>> 0*1e400
>>>    nan
>>
>> Nice, that's shorter than mine.
>
> I'm not entirely sure, but I suspect that 0*1e400 may not be quite as
> portable as your inf-inf. By the rules of IEEE-754 arithmetic, inf-inf has
> to return a NAN, but if your floats have sufficient precision available to
> represent 1e400, 0*1e400 might return 0.
>
> The fallback rule I use when float('nan') fails is
>
>     INF = 1e3000  # Hopefully, this should overflow to INF.
>     NAN = INF-INF  # And this hopefully will give a NaN.

The first question is "will 1eN overflow to inf?". I'm fairly sure
Python guarantees that an unrepresentable float constant will be
treated as infinity, so all you have to do is crank up the exponent
any time you suspect it'll be representable. The second is "will 0*inf
result in nan?", and I would expect that to be as reliable as INF-INF.

ChrisA



More information about the Python-list mailing list