Calculate tan having sin/cos LUT

做~自己de王妃 提交于 2019-12-25 18:16:06

问题


#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);
}

It turns out that calculating tan using division is even more expensive than original tan function.

Is there any way to calculate tan using sin/cos lookup table values without division operation, since division is expensive on my MCU.

Is it better to have tan LUT and extract result using tan/sin or tan/cos as it's done now for sin/cos?


回答1:


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)


来源:https://stackoverflow.com/questions/13765457/calculate-tan-having-sin-cos-lut

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!