一个极其隐秘只有...吧 关注:2,376贴子:11,097
  • 4回复贴,共1

求arctan的算法

只看楼主收藏回复

自己试过泰勒展开、二分法,都没有math库自带的atan快,所以很好奇它怎么算的...
历史是由胜利者书写的。——普莱斯


1楼2013-01-31 16:42回复
    百度一下只有一篇文章是有用的
    http://blog.163.com/shikang999@126/blog/static/1726248962012426103454943/
    C语言代码:
    #include<stdio.h>
    #include<math.h>
    #include<time.h>
    int times=0,tot=0;
    double arctan(double x)
    {
    times++;
    if (x<0)
    {return -arctan(-x);}
    else if(x<0.25)
    {
    double s=0,n,t1=1,t2,t3,t4;
    t2=x;
    t3=x*x;
    for(n=1;;n+=2)
    {
    tot++;
    t4=t1*t2/n;
    s+=t4;
    if(t4>-0.00000000000000001 && t4<0.00000000000000001)
    break;//允许误差
    t1*=-1;
    t2*=t3;
    }
    return s;
    }
    else if(x<0.75)
    {return 0.46364760900080609+arctan((x-0.5)/(1+x*0.5));}
    else if(x<1)
    {return 0.64350110879328437+arctan((x-0.75)/(1+x*0.75));}


    2楼2013-01-31 16:43
    回复
      else if(x<2)
      {return 0.98279372324732905+arctan((x-1.5)/(1+x*1.5));}
      else
      {return 1.32581766366803260+arctan((x-4)/(1+x*4));}
      }
      int main()
      {
      //freopen("out.txt","w",stdout);
      double x;
      scanf("%lf",&x);
      printf("递归次数:%d 计算次数:%d\n atan=%.17lf\narctan=%.17lf\n"
      ,times,tot,atan(x),arctan(x));

      double t;
      int i;
      t=clock();
      for(i=0;i<5000000;i++)
      atan(998);
      printf("500W次atan耗时 %lfs\n",((double)clock()-t)/CLOCKS_PER_SEC);
      t=clock();
      for(i=0;i<5000000;i++)
      arctan(998);
      printf("500W次arctan耗时%lfs\n",((double)clock()-t)/CLOCKS_PER_SEC);

      return 0;
      }
      老头们宣布开战, 但是战斗和死亡的都是年轻人。——赫伯特·胡夫


      3楼2013-01-31 16:43
      回复
        结果

        = =虽然还是没有atan强大,但是比直接用泰勒展开快了数倍(我用x=1测试的),而且不会溢出
        把那4个数字换一下应该能更快
        命运这种东西是能简单改变的!——前原圭一


        4楼2013-01-31 16:47
        回复