Home | History | Annotate | Line # | Download | only in noieee_src
n_cabs.c revision 1.3
      1  1.3  simonb /*      $NetBSD: n_cabs.c,v 1.3 1999/07/02 15:37:36 simonb Exp $ */
      2  1.1   ragge /*
      3  1.1   ragge  * Copyright (c) 1985, 1993
      4  1.1   ragge  *	The Regents of the University of California.  All rights reserved.
      5  1.1   ragge  *
      6  1.1   ragge  * Redistribution and use in source and binary forms, with or without
      7  1.1   ragge  * modification, are permitted provided that the following conditions
      8  1.1   ragge  * are met:
      9  1.1   ragge  * 1. Redistributions of source code must retain the above copyright
     10  1.1   ragge  *    notice, this list of conditions and the following disclaimer.
     11  1.1   ragge  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1   ragge  *    notice, this list of conditions and the following disclaimer in the
     13  1.1   ragge  *    documentation and/or other materials provided with the distribution.
     14  1.1   ragge  * 3. All advertising materials mentioning features or use of this software
     15  1.1   ragge  *    must display the following acknowledgement:
     16  1.1   ragge  *	This product includes software developed by the University of
     17  1.1   ragge  *	California, Berkeley and its contributors.
     18  1.1   ragge  * 4. Neither the name of the University nor the names of its contributors
     19  1.1   ragge  *    may be used to endorse or promote products derived from this software
     20  1.1   ragge  *    without specific prior written permission.
     21  1.1   ragge  *
     22  1.1   ragge  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  1.1   ragge  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  1.1   ragge  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  1.1   ragge  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  1.1   ragge  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  1.1   ragge  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  1.1   ragge  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  1.1   ragge  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  1.1   ragge  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  1.1   ragge  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  1.1   ragge  * SUCH DAMAGE.
     33  1.1   ragge  */
     34  1.1   ragge 
     35  1.1   ragge #ifndef lint
     36  1.1   ragge static char sccsid[] = "@(#)cabs.c	8.1 (Berkeley) 6/4/93";
     37  1.1   ragge #endif /* not lint */
     38  1.1   ragge 
     39  1.1   ragge /* HYPOT(X,Y)
     40  1.1   ragge  * RETURN THE SQUARE ROOT OF X^2 + Y^2  WHERE Z=X+iY
     41  1.1   ragge  * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS)
     42  1.3  simonb  * CODED IN C BY K.C. NG, 11/28/84;
     43  1.1   ragge  * REVISED BY K.C. NG, 7/12/85.
     44  1.1   ragge  *
     45  1.1   ragge  * Required system supported functions :
     46  1.1   ragge  *	copysign(x,y)
     47  1.1   ragge  *	finite(x)
     48  1.1   ragge  *	scalb(x,N)
     49  1.1   ragge  *	sqrt(x)
     50  1.1   ragge  *
     51  1.1   ragge  * Method :
     52  1.1   ragge  *	1. replace x by |x| and y by |y|, and swap x and
     53  1.1   ragge  *	   y if y > x (hence x is never smaller than y).
     54  1.1   ragge  *	2. Hypot(x,y) is computed by:
     55  1.1   ragge  *	   Case I, x/y > 2
     56  1.3  simonb  *
     57  1.1   ragge  *				       y
     58  1.1   ragge  *		hypot = x + -----------------------------
     59  1.1   ragge  *			 		    2
     60  1.1   ragge  *			    sqrt ( 1 + [x/y]  )  +  x/y
     61  1.1   ragge  *
     62  1.3  simonb  *	   Case II, x/y <= 2
     63  1.1   ragge  *				                   y
     64  1.1   ragge  *		hypot = x + --------------------------------------------------
     65  1.3  simonb  *				          		     2
     66  1.1   ragge  *				     			[x/y]   -  2
     67  1.1   ragge  *			   (sqrt(2)+1) + (x-y)/y + -----------------------------
     68  1.1   ragge  *			 		    			  2
     69  1.1   ragge  *			    			  sqrt ( 1 + [x/y]  )  + sqrt(2)
     70  1.1   ragge  *
     71  1.1   ragge  *
     72  1.1   ragge  *
     73  1.1   ragge  * Special cases:
     74  1.1   ragge  *	hypot(x,y) is INF if x or y is +INF or -INF; else
     75  1.1   ragge  *	hypot(x,y) is NAN if x or y is NAN.
     76  1.1   ragge  *
     77  1.1   ragge  * Accuracy:
     78  1.1   ragge  * 	hypot(x,y) returns the sqrt(x^2+y^2) with error less than 1 ulps (units
     79  1.1   ragge  *	in the last place). See Kahan's "Interval Arithmetic Options in the
     80  1.1   ragge  *	Proposed IEEE Floating Point Arithmetic Standard", Interval Mathematics
     81  1.1   ragge  *      1980, Edited by Karl L.E. Nickel, pp 99-128. (A faster but less accurate
     82  1.1   ragge  *	code follows in	comments.) In a test run with 500,000 random arguments
     83  1.1   ragge  *	on a VAX, the maximum observed error was .959 ulps.
     84  1.1   ragge  *
     85  1.1   ragge  * Constants:
     86  1.1   ragge  * The hexadecimal values are the intended ones for the following constants.
     87  1.1   ragge  * The decimal values may be used, provided that the compiler will convert
     88  1.1   ragge  * from decimal to binary accurately enough to produce the hexadecimal values
     89  1.1   ragge  * shown.
     90  1.1   ragge  */
     91  1.1   ragge #include "mathimpl.h"
     92  1.1   ragge 
     93  1.1   ragge vc(r2p1hi, 2.4142135623730950345E0   ,8279,411a,ef32,99fc,   2, .9A827999FCEF32)
     94  1.1   ragge vc(r2p1lo, 1.4349369327986523769E-17 ,597d,2484,754b,89b3, -55, .84597D89B3754B)
     95  1.1   ragge vc(sqrt2,  1.4142135623730950622E0   ,04f3,40b5,de65,33f9,   1, .B504F333F9DE65)
     96  1.1   ragge 
     97  1.1   ragge ic(r2p1hi, 2.4142135623730949234E0   ,   1, 1.3504F333F9DE6)
     98  1.1   ragge ic(r2p1lo, 1.2537167179050217666E-16 , -53, 1.21165F626CDD5)
     99  1.1   ragge ic(sqrt2,  1.4142135623730951455E0   ,   0, 1.6A09E667F3BCD)
    100  1.1   ragge 
    101  1.1   ragge #ifdef vccast
    102  1.1   ragge #define	r2p1hi	vccast(r2p1hi)
    103  1.1   ragge #define	r2p1lo	vccast(r2p1lo)
    104  1.1   ragge #define	sqrt2	vccast(sqrt2)
    105  1.1   ragge #endif
    106  1.1   ragge 
    107  1.1   ragge double
    108  1.1   ragge hypot(x,y)
    109  1.1   ragge double x, y;
    110  1.1   ragge {
    111  1.3  simonb 	static const double zero=0, one=1,
    112  1.1   ragge 		      small=1.0E-18;	/* fl(1+small)==1 */
    113  1.1   ragge 	static const ibig=30;	/* fl(1+2**(2*ibig))==1 */
    114  1.1   ragge 	double t,r;
    115  1.1   ragge 	int exp;
    116  1.1   ragge 
    117  1.1   ragge 	if(finite(x))
    118  1.1   ragge 	    if(finite(y))
    119  1.3  simonb 	    {
    120  1.1   ragge 		x=copysign(x,one);
    121  1.1   ragge 		y=copysign(y,one);
    122  1.3  simonb 		if(y > x)
    123  1.1   ragge 		    { t=x; x=y; y=t; }
    124  1.1   ragge 		if(x == zero) return(zero);
    125  1.1   ragge 		if(y == zero) return(x);
    126  1.1   ragge 		exp= logb(x);
    127  1.3  simonb 		if(exp-(int)logb(y) > ibig )
    128  1.1   ragge 			/* raise inexact flag and return |x| */
    129  1.1   ragge 		   { one+small; return(x); }
    130  1.1   ragge 
    131  1.1   ragge 	    /* start computing sqrt(x^2 + y^2) */
    132  1.1   ragge 		r=x-y;
    133  1.1   ragge 		if(r>y) { 	/* x/y > 2 */
    134  1.1   ragge 		    r=x/y;
    135  1.1   ragge 		    r=r+sqrt(one+r*r); }
    136  1.1   ragge 		else {		/* 1 <= x/y <= 2 */
    137  1.1   ragge 		    r/=y; t=r*(r+2.0);
    138  1.1   ragge 		    r+=t/(sqrt2+sqrt(2.0+t));
    139  1.1   ragge 		    r+=r2p1lo; r+=r2p1hi; }
    140  1.1   ragge 
    141  1.1   ragge 		r=y/r;
    142  1.1   ragge 		return(x+r);
    143  1.1   ragge 
    144  1.1   ragge 	    }
    145  1.1   ragge 
    146  1.1   ragge 	    else if(y==y)   	   /* y is +-INF */
    147  1.1   ragge 		     return(copysign(y,one));
    148  1.3  simonb 	    else
    149  1.1   ragge 		     return(y);	   /* y is NaN and x is finite */
    150  1.1   ragge 
    151  1.1   ragge 	else if(x==x) 		   /* x is +-INF */
    152  1.1   ragge 	         return (copysign(x,one));
    153  1.1   ragge 	else if(finite(y))
    154  1.1   ragge 	         return(x);		   /* x is NaN, y is finite */
    155  1.2    matt #if !defined(__vax__)&&!defined(tahoe)
    156  1.1   ragge 	else if(y!=y) return(y);  /* x and y is NaN */
    157  1.2    matt #endif	/* !defined(__vax__)&&!defined(tahoe) */
    158  1.1   ragge 	else return(copysign(y,one));   /* y is INF */
    159  1.1   ragge }
    160  1.1   ragge 
    161  1.1   ragge /* CABS(Z)
    162  1.1   ragge  * RETURN THE ABSOLUTE VALUE OF THE COMPLEX NUMBER  Z = X + iY
    163  1.1   ragge  * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS)
    164  1.1   ragge  * CODED IN C BY K.C. NG, 11/28/84.
    165  1.1   ragge  * REVISED BY K.C. NG, 7/12/85.
    166  1.1   ragge  *
    167  1.1   ragge  * Required kernel function :
    168  1.1   ragge  *	hypot(x,y)
    169  1.1   ragge  *
    170  1.1   ragge  * Method :
    171  1.1   ragge  *	cabs(z) = hypot(x,y) .
    172  1.1   ragge  */
    173  1.1   ragge 
    174  1.1   ragge struct complex { double x, y; };
    175  1.1   ragge 
    176  1.1   ragge double
    177  1.1   ragge cabs(z)
    178  1.1   ragge struct complex z;
    179  1.1   ragge {
    180  1.1   ragge 	return hypot(z.x,z.y);
    181  1.1   ragge }
    182  1.1   ragge 
    183  1.1   ragge double
    184  1.1   ragge z_abs(z)
    185  1.1   ragge struct complex *z;
    186  1.1   ragge {
    187  1.1   ragge 	return hypot(z->x,z->y);
    188  1.1   ragge }
    189  1.1   ragge 
    190  1.1   ragge /* A faster but less accurate version of cabs(x,y) */
    191  1.1   ragge #if 0
    192  1.1   ragge double hypot(x,y)
    193  1.1   ragge double x, y;
    194  1.1   ragge {
    195  1.1   ragge 	static const double zero=0, one=1;
    196  1.1   ragge 		      small=1.0E-18;	/* fl(1+small)==1 */
    197  1.1   ragge 	static const ibig=30;	/* fl(1+2**(2*ibig))==1 */
    198  1.1   ragge 	double temp;
    199  1.1   ragge 	int exp;
    200  1.1   ragge 
    201  1.1   ragge 	if(finite(x))
    202  1.1   ragge 	    if(finite(y))
    203  1.3  simonb 	    {
    204  1.1   ragge 		x=copysign(x,one);
    205  1.1   ragge 		y=copysign(y,one);
    206  1.3  simonb 		if(y > x)
    207  1.1   ragge 		    { temp=x; x=y; y=temp; }
    208  1.1   ragge 		if(x == zero) return(zero);
    209  1.1   ragge 		if(y == zero) return(x);
    210  1.1   ragge 		exp= logb(x);
    211  1.1   ragge 		x=scalb(x,-exp);
    212  1.3  simonb 		if(exp-(int)logb(y) > ibig )
    213  1.1   ragge 			/* raise inexact flag and return |x| */
    214  1.1   ragge 		   { one+small; return(scalb(x,exp)); }
    215  1.1   ragge 		else y=scalb(y,-exp);
    216  1.1   ragge 		return(scalb(sqrt(x*x+y*y),exp));
    217  1.1   ragge 	    }
    218  1.1   ragge 
    219  1.1   ragge 	    else if(y==y)   	   /* y is +-INF */
    220  1.1   ragge 		     return(copysign(y,one));
    221  1.3  simonb 	    else
    222  1.1   ragge 		     return(y);	   /* y is NaN and x is finite */
    223  1.1   ragge 
    224  1.1   ragge 	else if(x==x) 		   /* x is +-INF */
    225  1.1   ragge 	         return (copysign(x,one));
    226  1.1   ragge 	else if(finite(y))
    227  1.1   ragge 	         return(x);		   /* x is NaN, y is finite */
    228  1.1   ragge 	else if(y!=y) return(y);  	/* x and y is NaN */
    229  1.1   ragge 	else return(copysign(y,one));   /* y is INF */
    230  1.1   ragge }
    231  1.1   ragge #endif
    232