[Python-ideas] Suggestion: Extend integers to include iNaN

Richard Damon Richard at Damon-Family.org
Sat Sep 29 09:11:41 EDT 2018


On 9/29/18 2:31 AM, Steve Barnes wrote:
> One of the strengths of the IEEE float, (to set against its many 
> weaknesses), 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). 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).
>
> In languages such as C integers can only be used to represent a limited 
> range of values in integers and a less limited range of values, (but 
> still limited), with a limited accuracy. However, one of Pythons 
> strengths is that its integers can represent any whole number value, (up 
> to the maximum available memory and in exchange for slow performance 
> when numbers get huge). This is accomplished by Python Integers being 
> objects rather than a fixed number of bytes.
>
> 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.
>
> One specific use case that springs to mind would be for Libraries such 
> as Pandas to return iNaN for entries that are not numbers in a column 
> that it has been told to treat as integers.
>
> We would possibly need a flag to set this behaviour, rather than raising 
> an Exception, or at the very least automatically (or provide a method 
> to) set LHS integers to iNaN on such an exception.
>
> I thought that I would throw this out to Python Ideas for some 
> discussion of whether such a feature is:
>   a) Desirable?
>   b) Possible, (I am sure that it could be done)?
>   c) Likely to get me kicked off of the list?

I would think that a possibly better solution would be the creation of a
NAN type (similar  to NONE) that implement this sort of property. That
way the feature can be added to integers, rationals, and any other
numeric types that exist (why do just integers need this addition).

-- 
Richard Damon



More information about the Python-ideas mailing list