amath  1.8.5
Simple command line calculator
asin.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_asin.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 asin.c
43  * @brief Inverse sine function
44  */
45 
46 #include "prim.h"
47 
48 static const double
49  one = 1.00000000000000000000e+00, /* 0x3FF00000, 0x00000000 */
50  huge = 1.000e+300,
51  pio2_hi = 1.57079632679489655800e+00, /* 0x3FF921FB, 0x54442D18 */
52  pio2_lo = 6.12323399573676603587e-17, /* 0x3C91A626, 0x33145C07 */
53  pio4_hi = 7.85398163397448278999e-01, /* 0x3FE921FB, 0x54442D18 */
54  /* coefficient for R(x^2) */
55  pS0 = 1.66666666666666657415e-01, /* 0x3FC55555, 0x55555555 */
56  pS1 = -3.25565818622400915405e-01, /* 0xBFD4D612, 0x03EB6F7D */
57  pS2 = 2.01212532134862925881e-01, /* 0x3FC9C155, 0x0E884455 */
58  pS3 = -4.00555345006794114027e-02, /* 0xBFA48228, 0xB5688F3B */
59  pS4 = 7.91534994289814532176e-04, /* 0x3F49EFE0, 0x7501B288 */
60  pS5 = 3.47933107596021167570e-05, /* 0x3F023DE1, 0x0DFDF709 */
61  qS1 = -2.40339491173441421878e+00, /* 0xC0033A27, 0x1C8A2D4B */
62  qS2 = 2.02094576023350569471e+00, /* 0x40002AE5, 0x9C598AC8 */
63  qS3 = -6.88283971605453293030e-01, /* 0xBFE6066C, 0x1B8D0159 */
64  qS4 = 7.70381505559019352791e-02; /* 0x3FB3B8C5, 0xB12E9282 */
65 
66 /**
67  * @brief Inverse sine function
68  * @details
69  * <pre>
70  * Method
71  *
72  * Since asin(x) = x + x^3/6 + x^5*3/40 + x^7*15/336 + ...
73  * we approximate asin(x) on [0,0.5] by
74  * asin(x) = x + x*x^2*R(x^2)
75  * where
76  * R(x^2) is a rational approximation of (asin(x)-x)/x^3
77  * and its remez error is bounded by
78  * |(asin(x)-x)/x^3 - R(x^2)| < 2^(-58.75)
79  *
80  * For x in [0.5,1]
81  * asin(x) = pi/2-2*asin(sqrt((1-x)/2))
82  * Let y = (1-x), z = y/2, s := sqrt(z), and pio2_hi+pio2_lo=pi/2;
83  * then for x>0.98
84  * asin(x) = pi/2 - 2*(s+s*z*R(z))
85  * = pio2_hi - (2*(s+s*z*R(z)) - pio2_lo)
86  *
87  * For x<=0.98, let pio4_hi = pio2_hi/2, then
88  * f = hi part of s;
89  * c = sqrt(z) - f = (z-f*f)/(s+f) ...f+c=sqrt(z)
90  * and
91  * asin(x) = pi/2 - 2*(s+s*z*R(z))
92  * = pio4_hi+(pio4-2s)-(2s*z*R(z)-pio2_lo)
93  * = pio4_hi+(pio4-2f)-(2s*z*R(z)-(pio2_lo+2c))
94  *
95  * Special cases
96  * if x is NaN, return NaN
97  * if |x|>1, return NaN
98  * </pre>
99  */
100 double asin(double x)
101 {
102  double t, w, p, q, c, r, s;
103  int32_t hx, ix;
104  GET_HIGH_WORD(hx, x);
105  ix = hx & 0x7fffffff;
106  if (ix >= 0x3ff00000)
107  { /* |x|>= 1 */
108  uint32_t lx;
109  GET_LOW_WORD(lx, x);
110  if (((ix - 0x3ff00000) | lx) == 0)
111  /* asin(1)=+-pi/2 with inexact */
112  return x * pio2_hi + x * pio2_lo;
113  return NAN; /* asin(|x|>1) is NaN */
114  }
115  else if (ix < 0x3fe00000)
116  { /* |x|<0.5 */
117  if (ix < 0x3e400000)
118  { /* if |x| < 2**-27 */
119  if (huge + x > one)
120  {
121  return x; /* return x with inexact if x!=0*/
122  }
123  else
124  {
125  t = 0;
126  }
127  }
128  else
129  {
130  t = x * x;
131  }
132 
133  p = t * (pS0 + t * (pS1 + t * (pS2 + t * (pS3 + t * (pS4 + t * pS5)))));
134  q = one + t * (qS1 + t * (qS2 + t * (qS3 + t * qS4)));
135  w = p / q;
136  return x + x * w;
137  }
138  /* 1> |x|>= 0.5 */
139  w = one - fabs(x);
140  t = w * 0.5;
141  p = t * (pS0 + t * (pS1 + t * (pS2 + t * (pS3 + t * (pS4 + t * pS5)))));
142  q = one + t * (qS1 + t * (qS2 + t * (qS3 + t * qS4)));
143  s = sqrt(t);
144  if (ix >= 0x3FEF3333)
145  { /* if |x| > 0.975 */
146  w = p / q;
147  t = pio2_hi - (2.0 * (s + s * w) - pio2_lo);
148  }
149  else
150  {
151  w = s;
152  SET_LOW_WORD(w, 0);
153  c = (t - w * w) / (s + w);
154  r = p / q;
155  p = 2.0 * s * r - (pio2_lo - 2.0 * c);
156  q = pio4_hi - 2.0 * w;
157  t = pio4_hi - (p - q);
158  }
159  if (hx > 0)
160  return t;
161  else
162  return -t;
163 }
static const double pS1
Definition: asin.c:56
double sqrt(double x)
Square root function.
Definition: sqrt.c:119
double asin(double x)
Inverse sine function.
Definition: asin.c:100
#define GET_HIGH_WORD(i, d)
Get the more significant 32 bit int from a double.
Definition: prim.h:167
static const double huge
Definition: asin.c:50
static const double pS4
Definition: asin.c:59
static const double pS5
Definition: asin.c:60
static const double pio2_hi
Definition: asin.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 qS1
Definition: asin.c:61
static const double pio4_hi
Definition: asin.c:53
static const double pS0
Definition: asin.c:55
#define NAN
Definition: mathr.h:53
static const double pS2
Definition: asin.c:57
double fabs(double x)
Returns the absolute value of x.
Definition: fabs.c:51
static const double qS4
Definition: asin.c:64
static const double one
Definition: asin.c:49
static const double pS3
Definition: asin.c:58
static const double pio2_lo
Definition: asin.c:52
#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 qS2
Definition: asin.c:62
static const double qS3
Definition: asin.c:63