我刚刚开始用C++编写代码,但在MATLAB和MySql中有相当多的以前的经验。我正在计算一些复合数字,因此准确性是关键。我试图用双数来做到这一点,但由于某种原因,我只能得到7位有效数字的准确性(与浮点数相同)。我甚至尝试使用一个长双重来尝试计算,但我仍然只得到7 s.f.的精度。Visual Express 2010 double variable precision
我没有正确初始化双打吗?我以为他们只是标准库的一部分?任何帮助不胜感激。下面的代码给出了用于计算的代码的主要部分(其余部分主要是加载数据或调试)。
UPDATE
下面是代码(减去数据读取)的一个样本。我输入了前5个值。计算应该给(预期输出)在Excel计算,使用完全相同的输入:
0
-1.09526
4.364551963
2.745835774
3.029002506
什么下面的代码给出(实际输出):
0
-1.095260000
4.3591394642
2.7340763329
3.0179393198
代码:
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
int main(){
std::vector<double> compoundedcalculation; // pre-allocating for the compounded calculations
std::vector<double> dailycompound; // pre-allocating for daily compoundvalue
compoundedcalculation.insert(compoundedcalculation.end(), 0.0); // setting the first value as 0
double dailycompoundval[] = {0,-1.09526,5.46038,-1.61801,0.283089};
dailycompound.assign(dailycompoundval,dailycompoundval+5);
double indCC;
for (int n = 0; n < 5 ;n++)
{
indCC = ((((1+((compoundedcalculation.at(n))/1000))*(1+((dailycompound.at(n))/1000)))-1)*1000);
printf(" %.17g \n", indCC);
compoundedcalculation.insert(compoundedcalculation.end(), indCC);
}
return 0;
}
感谢您的努力。
更新2:
预期和实效使用相同的公式为复合。
混配总=((1+(每日房价/ 10000))*(1+(前复配合计/ 10000)))
每日房价:
第一天:0 第二天:-1.09526 第3天:5.46038 第4天:-1.61801 第5天:0.283089
你怎么知道它只存储7位数的精度? – yiding
您应该在打印数字的位置包含该部分,还是只在调试器中查找? – Mario
这是毫无意义的要求我们猜测。如果你想得到有意义的帮助,你应该展示一个展现你不明白的行为的程序。然后我们可以解释它。 –