xref: /csrg-svn/lib/libm/ieee/cabs.c (revision 61283)
134123Sbostic /*
2*61283Sbostic  * Copyright (c) 1985, 1993
3*61283Sbostic  *	The Regents of the University of California.  All rights reserved.
434123Sbostic  *
542657Sbostic  * %sccs.include.redist.c%
624579Szliu  */
724579Szliu 
824579Szliu #ifndef lint
9*61283Sbostic static char sccsid[] = "@(#)cabs.c	8.1 (Berkeley) 06/04/93";
1034123Sbostic #endif /* not lint */
1124579Szliu 
1224579Szliu /* HYPOT(X,Y)
1324579Szliu  * RETURN THE SQUARE ROOT OF X^2 + Y^2  WHERE Z=X+iY
1424579Szliu  * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS)
1524579Szliu  * CODED IN C BY K.C. NG, 11/28/84;
1624579Szliu  * REVISED BY K.C. NG, 7/12/85.
1724579Szliu  *
1824579Szliu  * Required system supported functions :
1924579Szliu  *	copysign(x,y)
2024579Szliu  *	finite(x)
2124579Szliu  *	scalb(x,N)
2224579Szliu  *	sqrt(x)
2324579Szliu  *
2424579Szliu  * Method :
2524579Szliu  *	1. replace x by |x| and y by |y|, and swap x and
2624579Szliu  *	   y if y > x (hence x is never smaller than y).
2724579Szliu  *	2. Hypot(x,y) is computed by:
2824579Szliu  *	   Case I, x/y > 2
2924579Szliu  *
3024579Szliu  *				       y
3124579Szliu  *		hypot = x + -----------------------------
3224579Szliu  *			 		    2
3324579Szliu  *			    sqrt ( 1 + [x/y]  )  +  x/y
3424579Szliu  *
3524579Szliu  *	   Case II, x/y <= 2
3624579Szliu  *				                   y
3724579Szliu  *		hypot = x + --------------------------------------------------
3824579Szliu  *				          		     2
3924579Szliu  *				     			[x/y]   -  2
4024579Szliu  *			   (sqrt(2)+1) + (x-y)/y + -----------------------------
4124579Szliu  *			 		    			  2
4224579Szliu  *			    			  sqrt ( 1 + [x/y]  )  + sqrt(2)
4324579Szliu  *
4424579Szliu  *
4524579Szliu  *
4624579Szliu  * Special cases:
4724579Szliu  *	hypot(x,y) is INF if x or y is +INF or -INF; else
4824579Szliu  *	hypot(x,y) is NAN if x or y is NAN.
4924579Szliu  *
5024579Szliu  * Accuracy:
5124579Szliu  * 	hypot(x,y) returns the sqrt(x^2+y^2) with error less than 1 ulps (units
5224579Szliu  *	in the last place). See Kahan's "Interval Arithmetic Options in the
5324579Szliu  *	Proposed IEEE Floating Point Arithmetic Standard", Interval Mathematics
5424579Szliu  *      1980, Edited by Karl L.E. Nickel, pp 99-128. (A faster but less accurate
5524579Szliu  *	code follows in	comments.) In a test run with 500,000 random arguments
5624579Szliu  *	on a VAX, the maximum observed error was .959 ulps.
5724579Szliu  *
5824579Szliu  * Constants:
5924579Szliu  * The hexadecimal values are the intended ones for the following constants.
6024579Szliu  * The decimal values may be used, provided that the compiler will convert
6124579Szliu  * from decimal to binary accurately enough to produce the hexadecimal values
6224579Szliu  * shown.
6324579Szliu  */
6435681Sbostic #include "mathimpl.h"
6524579Szliu 
6635681Sbostic vc(r2p1hi, 2.4142135623730950345E0   ,8279,411a,ef32,99fc,   2, .9A827999FCEF32)
6735681Sbostic vc(r2p1lo, 1.4349369327986523769E-17 ,597d,2484,754b,89b3, -55, .84597D89B3754B)
6835681Sbostic vc(sqrt2,  1.4142135623730950622E0   ,04f3,40b5,de65,33f9,   1, .B504F333F9DE65)
6924579Szliu 
7035681Sbostic ic(r2p1hi, 2.4142135623730949234E0   ,   1, 1.3504F333F9DE6)
7135681Sbostic ic(r2p1lo, 1.2537167179050217666E-16 , -53, 1.21165F626CDD5)
7235681Sbostic ic(sqrt2,  1.4142135623730951455E0   ,   0, 1.6A09E667F3BCD)
7335681Sbostic 
7435681Sbostic #ifdef vccast
7535681Sbostic #define	r2p1hi	vccast(r2p1hi)
7635681Sbostic #define	r2p1lo	vccast(r2p1lo)
7735681Sbostic #define	sqrt2	vccast(sqrt2)
7835681Sbostic #endif
7935681Sbostic 
8031991Szliu double
8131991Szliu hypot(x,y)
8224579Szliu double x, y;
8324579Szliu {
8435681Sbostic 	static const double zero=0, one=1,
8524579Szliu 		      small=1.0E-18;	/* fl(1+small)==1 */
8635681Sbostic 	static const ibig=30;	/* fl(1+2**(2*ibig))==1 */
8735681Sbostic 	double t,r;
8835681Sbostic 	int exp;
8924579Szliu 
9024579Szliu 	if(finite(x))
9124579Szliu 	    if(finite(y))
9224579Szliu 	    {
9324579Szliu 		x=copysign(x,one);
9424579Szliu 		y=copysign(y,one);
9524579Szliu 		if(y > x)
9624579Szliu 		    { t=x; x=y; y=t; }
9724579Szliu 		if(x == zero) return(zero);
9824579Szliu 		if(y == zero) return(x);
9924579Szliu 		exp= logb(x);
10024579Szliu 		if(exp-(int)logb(y) > ibig )
10124579Szliu 			/* raise inexact flag and return |x| */
10224579Szliu 		   { one+small; return(x); }
10324579Szliu 
10424579Szliu 	    /* start computing sqrt(x^2 + y^2) */
10524579Szliu 		r=x-y;
10624579Szliu 		if(r>y) { 	/* x/y > 2 */
10724579Szliu 		    r=x/y;
10824579Szliu 		    r=r+sqrt(one+r*r); }
10924579Szliu 		else {		/* 1 <= x/y <= 2 */
11024579Szliu 		    r/=y; t=r*(r+2.0);
11124579Szliu 		    r+=t/(sqrt2+sqrt(2.0+t));
11224579Szliu 		    r+=r2p1lo; r+=r2p1hi; }
11324579Szliu 
11424579Szliu 		r=y/r;
11524579Szliu 		return(x+r);
11624579Szliu 
11724579Szliu 	    }
11824579Szliu 
11924579Szliu 	    else if(y==y)   	   /* y is +-INF */
12024579Szliu 		     return(copysign(y,one));
12124579Szliu 	    else
12224579Szliu 		     return(y);	   /* y is NaN and x is finite */
12324579Szliu 
12424579Szliu 	else if(x==x) 		   /* x is +-INF */
12524579Szliu 	         return (copysign(x,one));
12624579Szliu 	else if(finite(y))
12724579Szliu 	         return(x);		   /* x is NaN, y is finite */
12831855Szliu #if !defined(vax)&&!defined(tahoe)
12924579Szliu 	else if(y!=y) return(y);  /* x and y is NaN */
13031855Szliu #endif	/* !defined(vax)&&!defined(tahoe) */
13124579Szliu 	else return(copysign(y,one));   /* y is INF */
13224579Szliu }
13324579Szliu 
13431991Szliu /* CABS(Z)
13531991Szliu  * RETURN THE ABSOLUTE VALUE OF THE COMPLEX NUMBER  Z = X + iY
13631991Szliu  * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS)
13731991Szliu  * CODED IN C BY K.C. NG, 11/28/84.
13831991Szliu  * REVISED BY K.C. NG, 7/12/85.
13931991Szliu  *
14031991Szliu  * Required kernel function :
14131991Szliu  *	hypot(x,y)
14231991Szliu  *
14331991Szliu  * Method :
14431991Szliu  *	cabs(z) = hypot(x,y) .
14531991Szliu  */
14631991Szliu 
14758565Sralph struct complex { double x, y; };
14858565Sralph 
14931991Szliu double
cabs(z)15031991Szliu cabs(z)
15158565Sralph struct complex z;
15231991Szliu {
15331991Szliu 	return hypot(z.x,z.y);
15431991Szliu }
15531991Szliu 
15631991Szliu double
z_abs(z)15731991Szliu z_abs(z)
15858565Sralph struct complex *z;
15931991Szliu {
16031991Szliu 	return hypot(z->x,z->y);
16131991Szliu }
16231991Szliu 
16324579Szliu /* A faster but less accurate version of cabs(x,y) */
16424579Szliu #if 0
16524579Szliu double hypot(x,y)
16624579Szliu double x, y;
16724579Szliu {
16835681Sbostic 	static const double zero=0, one=1;
16924579Szliu 		      small=1.0E-18;	/* fl(1+small)==1 */
17035681Sbostic 	static const ibig=30;	/* fl(1+2**(2*ibig))==1 */
17135681Sbostic 	double temp;
17235681Sbostic 	int exp;
17324579Szliu 
17424579Szliu 	if(finite(x))
17524579Szliu 	    if(finite(y))
17624579Szliu 	    {
17724579Szliu 		x=copysign(x,one);
17824579Szliu 		y=copysign(y,one);
17924579Szliu 		if(y > x)
18024579Szliu 		    { temp=x; x=y; y=temp; }
18124579Szliu 		if(x == zero) return(zero);
18224579Szliu 		if(y == zero) return(x);
18324579Szliu 		exp= logb(x);
18424579Szliu 		x=scalb(x,-exp);
18524579Szliu 		if(exp-(int)logb(y) > ibig )
18624579Szliu 			/* raise inexact flag and return |x| */
18724579Szliu 		   { one+small; return(scalb(x,exp)); }
18824579Szliu 		else y=scalb(y,-exp);
18924579Szliu 		return(scalb(sqrt(x*x+y*y),exp));
19024579Szliu 	    }
19124579Szliu 
19224579Szliu 	    else if(y==y)   	   /* y is +-INF */
19324579Szliu 		     return(copysign(y,one));
19424579Szliu 	    else
19524579Szliu 		     return(y);	   /* y is NaN and x is finite */
19624579Szliu 
19724579Szliu 	else if(x==x) 		   /* x is +-INF */
19824579Szliu 	         return (copysign(x,one));
19924579Szliu 	else if(finite(y))
20024579Szliu 	         return(x);		   /* x is NaN, y is finite */
20124579Szliu 	else if(y!=y) return(y);  	/* x and y is NaN */
20224579Szliu 	else return(copysign(y,one));   /* y is INF */
20324579Szliu }
20424579Szliu #endif
205