Quote:
Originally posted by DNA7433
How come CInt(750 / 100) = CInt(850 / 100)?
They both equal 8...how is that possible? and how would i emulate this weird formula evaluation in C++?
I'm trying to convert some VB code (that works perfectly) to C++ and this is a bug I tracked down. C++ evaluates static_cast<long>(750 / 100) = 7, static_cast<long>(850 / 100) = 8
What you are seeing is actually a result of the VB's use of what is called Banker's rounding. The results of those divisions are of course 7.5 and 8.5 respectivly, but since you are asking VB to convert them to Integers they have to be rounded. Here is VB's explanation of Banker's Rounding
Quote:
When you add rounded values together, always rounding .5 in the same direction results in a bias that grows with the more numbers you add together. One way to minimize the bias is with banker's rounding.
Banker's rounding rounds .5 up sometimes and down sometimes. The convention is to round to the nearest even number, so that both 1.5 and 2.5 round to 2, and 3.5 and 4.5 both round to 4. Banker's rounding is symmetric.
In Visual Basic for Applications, the following numeric functions perform banker's rounding: CByte(), CInt(), CLng(), CCur(), and Round().