📄 sincordic.c
字号:
#include "headDefine.h"
double sinCordic(double theta) {
int i; // for iteration
int N = 8; // loop times
// 7 -> 0.01的误差;8->可以收敛;11 -> 0.001的误差
double X[21] = {0.607253, 0}; // 11 = N + 1
double Y[21] = {0};
double result = 0; // the return result
double precision = 0.00001; // the pricision of the radian
int thetaQuadrant = 0; // the quadrant of theta
int S; //
// get the (theta % (2 * PI)), [0, 2*PI]
int quotient = floor(theta / (2 * PI));
theta = theta - 2 * PI * quotient;
// turn to the range: [-PI, PI]
//if (theta > PI && theta <= 2 * PI) {
// theta = theta - 2 * PI;
//}
// judge the quadrant of theta, and transfer theta to the first quadrant
if (theta >= 0 && theta <= PI/2) { // 第一象限
theta = theta;
thetaQuadrant = 1;
}
else if (theta > PI/2 && theta <= PI) { // 第二象限
theta = PI - theta;
thetaQuadrant = 2;
}
else if (theta >= PI && theta < PI / 2 * 3) { // 第三象限
theta = theta - PI;
thetaQuadrant = 3;
}
else if (theta >= PI / 2 * 3 && theta < 2 * PI) { // 第四象限
theta = 2 * PI - theta;
thetaQuadrant = 4;
}
if (theta == 0 || theta == PI/6 || (theta >= 0.785397 && theta <= 0.785398) || theta == PI/3 || theta == PI/2) {
if (theta == 0)
result = 0;
else if (theta == PI/6)
result = 0.5000;
else if (theta >= 0.785397 && theta <= 0.785398)
result = 0.70710678118655;
else if (theta == PI/3)
result = 0.86602540378444;
else if (theta == PI/2)
result = 1;
if (thetaQuadrant == 1 || thetaQuadrant == 2)
result = result;
else
result = -result;
}
else {
// main iterations
for (i = 0; i < N; i++) {
// get the sign of S
// that is the clockwise or anticlockwise rotation
if (theta > precision)
S = 1;
else if (theta < -precision)
S = -1;
else {
break;
}
X[i + 1] = X[i] - S * Y[i] / pow(2, i);
Y[i + 1] = Y[i] + S * X[i] / pow(2, i);
theta = theta - S * atan(1 / pow(2, i));
}
if (thetaQuadrant == 1 || thetaQuadrant == 2)
result = Y[i];
else
result = -Y[i];
}
return(result);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -