Decimal Handling in C++

Quote from Craig66:

Say you have a software S/L @ 1.2560, if you store this as a double C++ may end up with something like 1.25599999999 internally (for example), when the price gets to 1.2560 the S/L is not detected because 1.2560 is not equal to 1.25599999999.

If you are implementing a stop procedure with an "=" you should be shot, strung up, tarred and feathered.
 
Quote from Random.Capital:

If you are implementing a stop procedure with an "=" you should be shot, strung up, tarred and feathered.

Probably using "n <= 1.2560" or "n >= 1.2560".
 
Quote from Random.Capital:

If you are implementing a stop procedure with an "=" you should be shot, strung up, tarred and feathered.

Like duh...
It's meant to be an illustrative example, not a definitive guide to coding stop losses. So just take a deep breath and relax.
 
Quote from Craig66:

I'm in the process of writing a ATS and I am starting to run across problems concerning rounding errors with standard C++ types (float, double) etc. I was wondering how other people using C++ are handling fixed decimal point representation problems of the kind which occur when handling financial data.

One piece of information that you might already be aware of, but is important if you are not, is that division by integers will always equal integers even if the result is stored in a double.

More often that not, when I think I have a rounding problem it is because of this.

Here is an example of what I mean

int a = 3;
int b = 2;
double c = 0;
c = 3/2; //c has a value of 1, not 1.5

In this situation you must explicitly state that during the calculation you want these variables represented as doubles by casting them:

c = (double)3/(double)2; //c has a value of 1.5

I believe this is caused by the fact that the CPU carries out the calculation using the same registers to store the result as the original value, and since the original value was an int, the answer is an int.

Also note that another data type that is sometimes larger than double, depending on your compiler is a long double
 
Quote from n00b7r4d3r:

One piece of information that you might already be aware of, but is important if you are not, is that division by integers will always equal integers even if the result is stored in a double.

More often that not, when I think I have a rounding problem it is because of this.

Here is an example of what I mean

int a = 3;
int b = 2;
double c = 0;
c = 3/2; //c has a value of 1, not 1.5

In this situation you must explicitly state that during the calculation you want these variables represented as doubles by casting them:

c = (double)3/(double)2; //c has a value of 1.5

I believe this is caused by the fact that the CPU carries out the calculation using the same registers to store the result as the original value, and since the original value was an int, the answer is an int.
:confused: Huh?

This has nothing to do with how the CPU carries out the calculation or how registers are used - this happens because that's the C standard and any other result would mean that the C compiler implementation is badly broken.

One int divided by another int yields an int. If you want to force the calculation and the result to a double you cast it just like you said.
 
Back
Top