On Sat, Dec 28, 2019 at 09:20:49PM -0800, Brendan Barnwell wrote:

> The things that 
> computers work with are floats, and NaN is a float, so in any relevant 
> sense it is a number; it is an instance of a numerical type.

You seem to be assuming that `x is an instance of type float (or 
Decimal)` implies `x is a number`. That seems reasonable in the general 
case (e.g. we would rightly expect that every instance of a type "Car" 
represents an actual car) but in the specific case of floats (and 
Decimal) the assumption breaks down.

The writers of the IEEE-754 standard didn't choose the name "Not A 
Number" at random. The name tells you their intention: NANs represent 
values or quantities which are not numbers. It seems particularly 
perverse to insist that even when a value is explicitly described as Not 
A Number, it's actually a number.

Especially since it fails quite a few commonsense tests for whether or 
not something is a number:

- Do NANs appear anywhere on the real number line or complex plane?

- Can we successfully measure (say) the length of a piece of string 
  and get a result of NAN inches?

- Can we successfully count the number of (say) spoons on a table 
  and get a result of NAN?

- Do NANs obey, even approximately, the usual properties of numbers?

The answer in all four cases is No. If something doesn't quack like a 
duck, doesn't swim like a duck, and doesn't walk like a duck, and is 
explicitly called Not A Duck, would we insist that it's actually a duck?

One of the earliest computer companies to implement the IEEE-754 was 
Apple, in their "Standard Apple Numerics Environment" or SANE. The SANE 
manual states:

    "When a SANE operation cannot produce a meaningful result, the
    operation delivers a special bit pattern called a NaN 
    (Not-a-Number)."

So NANs are placeholders applying where there is no meaningful result. 
One of the operations where SANE would return a NAN result was when 
trying to convert a string to a number. In Python terms, the SANE 
equivalent of:

    float("Hello World")

would return a NAN. 

David Goldberg's "What Every Computer Scientist Should Know About 
Floating-Point Arithmetic" contrasts two different styles of numeric 
programming:

(1) One where every bit-pattern is a number (example: IBM System/370)

(2) One where some bit-patterns don't represent numbers, but represent 
"special quantities such as INDEFINITE and INFINITY" (e.g. the CDC 6600 
and the VAX).

Goldberg says:

    "The IEEE standard continues in this tradition and has NaNs (Not a 
    Number) and infinities. Without any special quantities, there is
    no good way to handle exceptional situations like taking the square 
    root of a negative number, other than aborting computation."

https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

so it should be clear that Goldberg considers IEEE-754 to belong to (2) 
where some bit-patterns are special quantities, not numbers. He then 
goes on to contrast the situation on System/370:

    "Since every bit pattern represents a valid number [on System/370], 
    the return value of square root must be some floating-point number. 
    In the case of System/370 FORTRAN, √(−4)=2 is returned.

with the situation in IEEE-754 arithmetic which has "special value[s] 
called NaN":

    "In IEEE arithmetic, a NaN is returned in this situation."


I think that in order to be precise we need to distinguish between at 
least four cases:

- instances of the abstract type Number;
- instances of concrete types such as float or Decimal;
- and instances whose value represents a number.

(Aside: recall that in Python, the object model is that every object has 
a type, an identity and a value.)

NANs, it is true, are instances of both the abstract type Number and the 
concrete type float or Decimal; but their values don't represent 
numbers.



-- 
Steven
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/NUU45A6I5VI6ZB6T4ULXVX6ZUBJ5IMNU/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to