From: dseal@armltd.co.uk (David Seal)
Newsgroups: sci.math
Subject: Re: Not-a-Number
Date: 23 Dec 92 13:16:12 GMT
Distribution: sci
Organization: A.R.M. Ltd, Swaffham Bulbeck, Cambs, UK

In article <1428@kepler1.rentec.com> andrew@rentec.com (Andrew Mullhaupt)
writes:

>In article <BzGwEC.ALr@unx.sas.com> sasbmt@jjoyce.unx.sas.com (Bruce Tindall) writes:
>>
>>In article <7ZJ0uB1w165w@west.darkside.com>, susskind@west.darkside.com (Mark Susskind) writes:
>>>On to the main purpose.  I believe that it would be pointless to assign
>>>Not-a-Number to a variable because one cannot really do any algebra with
>>>it.  For a quick example, let x=NaN.  What would be x+1?  What about 2x?
>>>And 0x would be every real (or complex) number, would it not?
>
>Actually, there are _conceivable_ uses of NaN assignment...

See below for an actual use of NaNs...

>>Certain software systems, such as the SAS system, recognize several
>>different "not a number" values (which SAS calls "missing values").  
>>Usually, any arithmetic operation on a missing value yields a missing 
>>value.  There are some circumstances in which missing values are 
>>treated as zero instead. 
>
>Which in particular shows that the sensible treatment of missing values
>is different than the sensible treatment of non-numbers.
>
>> In sorting and comparisons, missing values 
>>sort as less than the "most negative" number (i.e., less than the 
>>negative number with the greatest absolute value).
>
>This is also different than not a number.

The IEEE standard allows for two types of NaN, namely "quiet" NaNs and
"signalling" NaNs. Arithmetic operations on a quiet NaN simply produce the
NaN itself as the result. Arithmetic operations on a signalling NaN raise an
"invalid operation" exception. The exception can be trapped; if not, by
default the result is a quiet NaN. If it *is* trapped, a well-designed trap
handler can find out what the operation and operands were, then produce
results by its own rules. (I say "well-designed" because the standard
doesn't actually require that this is possible: it is intended to be the
case, but the standard allows you to produce a system which has been cut
down in this respect.)

Now for the application: you can use signalling NaNs to represent such
missing values. You simply write and install a trap handler that implements
whatever arithmetic rules you want to apply to them (e.g. producing another
"missing value" by returning a suitable signalling NaN as the result of the
operation). Then enable the "invalid operation" trap and arithmetic on
missing values works the way you want it to. Similarly, a comparison of a
signalling NaN and a number will enter the trap handler: you can then make
the result come out as "less than" if you want.

Provisos: Many systems don't implement this area of the IEEE standard fully,
or require performance losses to get full compliance. Also, obtaining the
result of an operation by executing a trap handler is obviously much slower
than just using an instruction or two. In short, these facilities don't come
for free!

>It would be nice to have a standard arithmetic which dealt with unspecified
>values, but none exists to my knowledge.

IEEE arithmetic does at least give you the building blocks for such an
arithmetic. Admittedly, something that did not depend on the use of trap
handlers would be better - i.e. something that handled missing values in
hardware. But there are many different extensions to ordinary arithmetic
that people might want, and specialised hardware would be useless for all
but the one it actually implements. In short, there's a trade-off between
speed and flexibility here...

David Seal
dseal@armltd.co.uk

All opinions are mine only...
Newsgroups: sci.math