Visual Basic 2010 provides some arithmetic operators, listed in Table 4.11.
The first three operators are self-explanatory, so I would like to focus on the other ones. First, an important consideration should be done on the division operators. As shown in Table 4.11, Visual Basic offers two symbols, the slash (/) and backslash (). The first one can be used in divisions between floating point numbers (such as Double
and Single
types), and the second can be used only in divisions between integer numbers. This baclslash is fast when working with integers and truncates the result in case it is a floating point number. Basically, the backslash accepts and returns just integers. To understand this concept, consider the following division between Doubles
:
'Division between double: returns 2.5
Dim dblResult As Double = 10 / 4
The result of such calculation is 2.5. Now consider the following one:
'Division between integers: returns 2
Dim intResult As Integer = 10 4
The result of this calculation is 2. This is because the operator truncated the result, due to its integer nature. If you try to use such operators in a division involving floating point numbers, the Visual Basic compiler throws an exception which is useful for avoiding subtle errors. By the way, such an exception occurs only with Option Strict On
, which you should always set as your default choice.
The integer division operator supports the following data types: SByte, Byte, Short, UShort, Integer, UInteger, Long, and ULong, that is, all numeric types that do not support a floating point.
For divisions between floating point numbers, it’s worth mentioning that divisions between Single
and Double
are also allowed but this causes the compiler to perform some implicit conversions that should be avoided. In such situations, you should just perform an explicit conversion, as in the following code:
The next interesting operator is the exponentiation operator. A simple example follows:
Dim result As Double = 2 ^ 4 'returns 16
The exponentiation operator returns a Double
value. Because of this, even if operands are other types (such as Integer
or Long
), they will be always converted to Double
. Behind the scenes, the ^
operator invokes the Pow
method exposed by the System.Math
class. So you could also rewrite the preceding line of code as follows:
Dim result As Double = System.Math.Pow(2,4) 'returns 16
The last built-in operator is Mod
(which stands for Modulus) that returns the remainder of a division between numbers. The following lines of code show an example:
A typical usage of Mod
is for determining if a number is an odd number. To accomplish this, you could create a function like the following:
If the remainder is different from zero, the number is odd and therefore returns True
. Mod
supports all numeric types, including unsigned types and floating point ones. The .NET Framework offers another way for retrieving the remainder of a division, which is the System.Math.IEEERemainnder
method that works as follows:
'Double remainder
Dim dblRemainder As Double = System.Math.IEEERemainder(10.42, 5.12)
Although both Mod
and IEEERemainder
return the remainder of a division between numbers, they use different formulas behind the scenes and therefore the result may differ. According to the MSDN documentation, this is the formula for the IEEERemainder
method:
IEEERemainder = dividend - (divisor * Math.Round(dividend / divisor))
This is instead the formula for the Modulus operator:
You can see how calculations work differently, especially where Modulus gets the absolute value for dividend and divisor.
System.Math
ClassThis section provides an overview of the arithmetic operators built into the Visual Basic 2010 programming language. The System.Math class provides lots of additional methods for performing complex calculations but this is beyond the scope here.
3.147.79.45