Decimal Handling in C++

Discussion in 'Automated Trading' started by Craig66, Aug 19, 2008.

  1. If you are implementing a stop procedure with an "=" you should be shot, strung up, tarred and feathered.
     
    #21     Nov 11, 2008
  2. Probably using "n <= 1.2560" or "n >= 1.2560".
     
    #22     Nov 11, 2008
  3. Craig66

    Craig66

    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.
     
    #23     Nov 12, 2008
  4. 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
     
    #24     Nov 13, 2008
  5. GTS

    GTS

    :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.
     
    #25     Nov 13, 2008