Possible Duplicate:
Division in C++ not working as expected
Turns out my program has been returning wrong results, so I decided to break the code into little pieces. After setting breakpoint, turns out that…
double test3 = ((2 - 1) / 2);
…equals 0 according to C++ compiler. I have no idea why. Can someone explain it to me?
I’m using MS Visual Studio Premium 2012
Because you are doing integer division.
1/2is0, which is then converted todouble, yielding0.. If you want floating point division, try making one of the arguments of the division a floating point number: