8

In VB.NET even if both the operands are integer, the / operator will cause the value to be floating point (if the result is non integer).

So I tried with the \ operator which yields integer value irrespective of the operands.
So I thought \ is integer division.

2.5 \ 3 results in 0.

Now I tried 1.5 \ 2. I expected it to be 0 but it resulted in a 1.
Now, is it a bug or a correct result?
What the \ operator actually is?

If it's a bug it exists right through VB6.

GSerg
  • 76,472
  • 17
  • 159
  • 346
techBeginner
  • 3,792
  • 11
  • 43
  • 59

3 Answers3

17

If you use \ on non-integers, you first convert them to integers, which causes rounding: the equivalent of CLng(1.5) \ 2, which is 2 \ 2 or 1.

If you use Option Strict On then you will see this taking place.

user541686
  • 205,094
  • 128
  • 528
  • 886
  • if so, then what would you say on 2.5\3 which is = 0 ?? – techBeginner Aug 26 '11 at 06:05
  • 4
    @dotNETbeginner It uses bankers rounding or "round to even". So it is effectively `2\3 = 0`. See the remarks in [Conversions](http://msdn.microsoft.com/en-us/library/s2dy91zy.aspx): "If the fractional part is exactly 0.5, the integer conversion functions round it to the nearest even integer. For example, 0.5 rounds to 0, and 1.5 and 2.5 both round to 2. This is sometimes called banker's rounding, and its purpose is to compensate for a bias that could accumulate when adding many such numbers together." –  Aug 26 '11 at 06:10
  • @dotNETbeginner: Because 2.5 is rounded to 2, due to *Banker's rounding* (rounding to the nearest even integer, to help cancel out some changes that come in along the way). – user541686 Aug 26 '11 at 06:10
  • They use bankers rounding, where values ending in .5 are rounded to the nearest even number. This prevents the bias of always rounding .5 up. This is the default rounding mode used in IEEE 754 computing functions and operators. – Hand-E-Food Aug 26 '11 at 06:14
  • 3
    +1 for `Option Strict On` which I did not know about and will probably use from now on, being a rigor fanatic. – Jean-François Corbett Aug 26 '11 at 07:34
  • 2
    @Jean-François Corbett: You can actually turn it on globally, instead of doing it individually for every file: go to Tools->Projects and Solutions->VB Defaults, and turn it on. Heck, turn *all* of them on! If you do, then you'll get the maximum rigor included with type inference, to make life easier. – user541686 Aug 26 '11 at 07:49
5

See the Remarks-Section in the Documentation:

Before performing the division, Visual Basic attempts to convert any floating-point numeric expression to Long. ... The conversion to Long is also subject to banker's rounding.

That means 1.5 \ 2 becomes 2 / 2 which is 1.

Banker's rounding (from Type Conversion Functions):

If the fractional part is exactly 0.5, the integer conversion functions round it to the nearest even integer. For example, 0.5 rounds to 0, and 1.5 and 2.5 both round to 2. This is sometimes called banker's rounding, and its purpose is to compensate for a bias that could accumulate when adding many such numbers together.

vstm
  • 12,407
  • 1
  • 51
  • 47
2

not a bug, but simply the result is rounded to the nearest whole number. The operator / is used to make a division between numbers and integers float or double, not forgetting also the Decimal type.

Regards.