I have just started out coding in c++ but have quite a bit of previous experience with MATLAB and MySql. I am trying to calculation some compounding numbers hence accuracy is key. I have tried to do this using double numbers but for some reason I only ever get an accuracy of 7 significant figures (the same as a float). I have even tried using a long double to try the calculations but I still only get 7 s.f. of precision.
Have I not initialised doubles correctly? I thought they were just part of the standard library?? Any help greatly appreciated. The code below gives the main parts of the code in use for the calcuation (the rest is mainly loading data or debug).
UPDATE
Here is a sample of the code (minus data reading). I've input the first 5 values. The calculations should give (EXPECTED OUTPUT) Calculated in Excel, using exactly the same input:
0
-1.09526
4.364551963
2.745835774
3.029002506
What the code below gives (ACTUAL OUTPUT):
0
-1.095260000
4.3591394642
2.7340763329
3.0179393198
Code:
#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;
}
Thanks for the effort.
UPDATE 2:
Both Expected and Actual Results use the same formula for compounding.
Compounded Total = ((1+(Daily Rate/10000))*(1+(Previous Compounded Total/10000)))
The Daily Rates are:
1st day: 0 2nd day: -1.09526 3rd day: 5.46038 4th day: -1.61801 5th day: 0.283089