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