Is \ integer division in VB?

In VB.NET, even if both operands are integer, the / operator will call a floating point value (if the result is not an integer).

So I tried with the operator \ which returns an integer value regardless of the operands.
So I thought, \ this is an integer division.

2.5\3 results at 0 .

Now I tried 1.5\2 . I expected this to be 0 but this led to 1 .
Now, is this a mistake or the correct result?
What is the operator \ really?

If this is a mistake, it exists right through VB6.

+7
source share
3 answers

If you use \ for non-integer numbers, 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 , you will see this happening.

+15
source

See the Remarks section in the Documentation section:

Before performing the separation, Visual Basic tries to convert any floating-point numeric expression to Long .... Converting to Long also rounds off bankers.

This means that 1.5 \ 2 becomes 2/2 equal to 1.

Banker rounding (from Type Conversion Function ):

If the fractional part is 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 rounds to 2. This is sometimes called banker rounding, and its purpose is to compensate for the bias that can accumulate when you add many of these numbers together.

+4
source

not an error, but simply the result is rounded to the nearest integer. The / operator is used to divide numbers and integers by float or double, without forgetting also the Decimal type.

Sincerely.

+2
source

Source: https://habr.com/ru/post/895939/


All Articles