1 |
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
2 |
* |
3 |
* ***** BEGIN LICENSE BLOCK ***** |
4 |
* Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
5 |
* |
6 |
* The contents of this file are subject to the Mozilla Public License Version |
7 |
* 1.1 (the "License"); you may not use this file except in compliance with |
8 |
* the License. You may obtain a copy of the License at |
9 |
* http://www.mozilla.org/MPL/ |
10 |
* |
11 |
* Software distributed under the License is distributed on an "AS IS" basis, |
12 |
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
13 |
* for the specific language governing rights and limitations under the |
14 |
* License. |
15 |
* |
16 |
* The Original Code is Mozilla Communicator client code, released |
17 |
* March 31, 1998. |
18 |
* |
19 |
* The Initial Developer of the Original Code is |
20 |
* Sun Microsystems, Inc. |
21 |
* Portions created by the Initial Developer are Copyright (C) 1998 |
22 |
* the Initial Developer. All Rights Reserved. |
23 |
* |
24 |
* Contributor(s): |
25 |
* |
26 |
* Alternatively, the contents of this file may be used under the terms of |
27 |
* either of the GNU General Public License Version 2 or later (the "GPL"), |
28 |
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
29 |
* in which case the provisions of the GPL or the LGPL are applicable instead |
30 |
* of those above. If you wish to allow use of your version of this file only |
31 |
* under the terms of either the GPL or the LGPL, and not to allow others to |
32 |
* use your version of this file under the terms of the MPL, indicate your |
33 |
* decision by deleting the provisions above and replace them with the notice |
34 |
* and other provisions required by the GPL or the LGPL. If you do not delete |
35 |
* the provisions above, a recipient may use your version of this file under |
36 |
* the terms of any one of the MPL, the GPL or the LGPL. |
37 |
* |
38 |
* ***** END LICENSE BLOCK ***** */ |
39 |
|
40 |
/* @(#)e_acos.c 1.3 95/01/18 */ |
41 |
/* |
42 |
* ==================================================== |
43 |
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. |
44 |
* |
45 |
* Developed at SunSoft, a Sun Microsystems, Inc. business. |
46 |
* Permission to use, copy, modify, and distribute this |
47 |
* software is freely granted, provided that this notice |
48 |
* is preserved. |
49 |
* ==================================================== |
50 |
*/ |
51 |
|
52 |
/* __ieee754_acos(x) |
53 |
* Method : |
54 |
* acos(x) = pi/2 - asin(x) |
55 |
* acos(-x) = pi/2 + asin(x) |
56 |
* For |x|<=0.5 |
57 |
* acos(x) = pi/2 - (x + x*x^2*R(x^2)) (see asin.c) |
58 |
* For x>0.5 |
59 |
* acos(x) = pi/2 - (pi/2 - 2asin(sqrt((1-x)/2))) |
60 |
* = 2asin(sqrt((1-x)/2)) |
61 |
* = 2s + 2s*z*R(z) ...z=(1-x)/2, s=sqrt(z) |
62 |
* = 2f + (2c + 2s*z*R(z)) |
63 |
* where f=hi part of s, and c = (z-f*f)/(s+f) is the correction term |
64 |
* for f so that f+c ~ sqrt(z). |
65 |
* For x<-0.5 |
66 |
* acos(x) = pi - 2asin(sqrt((1-|x|)/2)) |
67 |
* = pi - 0.5*(s+s*z*R(z)), where z=(1-|x|)/2,s=sqrt(z) |
68 |
* |
69 |
* Special cases: |
70 |
* if x is NaN, return x itself; |
71 |
* if |x|>1, return NaN with invalid signal. |
72 |
* |
73 |
* Function needed: sqrt |
74 |
*/ |
75 |
|
76 |
#include "fdlibm.h" |
77 |
|
78 |
#ifdef __STDC__ |
79 |
static const double |
80 |
#else |
81 |
static double |
82 |
#endif |
83 |
one= 1.00000000000000000000e+00, /* 0x3FF00000, 0x00000000 */ |
84 |
pi = 3.14159265358979311600e+00, /* 0x400921FB, 0x54442D18 */ |
85 |
pio2_hi = 1.57079632679489655800e+00, /* 0x3FF921FB, 0x54442D18 */ |
86 |
pio2_lo = 6.12323399573676603587e-17, /* 0x3C91A626, 0x33145C07 */ |
87 |
pS0 = 1.66666666666666657415e-01, /* 0x3FC55555, 0x55555555 */ |
88 |
pS1 = -3.25565818622400915405e-01, /* 0xBFD4D612, 0x03EB6F7D */ |
89 |
pS2 = 2.01212532134862925881e-01, /* 0x3FC9C155, 0x0E884455 */ |
90 |
pS3 = -4.00555345006794114027e-02, /* 0xBFA48228, 0xB5688F3B */ |
91 |
pS4 = 7.91534994289814532176e-04, /* 0x3F49EFE0, 0x7501B288 */ |
92 |
pS5 = 3.47933107596021167570e-05, /* 0x3F023DE1, 0x0DFDF709 */ |
93 |
qS1 = -2.40339491173441421878e+00, /* 0xC0033A27, 0x1C8A2D4B */ |
94 |
qS2 = 2.02094576023350569471e+00, /* 0x40002AE5, 0x9C598AC8 */ |
95 |
qS3 = -6.88283971605453293030e-01, /* 0xBFE6066C, 0x1B8D0159 */ |
96 |
qS4 = 7.70381505559019352791e-02; /* 0x3FB3B8C5, 0xB12E9282 */ |
97 |
|
98 |
#ifdef __STDC__ |
99 |
double __ieee754_acos(double x) |
100 |
#else |
101 |
double __ieee754_acos(x) |
102 |
double x; |
103 |
#endif |
104 |
{ |
105 |
fd_twoints u; |
106 |
double df; |
107 |
double z,p,q,r,w,s,c; |
108 |
int hx,ix; |
109 |
u.d = x; |
110 |
hx = __HI(u); |
111 |
ix = hx&0x7fffffff; |
112 |
if(ix>=0x3ff00000) { /* |x| >= 1 */ |
113 |
if(((ix-0x3ff00000)|__LO(u))==0) { /* |x|==1 */ |
114 |
if(hx>0) return 0.0; /* acos(1) = 0 */ |
115 |
else return pi+2.0*pio2_lo; /* acos(-1)= pi */ |
116 |
} |
117 |
return (x-x)/(x-x); /* acos(|x|>1) is NaN */ |
118 |
} |
119 |
if(ix<0x3fe00000) { /* |x| < 0.5 */ |
120 |
if(ix<=0x3c600000) return pio2_hi+pio2_lo;/*if|x|<2**-57*/ |
121 |
z = x*x; |
122 |
p = z*(pS0+z*(pS1+z*(pS2+z*(pS3+z*(pS4+z*pS5))))); |
123 |
q = one+z*(qS1+z*(qS2+z*(qS3+z*qS4))); |
124 |
r = p/q; |
125 |
return pio2_hi - (x - (pio2_lo-x*r)); |
126 |
} else if (hx<0) { /* x < -0.5 */ |
127 |
z = (one+x)*0.5; |
128 |
p = z*(pS0+z*(pS1+z*(pS2+z*(pS3+z*(pS4+z*pS5))))); |
129 |
q = one+z*(qS1+z*(qS2+z*(qS3+z*qS4))); |
130 |
s = fd_sqrt(z); |
131 |
r = p/q; |
132 |
w = r*s-pio2_lo; |
133 |
return pi - 2.0*(s+w); |
134 |
} else { /* x > 0.5 */ |
135 |
z = (one-x)*0.5; |
136 |
s = fd_sqrt(z); |
137 |
u.d = s; |
138 |
__LO(u) = 0; |
139 |
df = u.d; |
140 |
c = (z-df*df)/(s+df); |
141 |
p = z*(pS0+z*(pS1+z*(pS2+z*(pS3+z*(pS4+z*pS5))))); |
142 |
q = one+z*(qS1+z*(qS2+z*(qS3+z*qS4))); |
143 |
r = p/q; |
144 |
w = r*s+c; |
145 |
return 2.0*(df+w); |
146 |
} |
147 |
} |