amath  1.8.5
Simple command line calculator
acos.c
Go to the documentation of this file.
1 /*-
2  * Copyright (c) 2014-2018 Carsten Sonne Larsen <cs@innolan.net>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  * Project homepage:
26  * https://amath.innolan.net
27  *
28  * The original source code can be obtained from:
29  * http://www.netlib.org/fdlibm/e_acos.c
30  *
31  * =================================================================
32  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
33  *
34  * Developed at SunSoft, a Sun Microsystems, Inc. business.
35  * Permission to use, copy, modify, and distribute this
36  * software is freely granted, provided that this notice
37  * is preserved.
38  * =================================================================
39  */
40 
41 /**
42  * @file acos.c
43  * @brief Inverse cosine function
44  */
45 
46 #include "prim.h"
47 
48 static const double
49  one = 1.00000000000000000000e+00, /* 0x3FF00000, 0x00000000 */
50  pi = 3.14159265358979311600e+00, /* 0x400921FB, 0x54442D18 */
51  pio2_hi = 1.57079632679489655800e+00, /* 0x3FF921FB, 0x54442D18 */
52  pio2_lo = 6.12323399573676603587e-17, /* 0x3C91A626, 0x33145C07 */
53  pS0 = 1.66666666666666657415e-01, /* 0x3FC55555, 0x55555555 */
54  pS1 = -3.25565818622400915405e-01, /* 0xBFD4D612, 0x03EB6F7D */
55  pS2 = 2.01212532134862925881e-01, /* 0x3FC9C155, 0x0E884455 */
56  pS3 = -4.00555345006794114027e-02, /* 0xBFA48228, 0xB5688F3B */
57  pS4 = 7.91534994289814532176e-04, /* 0x3F49EFE0, 0x7501B288 */
58  pS5 = 3.47933107596021167570e-05, /* 0x3F023DE1, 0x0DFDF709 */
59  qS1 = -2.40339491173441421878e+00, /* 0xC0033A27, 0x1C8A2D4B */
60  qS2 = 2.02094576023350569471e+00, /* 0x40002AE5, 0x9C598AC8 */
61  qS3 = -6.88283971605453293030e-01, /* 0xBFE6066C, 0x1B8D0159 */
62  qS4 = 7.70381505559019352791e-02; /* 0x3FB3B8C5, 0xB12E9282 */
63 
64 /**
65  * @brief Inverse cosine function
66  * @details
67  * <pre>
68  * Method
69  * acos(x) = pi/2 - asin(x)
70  * acos(-x) = pi/2 + asin(x)
71  *
72  * For |x|<=0.5
73  * acos(x) = pi/2 - (x + x*x^2*R(x^2)) (see asin.c)
74  *
75  * For x>0.5
76  * acos(x) = pi/2 - (pi/2 - 2asin(sqrt((1-x)/2)))
77  * = 2asin(sqrt((1-x)/2))
78  * = 2s + 2s*z*R(z) ...z=(1-x)/2, s=sqrt(z)
79  * = 2f + (2c + 2s*z*R(z))
80  * where f=hi part of s, and c = (z-f*f)/(s+f) is the correction term
81  * for f so that f+c ~ sqrt(z).
82  *
83  * For x<-0.5
84  * acos(x) = pi - 2asin(sqrt((1-|x|)/2))
85  * = pi - 0.5*(s+s*z*R(z)), where z=(1-|x|)/2,s=sqrt(z)
86  *
87  * Special cases
88  * if x is NaN, return NaN
89  * if |x|>1, return NaN
90  * </pre>
91  */
92 double acos(double x)
93 {
94  double z, p, q, r, w, s, c, df;
95  int32_t hx, ix;
96  uint32_t lx;
97 
98  GET_HIGH_WORD(hx, x);
99  ix = hx & 0x7FFFFFFF;
100 
101  // |x| >= 1
102  if (ix >= 0x3FF00000)
103  {
104  GET_LOW_WORD(lx, x);
105 
106  // |x|==1
107  if (((ix - 0x3FF00000) | lx) == 0)
108  {
109  if (hx > 0)
110  {
111  // acos(1) = 0
112  return 0.0;
113  }
114 
115  // acos(-1) = pi
116  return pi + 2.0 * pio2_lo;
117  }
118 
119  // acos(|x|>1) is NaN
120  return NAN;
121  }
122 
123  // |x| < 0.5
124  if (ix < 0x3FE00000)
125  {
126  // if |x|<2**-57
127  if (ix <= 0x3C600000)
128  {
129  return pio2_hi + pio2_lo;
130  }
131  z = x * x;
132  p = z * (pS0 + z * (pS1 + z * (pS2 + z * (pS3 + z * (pS4 + z * pS5)))));
133  q = one + z * (qS1 + z * (qS2 + z * (qS3 + z * qS4)));
134  r = p / q;
135  return pio2_hi - (x - (pio2_lo - x * r));
136  }
137 
138  // x < -0.5
139  if (hx < 0)
140  {
141  z = (one + x) * 0.5;
142  p = z * (pS0 + z * (pS1 + z * (pS2 + z * (pS3 + z * (pS4 + z * pS5)))));
143  q = one + z * (qS1 + z * (qS2 + z * (qS3 + z * qS4)));
144  s = sqrt(z);
145  r = p / q;
146  w = r * s - pio2_lo;
147  return pi - 2.0 * (s + w);
148  }
149 
150  // x > 0.5
151  z = (one - x) * 0.5;
152  s = sqrt(z);
153  df = s;
154  SET_LOW_WORD(df, 0);
155  c = (z - df * df) / (s + df);
156  p = z * (pS0 + z * (pS1 + z * (pS2 + z * (pS3 + z * (pS4 + z * pS5)))));
157  q = one + z * (qS1 + z * (qS2 + z * (qS3 + z * qS4)));
158  r = p / q;
159  w = r * s + c;
160  return 2.0 * (df + w);
161 }
double sqrt(double x)
Square root function.
Definition: sqrt.c:119
static const double pi
Definition: acos.c:50
static const double pS2
Definition: acos.c:55
#define GET_HIGH_WORD(i, d)
Get the more significant 32 bit int from a double.
Definition: prim.h:167
static const double pS4
Definition: acos.c:57
static const double pio2_hi
Definition: acos.c:51
#define GET_LOW_WORD(i, d)
Get the less significant 32 bit int from a double.
Definition: prim.h:177
static const double pS5
Definition: acos.c:58
static const double pS0
Definition: acos.c:53
#define NAN
Definition: mathr.h:53
static const double one
Definition: acos.c:49
static const double qS4
Definition: acos.c:62
#define SET_LOW_WORD(d, v)
Set the less significant 32 bits of a double from an int.
Definition: prim.h:209
static const double qS3
Definition: acos.c:61
double acos(double x)
Inverse cosine function.
Definition: acos.c:92
static const double qS1
Definition: acos.c:59
static const double pS3
Definition: acos.c:56
static const double qS2
Definition: acos.c:60
static const double pio2_lo
Definition: acos.c:52
static const double pS1
Definition: acos.c:54