Teis Draiby said:
In .NET, can I be sure that the result of a division between two integers
always is truncated rather that rounded to nearest?
Example:
99 / 50 = 1
regards, Teis
Um, I would have to disagree with those that say yes.
I haven't taken it to the IL or assembly level, but I believe as written the
values will be cast to doubles, divided, then the result rounded to get your
Integer. Even if that is not exactly what happens, you will NOT get 1.
The key here is the operator you are using ( / ) this is a floating point
division operator.
What you will want to employ is the often overlooked ( \ ) operator, which
is an integer division operator.
Aside from being much faster then ( / ), you should get your desired
results.
Put this into a function and step through it, monitoring the values of each
variable:
Dim intA, intB As Integer
Dim dblA, dblB As Double
Dim int1, int2, int3, int4 As Integer
Dim dbl1, dbl2, dbl3, dbl4 As Double
intA = 99
intB = 50
dblA = 99.0#
dblB = 50.0#
int1 = intA / intB '=2
int2 = intA \ intB '=1
int3 = dblA / dblB '=2
int4 = dblA \ dblB '=1
dbl1 = intA / intB '=1.98
dbl2 = intA \ intB '=1.0
dbl3 = dblA / dblB '=1.98
dbl4 = dblA \ dblB '=1.0
Gerald