Red
Red

Reputation: 49

Integer give wrong division

i am coding this school assignement that says to convert a number into a text

Ex : 20 should be viewed in the programme like this ==> twenty

and i am having this problem

Dim num,y As Integer

 num = 26

 y = num / 10

the result of this is 2.6 but since its integer.

the result that should be stock in y

is " 2 " but instead it stocks "3"

i am didnt have any problems with C++

please can someone explain the problems plus the way of how to fix it thank you in advance

Upvotes: 0

Views: 418

Answers (1)

Steve
Steve

Reputation: 216343

From MSDN VB.Net Operators

Integer division is carried out using the \ Operator (Visual Basic). Integer division returns the quotient, that is, the integer that represents the number of times the divisor can divide into the dividend without consideration of any remainder. Both the divisor and the dividend must be integral types (SByte, Byte, Short, UShort, Integer, UInteger, Long, and ULong) for this operator. All other types must be converted to an integral type first.

While / Operator

Divides two numbers and returns a floating-point result. Before division is performed, any integral numeric expressions are widened to Double. If you assign the result to an integral data type, Visual Basic attempts to convert the result from Double to that type.

So

y = num / 10  = 3 
y = num \ 10  = 2

Looking at the IL assembly of

Sub Main
    Dim num as Integer = 26
    Dim result as integer  = num / 10
    Console.WriteLine(result)
End Sub

IL_0000:  ldc.i4.s    1A 
IL_0002:  stloc.0     // num
IL_0003:  ldloc.0     // num
IL_0004:  conv.r8     
IL_0005:  ldc.r8      00 00 00 00 00 00 24 40 
IL_000E:  div         
IL_000F:  call        System.Math.Round
IL_0014:  conv.ovf.i4 
IL_0015:  stloc.1     // result
IL_0016:  ldloc.1     // result
IL_0017:  call        System.Console.WriteLine

You could easily spot the call to System.Math.Round and the conversion of the returning value of the floating point division to an integer

Upvotes: 2

Related Questions