
Cade Brown writes:
When I read "1e1000" or such a number it doesn't strike me as infinite
As Steven points out, it's an overflow, and IEEE *but not Python* is clear about that. In fact, none of the actual infinities I've tried (1.0 / 0.0 and math.tan(math.pi / 2.0)) result in values of inf. The former raises ZeroDivisionError and the latter gives the finite value 1.633123935319537e+16.
Steven D'Aprano writes:
Nobody is recommending that [as] the best way to create an infinity
Is there a way to create an infinity in base Python, though? You can create infs (with 1e1000 or 2e308 or 1e1000000000 or 2e200 * 2e200), but those aren't infinities, those are overflows. All the examples in this thread, and infs in general Pythonic practice, are expressions which have well-defined mathematical values, but overflow the range of floats, and *inf doesn't tell you which mathematical value*, or indeed even admit that "YMMV".[1]
It's not obvious to me why 1e309 and 9e308 should be equal in Python -- 1e309 - 9e308 is close to the largest number you can express as a Python float! The point is that inf arithmetic only makes sense if carefully analyzed in the context, not just of the application but even down to the specific calculation.
I assume that IEEE did some sort of generic analysis and concluded that mathematical infinities and overflows map to inf, mathematical undefineds map to nan, overflows map to inf, and underflows map to zero. But, as a large set of values, perhaps we'd be better off with overflow treated as nan? Or perhaps overflow should be rounded to sys.float_info.max since all finite values are closer to that than they are to infinity? Or maybe overflow should be a different kind of thing, where some operations produce another overflow (adding a positive finite number, multiplying by a number with magnitude > 1), while others produce nans (overflow - overflow, as with infs, but also overflow - positive_finite). Maybe all nans should raise by default.
Using something like 1e1000 [...] is a fall back
I prefer to think of it as being honest: this isn't infinity, this is overflow -- and the way Python treats infs, here be Dragons, all bets are off, "magic is loose in the world", and anything can happen.
Footnotes: [1] Float 0 also makes little sense for the same reason, but somehow "anything close enough to zero 'is' zero" doesn't bother me as much as "anything a little bit bigger than 1e308 'is' infinite" does. I guess it should ....