[comp.lang.c] Calculation returning NaN

fineberg@caip.rutgers.edu (Adam B. Fineberg) (01/29/91)

I was wondering if someone could point me to a source of information
about the math error NaN.  I understand that this means not a number
but I have been unable to find a reference which explains what causes
this message.  I have a program which uses floating point math
(actually complex math) and several of my results come out as NaN.  I
tried to isolate the problem but when I remove the small section of
code which causes the NaN I get numbers back.  Unfortunately my
program runs for several days and performs about 64 billion
calculations before I get this result.  I have not been succesful in
tracking it down and would appreciate any reference which describes
the conditions which cause NaN since my program ends without errors
and without floating point exceptions.  Thanks in advance for any
help.

Adam B. Fineberg

***********************************************************************
*                                                                     *
* Adam Fineberg                       Center for Computer Aids for    *
* (201) 932-0080 days                 Industrial Productivity (CAIP)  *
* (201) 359-7940 nites                Rutgers University              *
* fineberg@caip.rutgers.edu           Piscataway, New Jersey          *
* fineberg@enterprise.rutgers.edu     08855-1390                      *
*                                                                     *
***********************************************************************
*                                                                     *
* All is not lost, It's just a little hard to keep track of...        *
*                                                                     *
***********************************************************************

jap@convex.cl.msu.edu (Joe Porkka) (01/29/91)

fineberg@caip.rutgers.edu (Adam B. Fineberg) writes:


>I was wondering if someone could point me to a source of information
>about the math error NaN.  I understand that this means not a number

I've got to motorola handbook right here....
(If you want the manual, call 1-800-555-1212 to ask for morola number, call
morola and ask for m68000 programmers reference manual. I thinks its <$10)

Well, if a=Nan, and b= anynumber, then most things like
  a<b, a*b, etc... return NaN.

  On page 1-37 theres a nifty table for the FADD instruction

	source	 in range	| zero		| infinity
		+       - 	  +	   -	  +     -
=====================================================================
destination
	+
    in range	add		add		 +inf  -inf
	-

	+			+0.0	+-0.0
    zero	add				  +inf	-inf
	-			+-0.0   -0.0

	+	+inf		   +inf		  +inf	NaN
    infin.
	-	-inf		   -inf		   NaN   -inf



So if a=-inf and b=+inf, result = NaN
	+inf       +inf  result = +inf


Also, things like 4.0/0.0 == NaN

steve@taumet.com (Stephen Clamage) (01/30/91)

fineberg@caip.rutgers.edu (Adam B. Fineberg) writes:

>I was wondering if someone could point me to a source of information
>about the math error NaN (Not-a-Number).

This is a feature of IEEE floating-point, and can be found in
ANSI/IEEE Std 754-1985, "IEEE Standard for Binary Floating-Point Arithmetic".

In addition, the manuals for floating-point chips which implement IEEE
arithmetic (such as Motorola 68881/2 and Intel 80x87) will contain a
discussion of NaN. 

Briefly, an operation with an undefined result produces a NaN.  The result
of 0/0 or Infinity*0 is not defined, for example.  Almost any operation
involving a NaN, including comparison, results in a NaN, so that nonsense
does not later turn into something which looks reasonable.  The NaN result
lets you know that something went wrong earlier in the calculation.

NaN's are also used as the starting value of uninitialized variables.
-- 

Steve Clamage, TauMetric Corp, steve@taumet.com

gram@uctcs.uucp (Graham Wheeler) (02/01/91)

Some C compilers have a special representation for infinity. This can lead
to results being shown as `Inf'. `NaN' (Not a Number) usually occurs as the
result of attempting undefined calculations on infinite quantities, such as
attempting to divide infinity by infinity, subtraction of Inf from Inf, and
even multiplying Inf by zero. Obviously, as NaN is a result, not an error
message, there is a representation for it as well.

Check your compiler documentation, particularly what data representations
are being used, to find out the precise conditions under which this occurs.
Graham Wheeler		      |	"Don't bother me, I'm reading a `Crisis'!"
Data Network Architectures Lab| Internet: <gram@uctcs.uct.ac.za>
Dept. of Computer Science     | Fidonet: <gram.uctcs@f4.n494.z5.fidonet.org>
University of Cape Town       |     BANG: <...uunet!ddsw1!olsa99!uctcs!gram>