#define PARTPERDEGREE 1
double mysinlut[PARTPERDEGREE * 90 + 1];
double mycoslut[PARTPERDEGREE * 90 + 1];
void MySinCosCreate()
{
int i;
double angle, angleinc;
// Each degree also divided into 10 parts
angleinc = (M_PI / 180) / PARTPERDEGREE;
for (i = 0, angle = 0.0; i <= (PARTPERDEGREE * 90 + 1); ++i, angle += angleinc)
{
mysinlut[i] = sin(angle);
}
angleinc = (M_PI / 180) / PARTPERDEGREE;
for (i = 0, angle = 0.0; i <= (PARTPERDEGREE * 90 + 1); ++i, angle += angleinc)
{
mycoslut[i] = cos(angle);
}
}
double MySin(double rad)
{
int ix;
int sign = 1;
double angleinc = (M_PI / 180) / PARTPERDEGREE;
if(rad > (M_PI / 2))
rad = M_PI / 2 - (rad - M_PI / 2);
if(rad < -(M_PI / 2))
rad = -M_PI / 2 - (rad + M_PI / 2);
if(rad < 0)
{
sign = -1;
rad *= -1;
}
ix = (rad * 180) / M_PI * PARTPERDEGREE;
double h = rad - ix*angleinc;
return sign*(mysinlut[ix] + h*mycoslut[ix]);
}
double MyCos(double rad)
{
int ix;
int sign = 1;
double angleinc = (M_PI / 180) / PARTPERDEGREE;
if(rad > M_PI / 2)
{
rad = M_PI / 2 - (rad - M_PI / 2);
sign = -1;
}
else if(rad < -(M_PI / 2))
{
rad = M_PI / 2 + (rad + M_PI / 2);
sign = -1;
}
else if(rad > -M_PI / 2 && rad < M_PI / 2)
{
rad = abs(rad);
sign = 1;
}
ix = (rad * 180) / M_PI * PARTPERDEGREE;
double h = rad - ix*angleinc;
return sign*(mycoslut[ix] - h*mysinlut[ix]);
}
double MyTan(double rad)
{
return MySin(rad) / MyCos(rad);
}
事实证明,使用除法计算比原始函数tan
更昂贵。tan
有没有什么方法可以在tan
没有除法运算的情况下使用 sin/cos 查找表值来计算,因为除法在我的 MCU 上很昂贵。
使用 tan/sin 或 tan/cos 使用 LUT 并提取结果是否更好,tan
就像现在对 sin/cos 所做的那样?