Floating point implementations

lee at haddock.UUCP lee at haddock.UUCP
Sat Jan 11 03:46:00 AEST 1986

	Is there some sort of standard in unix or IEEE for how to deal
	with the following floating point issues.  If there is no
	standard, what is the "preferred" action?

Yes there is an IEEE standard.  No there is not a Unix standard, tho there
are Unix IEEE systems and other Unix systems to compare to.

		double a, b, c;
		  [assignment to b and c] 
		c = a / b;

	1) If `a' == 1.0 and `b' == 0.0 , should the divide-by-zero cause 
	   a SIGFPE?  (on some Unix systems it does not).  If a signal 
	   should not be generated, then what value should be
	   assigned to `c'?

If the system is IEEE, the operation should not cause a signal and +INF should
be assigned to c.  All non-IEEE systems I know about generate signals.

	2) If SIGFPE is being ignored or caught (perhaps there are two
	   answers), what value should be assigned to `c'?  I suppose the 
	   possibilities are infinity, a NaN, or some random value since 
	   one could claim that dividing by zero produces an undefined result.

The IEEE spec defines this; I believe it is still +INF but I would have to
look it up.  In a non-IEEE system, usually the hardware defines the result.

	3) If `a' and `b' above both contain infinity, then what should
	   happen (infinity divided by infinity is a Nan)?  Should SIGFPE
	   be generated in this case?  What if it is ignored or caught?

And so on.  The IEEE spec (get 10.0 or later, if there is one) is quite
explicit about the values and signals.  There is assumed to be a capability
to change whether or not actions such as divide-by-zero will cause signals,
but such a facility is highly non-portable and most systems have not been
extended in this way.

	4) If you manage to get the value of infinity or a nan into a
	   a float or double variable, what should printf(3) do with it
	   if you pass this value to it?  I have seen various Unix systems
	   deal with this in one of three ways:
		a) generate a SIGFPE
		b) print a large number (for infinity)
		c) go into an infinite loop (usually in ecvt(3))

The AT&T SystemV code has a special line in the entry to the printf formatter
that looks at the input value and if it is a NaN or INF, it kills itself
with a SIGFPE.  This code is "standard" in the sense that AT&T supports IEEE
machines and that is what they do.

More information about the Comp.lang.c mailing list