[net.lang.c] Floating point implementations

serge@pacific.UUCP (serge) (01/09/86)

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?  I am especially interested in how 68000-based 
systems behave, but would also be interested in knowing how other Unix 
systems deal with it.

		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'?

	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.

	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?

	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))

Please respond by E-mail and I will post a summary if there is enough
interest.  I apologize if this has all been discussed before (if it
has, would someone please let me know what, if anything, was decided). 

	Thanks!

	Serge Issakow
-- 

	sdcsvax  \
	          >!pacific!serge
	s3sun    /

lee@haddock.UUCP (01/10/86)

	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.

spaf@gatech.CSNET (Gene Spafford) (01/14/86)

You can obtain a copy of ANSI/IEEE Standard 754-1985, "IEEE Standard
for Binary Floating Point Arithmetic" by sending $6.50 plus $2 handling
to:
	IEEE Service Center
	445 Hoes Lane
	Piscataway, NJ 08854

NJ residents must add 6% sales tax to the $6.50 (= $6.89).  IEEE
members get a 10% discount (on the $6.50, *not* on the $2 handling) if
you include your member number with the order.

Canadian orders can be mailed to:
	IEEE Canadian Regional Office
	7061 Yonge St.
	Thornhill, Ont. L3T 2A6
and the conversion rate is US$ * 1.33 = CDN$

There are quantity discounts for 10 or more copies.  Details can be
directed to +1 201 981 0060.

I have all this information handy because I just ordered a copy....
-- 
Gene "the end is in sight" Spafford
The Clouds Project, School of ICS, Georgia Tech, Atlanta GA 30332-0280
CSNet:	Spaf @ GATech		ARPA:	Spaf%GATech.CSNet @ Relay.CS.NET
uucp:	...!{akgua,decvax,hplabs,ihnp4,linus,seismo,ulysses}!gatech!spaf