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

有帮助吗?

解决方案 2

In Visual Studio, double is IEEE754 double precison. That has 53 bits of binary precision, or around 15-16 decimal significant figures.

Probably your diagnostic code that is printing the values only prints to 7 digits of precision. Or your debugger view only shows 7 digits of precision.

In other words the problem is not in the underlying data type, but in the way you are viewing that data.

Update 1

Your comments indicate that you believe that calculations on double precision values are being carried out to single precision. By default that will not be the case. It could happen if you have change the floating point precision control with a call to _controlfp. However, if your floating point control is set at the default value, then operations on double precision values will not be rounded to single precision.

Update 2

Your Excel calculations are performing a different calculation. The output from your C++ program matches the code. The first non-zero value output -1.09526 which matches the code. Because the code says that the value should be dailycompoundval[1]. The corresponding value from your Excel code is -1.095231419 which therefore does not match the C++ code.

In other words the question is a red-herring. There's no rounding problems here. The issue is entirely down to discrepancies between the two different versions of your code.

Update 3

Your C++ code does not match the expression in the latest update. The code uses a multiplicative factor of 1000, but your expression uses a factor of 10000.

其他提示

double is IEEE 64-bit float on your machine, so it stores 15-17 significant decimal digits. You do not have to do anything special for this. Your problem is the way you print it to the screen, which you didn't show. By default the values are rounded to 6 significant digits, so you should increase it as follows:

cout.precision(17);
cout << x;

or

printf("%.17g", x);

depending on your output method.

UPDATE: Taking a high precision calculator and doing the calculation manually I get:

n = 0: ((1 + 0/1000)*(1 + 0/1000) - 1)*1000
        == 0
n = 1: ((1 + 0/1000)*(1 + -1.09526/1000) - 1)*1000
        == -1.09526
n = 2: ((1 + -1.09526/1000)*(1 + 5.46038/1000) - 1)*1000
        == 4.3591394642012
n = 3: ((1 + 4.3591394642012/1000)*(1 + -1.61801/1000) - 1)*1000
        == 2.734076332956727816388
n = 4: ((1 + 2.734076332956727816388/1000)*(1 + 0.283089/1000) - 1)*1000
        == 3.017939319891748203508813462532

The same results I get when I run the code:

 0  
 -1.0952599999999999  
 4.3591394642012  
 2.7340763329567279  
 3.0179393198917484  

However, when I replace 1000 by 10000 I get your "expected results",

 0  
 -1.0952599999999999  
 4.3645219464201199  
 2.7458057624046663  
 3.0289724931454134  

which seems to answer your question.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top