Quote:
Originally posted by Silvy
I'm no C - programmer but maybe the operands for the division need to be doubles for a result to return a double?
dValueResult = dValueWidth / dValueLength
(or perhaps only one of the two)
|
Yes, that's exactly it. It's a common mistake made when doing division in C (one that I've made myself many times
)
Let's say you have something like:
Code:
int nDenominator = 2;
int nNumerator = 3;
double dResult;
dResult = nNumerator / nDenominator;
You would expect the value of
dResult to be 1.5, except that it's not. Instead, it ends up holding 1.0. Why? Because in order for floating point division to work, either one or both the numerator, and denominator in the equation need to be a double (at least in Win32 it does, I didn't have a chance to verify this with any other compiler).
You can fix this code in either one of two ways.
1) Change the variables
nNumerator or
nDenominator to be a double
2) Typecast either one or both of the variables to be a double (i.e. (double) nNumerator / (double) nDenominator ).
In either case, you end up getting the answer 1.5, which is exactly what you should be getting.