-2

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

Mkoll
  • 1
  • 3
  • 1
    How can you tell that it stores only 7 digits precision? – yiding Jan 16 '13 at 11:27
  • You should include the part where you print out numbers, or are you looking in the debugger only? – Mario Jan 16 '13 at 11:30
  • It's pointless asking us to guess at this. If you want meaningful help you should show a program that exhibits behaviour that you don't understand. Then we can explain it. – David Heffernan Jan 16 '13 at 12:01

2 Answers2

4

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.

Yakov Galka
  • 70,775
  • 16
  • 139
  • 220
  • Thanks for your answer but the precision error is in the actual calculation rather than the display/result. – Mkoll Jan 16 '13 at 11:59
  • @Mkoll: if you tried the above, post the expected output and the output you got. Preferably post a [SSCCE](http://sscce.org/). – Yakov Galka Jan 16 '13 at 12:06
  • Hey ybungalobill, have added an example of the results and a small program of the problem. Thanks! – Mkoll Jan 16 '13 at 12:50
1

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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Thanks for your answer but the precision error is in the actual calculation rather than the display/result. – Mkoll Jan 16 '13 at 12:00
  • @Mkoll If that is so, prove it. Produce a program that shows that. Until you can do so, there's not a lot we can do. – David Heffernan Jan 16 '13 at 12:02
  • Have edited the original post to include a small program to produce the error. Cheers – Mkoll Jan 16 '13 at 12:49
  • Please can you explain why you expect different answers than produced by that program. Add to the question actual output and expected output. – David Heffernan Jan 16 '13 at 13:20
  • Hey david, I calculated the expected output in excel using the same input. Have annotated actual and expected output in the post. – Mkoll Jan 16 '13 at 13:25
  • I've updated my answer. Your two versions of code differ. – David Heffernan Jan 16 '13 at 13:43
  • Hi David, sorry that was my mistake, the original results come from MySQL and those were the ones I had pasted not the excel ones. I have corrected this. They both use exactly the same formula. The difference is much more noticable further along the compounding. The only reason I can think of this is because intCC is being calculated to single precision. Thanks for your patience!! – Mkoll Jan 16 '13 at 14:02
  • Why don't you check things on your side rather than trying to blame something else? Your C++ program works to double precision. So, look elsewhere for an explanation. Why can't you accept and admit that your expected output does not match the code. The code outputs `dailycompoundval[1]` as the first non-zero value. But your expected output is different. Why can't you understand that? – David Heffernan Jan 16 '13 at 14:07
  • Thanks for your help David; I know my expected output does not match the code. That is the problem; my code **should** output my expected values. My expected value is `-1.095261` the same as `dailycompoundval[1]` its further along the compounding that is the problem (values 3-5). – Mkoll Jan 16 '13 at 14:27
  • According to the question, your expected value is `-1.09523`. You are very confused. I can't help amidst the confusion. – David Heffernan Jan 16 '13 at 14:31
  • Sorry that was from manually entering the results. Corrected. Still having the same problem :(. Thanks anyway. – Mkoll Jan 16 '13 at 14:39
  • 1
    Your expected values don't match the code. I've no idea how you calculated them. I don't know which is correct. But the problem is in your code and is not in the programs that you are using. You are performing different calculations and so naturally get different answers. This is pretty much a non-question. You are asking why algo A gives different results from algo B, but only show algo A. – David Heffernan Jan 16 '13 at 14:41
  • I did mention in the OP above that both algorithms are the same. I have put the formula in the original post. Same algorithm with same input for the c++ code and excel. However I get different results and I have checked manually and found the c++ results are wrong. – Mkoll Jan 16 '13 at 15:01
  • Well, clearly the algorithms are not the same. If they were then you'd get the same answers. So you should focus your efforts on working out where you two pieces of code differ. As for the expression you just added to the question, I can't see how it matches the code. Your expression has 10000, but the code has 1000. – David Heffernan Jan 16 '13 at 15:08
  • Thankyou! Had re-written this 3 times and missed it every single one. Sorry about the huge amount of hassle – Mkoll Jan 16 '13 at 15:13