基于裁缝系列https://en.wikipedia.org/wiki/Taylor_series的指数近似代码适用于零附近的输入,但在向任一方向移动更远时完全无用。下面是我的小测试代码的输出,它计算-12到12范围内输入的exp,并打印与std :: exp结果相比的错误,并且边界处的错误很大。例如-12输入的错误大约是148255571469%:
in = -12 error = 148255571469.28%
in = -11.00 error = 18328703925.31%
in = -10.00 error = 2037562880.10%
in = -9.00 error = 199120705.27%
in = -8.00 error = 16588916.06%
in = -7.00 error = 01128519.76%
in = -6.00 error = 00058853.00%
in = -5.00 error = 00002133.29%
in = -4.00 error = 00000045.61%
in = -3.00 error = 00000000.42%
in = -2.00 error = 00000000.00%
in = -1.00 error = 00000000.00%
in = 0.00 error = 00000000.00%
in = 1.00 error = 00000000.00%
in = 2.00 error = 00000000.00%
in = 3.00 error = 00000000.00%
in = 4.00 error = 00000000.03%
in = 5.00 error = 00000000.20%
in = 6.00 error = 00000000.88%
in = 7.00 error = 00000002.70%
in = 8.00 error = 00000006.38%
in = 9.00 error = 00000012.42%
in = 10.00 error = 00000020.84%
in = 11.00 error = 00000031.13%
in = 12.00 error = 00000042.40%
我需要在尽可能大的范围内以误差小于1%的误差进行近似。任何想法如何实现这一目标?
我的小测试代码如下:
#include <cmath>
#include <iostream>
#include <iomanip>
double my_exp(double x) //based on https://en.wikipedia.org/wiki/Exponential_function
{
double res = 1. + x, t = x;
unsigned long factorial = 1;
for (unsigned char i = 2; i <= 12; ++i)
{
t *= x, factorial *= i;
res += t / factorial;
}
return res;
}
int main(int argc, char* argv[])
{
for (double in = -12; in <= 12; in += 1.)
{
auto error = std::abs(my_exp(in) - std::exp(in));
auto percent = error * 100. / std::exp(in);
std::cout << "in = " << in << " error = "
<< std::fixed << std::setw( 11 ) << std::setprecision( 2 )
<< std::setfill( '0' ) << percent << "%" << std::endl;
}
return 0;
}
看似相似问题的解决方案来自&#34;近似e ^ x&#34; Approximate e^x无法解决此问题:
答案 0 :(得分:1)
通过Tailor扩展找到近似值的最简单方法之一是检查收敛速度,只需添加一个新的更高项并确认结果。
这是支持上述想法的我的c ++代码。
#include <iostream>
#include <cmath>
double my_exp(const double x, const double x0 = 0, const double ncut = 1e-3)
{
double res = 1.;
double addterm = (x - x0);
size_t norder = 1;
while(true)
{
double res_update = res + addterm;
if(std::abs(res_update - res)/std::abs(res) < ncut){
break;
}
norder += 1;
addterm *= (x - x0)/norder;
res = res_update;
}
return res;
}
int main(int argc, char* argv[])
{
const double x = std::atof(argv[1]);
const double approxi = my_exp(x);
const double exactResult = std::exp(x);
std::cout<<"approxi : "<< approxi<<std::endl;
std::cout<<"exact : "<< exactResult<<std::endl;
std::cout<<"err: "<< (1 - std::abs(approxi/exactResult))*100 <<std::endl;
return 0;
}