amath  1.8.5
Simple command line calculator
cos.c File Reference

Cosine function. More...

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

Go to the source code of this file.

Functions

double cos (double x)
 Cosine function. More...
 

Detailed Description

Cosine function.

Definition in file cos.c.

Function Documentation

◆ cos()

double cos ( double  x)

Cosine function.

Parameters
x
Returns
Cosine function of x
Kernel function:
__kernel_sin        ... sine function on [-pi/4,pi/4]
__kernel_cos        ... cose function on [-pi/4,pi/4]
__ieee754_rem_pio2  ... argument reduction routine
Method:
Let S,C and T denote the sin, cos and tan respectively on
[-PI/4, +PI/4]. Reduce the argument x to y1+y2 = x-k*pi/2
in [-pi/4 , +pi/4], and let n = k mod 4.

We have

     n        sin(x)      cos(x)        tan(x)
----------------------------------------------------------
     0          S           C             T
     1          C          -S           -1/T
     2         -S          -C             T
     3         -C           S           -1/T
----------------------------------------------------------
Special cases:
Let trig be any of sin, cos, or tan.
trig(+-INF)  is NaN
trig(NaN)    is that NaN
Accuracy:
TRIG(x) returns trig(x) nearly rounded

Definition at line 87 of file cos.c.

References __kernel_cos(), __kernel_sin(), and rempio2().

Referenced by cchc(), ccos(), ccosh(), ccot(), ccoth(), ccsc(), ccsch(), cexp(), RealNumber::Cosine(), cot(), cpow(), csc(), csec(), csech(), csin(), csinh(), ctan(), ctanh(), hv(), hvc(), sec(), and vcs().

88 {
89  double y[2], z = 0.0;
90  int32_t n, ix;
91 
92  // High word of x
93  GET_HIGH_WORD(ix, x);
94  ix &= 0x7FFFFFFF;
95 
96  // |x| ~< pi/4
97  if (ix <= 0x3FE921FB)
98  {
99  return __kernel_cos(x, z);
100  }
101 
102  // cos(Inf or NaN) is NaN
103  if (ix >= 0x7FF00000)
104  {
105  return NAN;
106  }
107 
108  // argument reduction needed
109  n = rempio2(x, y);
110  switch (n & 3)
111  {
112  case 0:
113  return __kernel_cos(y[0], y[1]);
114  case 1:
115  return -__kernel_sin(y[0], y[1], 1);
116  case 2:
117  return -__kernel_cos(y[0], y[1]);
118  default:
119  return __kernel_sin(y[0], y[1], 1);
120  }
121 }
#define GET_HIGH_WORD(i, d)
Get the more significant 32 bit int from a double.
Definition: prim.h:167
double __kernel_cos(double x, double y)
Kernel cosine function.
Definition: kcos.c:94
#define NAN
Definition: mathr.h:53
int rempio2(double x, double *y)
Definition: remp2.c:104
double __kernel_sin(double x, double y, int iy)
Kernel sin function.
Definition: ksin.c:89
Here is the call graph for this function:
Here is the caller graph for this function: