Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;:关于计算精度(代码在里面)_C++_Taylor Series - Fatal编程技术网

C++ C++;:关于计算精度(代码在里面)

C++ C++;:关于计算精度(代码在里面),c++,taylor-series,C++,Taylor Series,关于计算指数泰勒级数的精度,你们能给我一些建议吗?我们有一个指数度和一个精度计算图作为输入数据。我们应该接收一个给定精度的计算数作为输出数据。我写了一个程序,但当我计算一个答案并和嵌入函数的答案进行比较时,它有不同之处。你能告诉我,我怎样才能消除答案之间的差异 #包括“stdafx.h” #包括“iostream” #包括 #包括 #包括 使用名称空间std; 整数阶乘(intn); 双泰勒(双x,整数q); int main() { 双res=0; int q=0; 双数=0; 库特数; co

关于计算指数泰勒级数的精度,你们能给我一些建议吗?我们有一个指数度和一个精度计算图作为输入数据。我们应该接收一个给定精度的计算数作为输出数据。我写了一个程序,但当我计算一个答案并和嵌入函数的答案进行比较时,它有不同之处。你能告诉我,我怎样才能消除答案之间的差异

#包括“stdafx.h”
#包括“iostream”
#包括
#包括
#包括
使用名称空间std;
整数阶乘(intn);
双泰勒(双x,整数q);
int main()
{
双res=0;
int q=0;
双数=0;
库特数;
coutq;

你的代码中有两个问题。首先,阶乘很容易溢出。实际上我不知道
int
阶乘何时发生溢出,但我记得在普通袖珍计算器上,例如
x!
已经溢出了
x==70
。你可能不需要那么高的阶乘,但最好还是使用从一开始就将该问题作废。如果您查看每个步骤中需要添加的更正:
x^i/i!
(数学符号)然后你注意到这个值实际上分别比
x^i
i!
小得多。你也可以简单地将它乘以
x/i
来计算上一个值

第二,我不理解你的计算精度。也许它是正确的,但老实说,对我来说,它看起来太复杂了,甚至无法理解;)

以下是获得正确值的方法:

#include <iostream>
#include <cmath>

struct taylor_result {
        int iterations;
        double value;
        taylor_result() : iterations(0),value(0) {}
};

taylor_result taylor(double x,double eps = 1e-8){
    taylor_result res;
    double accu = 1;                           // calculate only the correction
                                               // but not its individual terms
    while(accu > eps){                        
         res.value += accu;
         res.iterations++;
         accu *= (x / (res.iterations));
    }
    return res;
}       

int main() {
    std::cout << taylor(3.0).value << "\n";
    std::cout << exp(3.0) << "\n";
}
#包括
#包括
结构泰勒结果{
整数迭代;
双重价值;
taylor_result():迭代次数(0),值(0){}
};
泰勒结果泰勒(双x,双eps=1e-8){
泰勒结果;
double accu=1;//仅计算校正
//但不是它的个别条款
而(accu>eps){
res.value+=累计值;
res.iterations++;
累计*=(x/(分辨率迭代));
}
返回res;
}       
int main(){

std::cout有什么不同?你想扩展什么函数?我想让Taylor的函数更准确地像嵌入函数一样。例如,你可以尝试输入变量,你会看到答案的不同。你得到的结果是什么,它们与你想要得到的有什么不同?@tryuf至少,修复之前所有的警告或者询问,因为它们可能是您问题的原因:
警告C4715:“Taylor”:并非所有控制路径都返回值
。如果一个结果为39334,另一个结果为148413,则很可能不是精度问题,但更可能是您的结果完全错误
#include <iostream>
#include <cmath>

struct taylor_result {
        int iterations;
        double value;
        taylor_result() : iterations(0),value(0) {}
};

taylor_result taylor(double x,double eps = 1e-8){
    taylor_result res;
    double accu = 1;                           // calculate only the correction
                                               // but not its individual terms
    while(accu > eps){                        
         res.value += accu;
         res.iterations++;
         accu *= (x / (res.iterations));
    }
    return res;
}       

int main() {
    std::cout << taylor(3.0).value << "\n";
    std::cout << exp(3.0) << "\n";
}