泰勒级数实现asin(x)结果错误,请求技术排查建议
泰勒级数实现
asin(x)的问题排查 我在实现基于泰勒级数的asin(x)方法时遇到错误,无法定位问题所在。本次实现采用的泰勒展开式为:[对应图片链接的展开式]。
编辑说明:按照@Sam Varshavchik的建议移除了asin_x = -asin_x;取反操作后,方法已可正常工作。
实现代码
Decimal Decimal::get_asin() const { /* Perform a Taylor Series estimation of the natural logarithm of this number. */ Decimal one(ONE); Decimal two(TWO); Decimal x = *this; Decimal asin_x = x; Decimal asin_x_iter = x; Decimal asin_power(3); Decimal num_coef(1); Decimal den_coef(2); Decimal last; // Perform the calculation until the last 'last_iter' Decimal // is the same as the current calculation 'term'. while (last.get_Integer().get_Whole_Number() != asin_x.get_Integer().get_Whole_Number()) { last = asin_x; asin_x_iter *= x; asin_x_iter *= x; asin_x += (one / (den_coef * asin_power)) * (num_coef * asin_x_iter); num_coef *= asin_power; asin_power += one; den_coef *= asin_power; asin_power += one; } return asin_x; }
问题详情
测试输入x = 0.2时,得到结果0.19869011034924140647463691595020,而正确结果应为0.20135792079033079145512555221762。我已查阅StackExchange及其他相关论坛,但同类问题多采用阶乘、幂函数或内置函数实现,而我仅能使用标准数学运算符及取反操作,未找到明确解答。
内容的提问来源于stack exchange,提问作者StormCrow
相关产品推荐
相关产品推荐

