amath  1.8.5
Simple command line calculator
acos.c File Reference

Inverse cosine function. More...

#include "prim.h"
Include dependency graph for acos.c:

Go to the source code of this file.

Functions

double acos (double x)
 Inverse cosine function. More...
 

Variables

static const double one = 1.00000000000000000000e+00
 
static const double pi = 3.14159265358979311600e+00
 
static const double pio2_hi = 1.57079632679489655800e+00
 
static const double pio2_lo = 6.12323399573676603587e-17
 
static const double pS0 = 1.66666666666666657415e-01
 
static const double pS1 = -3.25565818622400915405e-01
 
static const double pS2 = 2.01212532134862925881e-01
 
static const double pS3 = -4.00555345006794114027e-02
 
static const double pS4 = 7.91534994289814532176e-04
 
static const double pS5 = 3.47933107596021167570e-05
 
static const double qS1 = -2.40339491173441421878e+00
 
static const double qS2 = 2.02094576023350569471e+00
 
static const double qS3 = -6.88283971605453293030e-01
 
static const double qS4 = 7.70381505559019352791e-02
 

Detailed Description

Inverse cosine function.

Definition in file acos.c.

Function Documentation

◆ acos()

double acos ( double  x)

Inverse cosine function.

Method
    acos(x)  = pi/2 - asin(x)
    acos(-x) = pi/2 + asin(x)
    For |x|<=0.5
    acos(x)  = pi/2 - (x + x*x^2*R(x^2))        (see asin.c)
    For x>0.5
    acos(x)  = pi/2 - (pi/2 - 2asin(sqrt((1-x)/2)))
             = 2asin(sqrt((1-x)/2))
             = 2s + 2s*z*R(z)   ...z=(1-x)/2, s=sqrt(z)
             = 2f + (2c + 2s*z*R(z))
    where f=hi part of s, and c = (z-f*f)/(s+f) is the correction term
    for f so that f+c ~ sqrt(z).
    For x<-0.5
    acos(x)  = pi - 2asin(sqrt((1-|x|)/2))
             = pi - 0.5*(s+s*z*R(z)), where z=(1-|x|)/2,s=sqrt(z)
Special cases
    if x is NaN, return NaN
    if |x|>1, return NaN

Definition at line 92 of file acos.c.

References one, pi, pio2_hi, pio2_lo, pS0, pS1, pS2, pS3, pS4, pS5, qS1, qS2, qS3, qS4, and sqrt().

Referenced by ahvc(), RealNumber::ArcCosine(), asec(), and avcs().

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 }
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 sqrt(double x)
Square root function.
Definition: sqrt.c:119
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
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ one

const double one = 1.00000000000000000000e+00
static

Definition at line 49 of file acos.c.

Referenced by acos().

◆ pi

const double pi = 3.14159265358979311600e+00
static

Definition at line 50 of file acos.c.

Referenced by acos().

◆ pio2_hi

const double pio2_hi = 1.57079632679489655800e+00
static

Definition at line 51 of file acos.c.

Referenced by acos().

◆ pio2_lo

const double pio2_lo = 6.12323399573676603587e-17
static

Definition at line 52 of file acos.c.

Referenced by acos().

◆ pS0

const double pS0 = 1.66666666666666657415e-01
static

Definition at line 53 of file acos.c.

Referenced by acos().

◆ pS1

const double pS1 = -3.25565818622400915405e-01
static

Definition at line 54 of file acos.c.

Referenced by acos().

◆ pS2

const double pS2 = 2.01212532134862925881e-01
static

Definition at line 55 of file acos.c.

Referenced by acos().

◆ pS3

const double pS3 = -4.00555345006794114027e-02
static

Definition at line 56 of file acos.c.

Referenced by acos().

◆ pS4

const double pS4 = 7.91534994289814532176e-04
static

Definition at line 57 of file acos.c.

Referenced by acos().

◆ pS5

const double pS5 = 3.47933107596021167570e-05
static

Definition at line 58 of file acos.c.

Referenced by acos().

◆ qS1

const double qS1 = -2.40339491173441421878e+00
static

Definition at line 59 of file acos.c.

Referenced by acos().

◆ qS2

const double qS2 = 2.02094576023350569471e+00
static

Definition at line 60 of file acos.c.

Referenced by acos().

◆ qS3

const double qS3 = -6.88283971605453293030e-01
static

Definition at line 61 of file acos.c.

Referenced by acos().

◆ qS4

const double qS4 = 7.70381505559019352791e-02
static

Definition at line 62 of file acos.c.

Referenced by acos().