📄 sin.c
字号:
/****************************************************************************/
/* sin v2.24 */
/* Copyright (c) 1995-2002 Texas Instruments Incorporated */
/****************************************************************************/
#include <math.h>
#include <values.h>
/****************************************************************************/
/* SIN() - sine */
/* */
/* Based on the algorithm from "Software Manual for the Elementary */
/* Functions", Cody and Waite, Prentice Hall 1980, chapter 8. */
/* */
/* N = round(x / PI) */
/* f = x - N * PI */
/* g = f * f */
/* R = polynomial expansion */
/* */
/* result = f + f * R */
/* */
/* if x < 0, result = - result */
/* if N is even, result = - result */
/* */
/* This will return the wrong result for x >= MAXINT * PI */
/****************************************************************************/
double sin(double x)
{
double xn, f, g, rg, result;
int sign = (x < 0);
int n;
x = fabs(x);
n = (int) ((x * INVSPI) + 0.5);
xn = (double) n;
/*************************************************************************/
/* if n is odd, negate the sign */
/*************************************************************************/
sign ^= n % 2;
/*************************************************************************/
/* f = x - xn * PI (but mathematically more stable) */
/*************************************************************************/
f = (x - xn * C1) - xn * C2;
/*************************************************************************/
/* determine polynomial expression */
/*************************************************************************/
g = f * f;
#if BITS<=24
rg = (((R4 * g + R3) * g + R2) * g + R1) * g;
#elif BITS>=25 && BITS<=32
rg = ((((R5 * g + R4) * g + R3) * g + R2) * g + R1) * g;
#elif BITS>=33 && BITS<=50
rg = ((((((R7*g+R6)*g+R5)*g+R4)*g+R3)*g+R2)*g+R1)*g;
#else
rg = (((((((R8*g+R7)*g+R6)*g+R5)*g+R4)*g+R3)*g+R2)*g+R1)*g;
#endif
result = f + f * rg;
return sign ? -result : result;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -