【发布时间】:2016-02-04 04:21:13
【问题描述】:
我一直在寻找解决这个问题的方法。我已经看到了许多计算任何 -1
我假设我需要添加或减去 pi 的倍数来抵消它?这种思路正确吗?
目前我有:
double my_atan(double x)
{
return x - (x*x*x)/3 + (x*x*x*x*x)/5;
}
哪个使用taylor series。
对于下面的代码,
int x;
for (x=0; x<M_PI*2; x++)
{
printf("Actual: %f\n", atan(x));
printf("Approx: %f\n", my_atan(x));
printf("\n");
}
它很快失去控制(正如预期的那样,因为它超出了范围):
Actual: 0.000000
Approx: 0.000000
Actual: 0.785398
Approx: 0.866667
Actual: 1.107149
Approx: 5.733333
Actual: 1.249046
Approx: 42.600000
Actual: 1.325818
Approx: 187.466667
Actual: 1.373401
Approx: 588.333333
Actual: 1.405648
Approx: 1489.200000
此处未显示,但当 theta 在适当范围内时,输出相当准确。
所以我的问题是 my_tan 函数究竟需要采取哪些步骤才能支持更广泛的范围?
关注这个已经有一段时间了,所以任何可以提供的指导都将不胜感激
【问题讨论】:
-
查找 CORDIC 以获得有效算法或将泰勒级数实现为相应 Wikipedia 文章中的大纲(它仅在 |x|
-
Taylor 对此很不利,因为收敛速度和收敛半径都很差。有更好的技术:最好通过搜索发现,但我相信 Newton Raphson 效果很好。
-
关键字:cordic atan uio.no/studier/emner/matnat/ifi/INF5430/v12/…
-
对于单精度实现,请参阅this question
-
arctan 函数的域不是角度,而是它的 (-inf,inf)。习惯使用语法 'atan(x) = theta'
标签: c trigonometry pi math.h