[Python-ideas] Suggestion: Extend integers to include iNaN
Steven D'Aprano
steve at pearwood.info
Sat Sep 29 09:00:08 EDT 2018
On Sat, Sep 29, 2018 at 06:31:46AM +0000, Steve Barnes wrote:
> One of the strengths of the IEEE float, (to set against its many
> weaknesses),
o_O
Since I'm old enough to (just barely) remember the chaos and horror of
numeric programming before IEEE-754, I find that comment rather
shocking.
I'm curious what you think those weaknesses are.
> is the presence of the magic value NaN. Not a Number, or
> NaA, is especially useful in that it is a valid value in any
> mathematical operation, (always returning NaN), or comparison, (always
> returning False).
Almost so. But the exceptions don't matter for this discussion.
> In functional programming this is especially useful as
> it allows the chain to complete after an error while retaining the fact
> that an error occurred, (as we got NaN).
Not just functional programming.
[...]
> I think that it should be relatively simple to extend the Python integer
> class to have a NaN flag, possibly by having a bit length of 0, and have
> it follow the same rules for the handling of floating point NaN, i.e.
> any mathematical operation on an iNaN returns an iNaN and any comparison
> with one returns False.
Alas, a bit length of 0 is zero:
py> (0).bit_length()
0
I too have often wished that integers would include three special
values, namely plus and minus infinity and a NAN. On the other hand,
that would add some complexity to the type, and make them harder to
learn and use. Perhaps it would be better to subclass int and put the
special values in the subclass.
A subclass could be written in Python, and would act as a good proof of
concept, demonstrating the behaviour of iNAN. For example, what would
you expect iNAN & 1 to return?
Back in the 1990s, Apple Computers introduced their implementation of
IEEE-754, called "SANE" (Standard Apple Numeric Environment). It
included a 64-bit integer format "comp", which included a single NAN
value (but no infinities), so there is definately prior art to having an
integer iNAN value.
Likewise, R includes a special NA value, distinct from IEEE-754 NANs,
which we could think of as something very vaguely like an integer NAN.
https://stat.ethz.ch/R-manual/R-devel/library/base/html/NA.html
--
Steve
More information about the Python-ideas
mailing list