问题描述
#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
函数昂贵.
It turns out that calculating tan
using division is even more expensive than original tan
function.
有什么方法可以使用sin/cos查找表值来计算tan
而不进行除法运算,因为除法在我的MCU上很昂贵.
Is there any way to calculate tan
using sin/cos lookup table values without division operation, since division is expensive on my MCU.
拥有tan
LUT并使用tan/sin或tan/cos提取结果更好吗?
Is it better to have tan
LUT and extract result using tan/sin or tan/cos as it's done now for sin/cos?
推荐答案
尤其是在微控制器中,y/x除法通常可以通过log& exp表或使用迭代乘法,直到分母1 +-eps = 1 +-x ^(2 ^ n)为零.
In microcontrollers especially, the division y/x can often be optimized either by log & exp tables, or with iterative multiplications, until the denominator 1 +- eps = 1 +- x^(2^n) is zero.
y/X = y / (1-x)
= (1+x)y / (1+x)(1-x)
= (1+x)(1+x^2)y / (1-x^2)(1+x^2)
= (1+x)(1+x^2)(1+x^4)y / (1-x^4)(1+x^4)
这篇关于计算具有sin/cos LUT的棕褐色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!