我刚刚开始使用 C++ 进行编码,但之前对 MATLAB 和 MySql 有相当多的经验。我正在尝试计算一些复合数字,因此准确性是关键。我曾尝试使用双数来做到这一点,但由于某种原因,我只能得到 7 位有效数字的准确度(与浮点数相同)。我什至尝试使用 long double 来尝试计算,但我仍然只能得到 7 sf 的精度。
我没有正确初始化双打吗?我以为它们只是标准库的一部分??非常感谢任何帮助。下面的代码给出了用于计算的代码的主要部分(其余部分主要是加载数据或调试)。
更新
这是代码示例(减去数据读取)。我已经输入了前 5 个值。计算应该给出 ( EXPECTED OUTPUT ) 在 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 第 2 天:-1.09526 第 3 天:5.46038 第 4 天:-1.61801 第 5 天:0.283089