 output.to from Sideway
Draft for Information Only

# Content

``` VB.NET Arithmetic Operators  ^ Operator  Syntax  Parts  Result  Supported Types  Remarks  See also  * Operator    Syntax  Parts  Result  Supported Types  Remarks  Overloading  See also  / Operator  Syntax  Parts  Supported Types  Result  Remarks  Attempted Division by Zero  See also  \ Operator  Syntax  Parts  Supported Types  Result  Remarks  Attempted Division by Zero  See also  Mod operator  Syntax  Parts  Supported types  Result  Remarks  Attempted division by zero  Equivalent formula  Floating-point imprecision  Overloading  See also  + Operator  Syntax  Parts  Result  Supported Types  Remarks  Overloading  See also  - Operator  Syntax  Parts  Result  Supported Types  Remarks  See also  Source/Reference```

# VB.NET Arithmetic Operators

The supporting VB.NET Arithmetic Operators are ^, *, /, \, Mod, + (unary and binary), - (unary and binary).

## ^ Operator

Exponentiation-Operator. Raises a number to the power of another number.

### Syntax

```number ^ exponent
```

### Parts

number: Required. Any numeric expression.

exponent: Required. Any numeric expression.

### Result

The result is number raised to the power of exponent, always as a Double value.

### Supported Types

Double. Operands of any different type are converted to Double.

### Remarks

Visual Basic always performs exponentiation in the Double Data Type.

The value of exponent can be fractional, negative, or both.

When more than one exponentiation is performed in a single expression, the ^ operator is evaluated as it is encountered from left to right.

Note

The ^ operator can be overloaded, which means that a class or structure can redefine its behavior when an operand has the type of that class or structure. If your code uses this operator on such a class or structure, be sure you understand its redefined behavior. For more information, see Operator Procedures.

## * Operator

Multiplication operator. Multiplies two numbers.

### Syntax

```number1 * number2
```

### Parts

number1: Required. Any numeric expression.

number2: Required. Any numeric expression.

### Result

The result is the product of number1 and number2.

### Supported Types

All numeric types, including the unsigned and floating-point types and Decimal.

### Remarks

The data type of the result depends on the types of the operands. The following table shows how the data type of the result is determined.

Operand data types Result data type
Both expressions are integral data types (SByte, Byte, Short, UShort, Integer, UInteger, Long, ULong) A numeric data type appropriate for the data types of number1 and number2. See the "Integer Arithmetic" tables in Data Types of Operator Results.
Both expressions are Decimal Decimal
Both expressions are Single Single
Either expression is a floating-point data type (Single or Double) but not both Single (note Decimal is not a floating-point data type) Double

If an expression evaluates to Nothing, it is treated as zero.

The * operator can be overloaded, which means that a class or structure can redefine its behavior when an operand has the type of that class or structure. If your code uses this operator on such a class or structure, be sure you understand its redefined behavior. For more information, see Operator Procedures.

## / Operator

Floating-point-division-operator. Divides two numbers and returns a floating-point result.

### Syntax

```expression1 / expression2
```

### Parts

expression1: Required. Any numeric expression.

expression2: Required. Any numeric expression.

### Supported Types

All numeric types, including the unsigned and floating-point types and Decimal.

### Result

The result is the full quotient of expression1 divided by expression2, including any remainder.

The \ Operator (Visual Basic) returns the integer quotient, which drops the remainder.

### Remarks

The data type of the result depends on the types of the operands. The following table shows how the data type of the result is determined.

Operand data types Result data type
Both expressions are integral data types (SByte, Byte, Short, UShort, Integer, UInteger, Long, ULong) Double
One expression is a Single data type and the other is not a Double Single
One expression is a Decimal data type and the other is not a Single or a Double Decimal
Either expression is a Double data type Double

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. This can throw an exception if the result does not fit in that type. In particular, see "Attempted Division by Zero" on this Help page.

If expression1 or expression2 evaluates to Nothing, it is treated as zero.

### Attempted Division by Zero

If expression2 evaluates to zero, the / operator behaves differently for different operand data types. The following table shows the possible behaviors.

Operand data types Behavior if expression2 is zero
Floating-point (Single or Double) Returns infinity (PositiveInfinity or NegativeInfinity), or NaN (not a number) if expression1 is also zero
Decimal Throws DivideByZeroException
Integral (signed or unsigned) Attempted conversion back to integral type throws OverflowException because integral types cannot accept PositiveInfinity, NegativeInfinity, or NaN

Note

The / operator can be overloaded, which means that a class or structure can redefine its behavior when an operand has the type of that class or structure. If your code uses this operator on such a class or structure, be sure you understand its redefined behavior. For more information, see Operator Procedures.

## \ Operator

Integer-division operator. Divides two numbers and returns an integer result.

### Syntax

```expression1 \ expression2
```

### Parts

expression1: Required. Any numeric expression.

expression2: Required. Any numeric expression.

### Supported Types

All numeric types, including the unsigned and floating-point types and Decimal.

### Result

The result is the integer quotient of expression1 divided by expression2, which discards any remainder and retains only the integer portion. This is known as truncation.

The result data type is a numeric type appropriate for the data types of expression1 and expression2. See the "Integer Arithmetic" tables in Data Types of Operator Results.

The / Operator (Visual Basic) returns the full quotient, which retains the remainder in the fractional portion.

### Remarks

Before performing the division, Visual Basic attempts to convert any floating-point numeric expression to Long. If Option Strict is On, a compiler error occurs. If Option Strict is Off, an OverflowException is possible if the value is outside the range of the Long Data Type. The conversion to Long is also subject to banker's rounding. For more information, see "Fractional Parts" in Type Conversion Functions.

If expression1 or expression2 evaluates to Nothing, it is treated as zero.

### Attempted Division by Zero

If expression2 evaluates to zero, the \ operator throws a DivideByZeroException exception. This is true for all numeric data types of the operands.

Note

The \ operator can be overloaded, which means that a class or structure can redefine its behavior when an operand has the type of that class or structure. If your code uses this operator on such a class or structure, be sure you understand its redefined behavior. For more information, see Operator Procedures.

## Mod operator

Divides two numbers and returns only the remainder.

### Syntax

```number1 Mod number2
```

### Parts

number1: Required. Any numeric expression.

number2: Required. Any numeric expression.

### Supported types

All numeric types. This includes the unsigned and floating-point types and Decimal.

### Result

The result is the remainder after number1 is divided by number2. For example, the expression 14 Mod 4 evaluates to 2.

Note

There is a difference between remainder and modulus in mathematics, with different results for negative numbers. The Mod operator in Visual Basic, the .NET Framework op_Modulus operator, and the underlying rem IL instruction all perform a remainder operation.

The result of a Mod operation retains the sign of the dividend, number1, and so it may be positive or negative. The result is always in the range (-number2, number2), exclusive.

### Remarks

If either number1 or number2 is a floating-point value, the floating-point remainder of the division is returned. The data type of the result is the smallest data type that can hold all possible values that result from division with the data types of number1 and number2.

If number1 or number2 evaluates to Nothing, it is treated as zero.

Related operators include the following:

• The \ Operator (Visual Basic) returns the integer quotient of a division. For example, the expression 14 \ 4 evaluates to 3.

• The / Operator (Visual Basic) returns the full quotient, including the remainder, as a floating-point number. For example, the expression 14 / 4 evaluates to 3.5.

### Attempted division by zero

If number2 evaluates to zero, the behavior of the Mod operator depends on the data type of the operands. An integral division throws a DivideByZeroException exception. A floating-point division returns NaN.

### Equivalent formula

The expression a Mod b is equivalent to either of the following formulas:

a - (b * (a \ b))

a - (b * Fix(a / b))

### Floating-point imprecision

When you work with floating-point numbers, remember that they do not always have a precise decimal representation in memory. This can lead to unexpected results from certain operations, such as value comparison and the Mod operator. For more information, see Troubleshooting Data Types.

The Mod operator can be overloaded, which means that a class or structure can redefine its behavior. If your code applies Mod to an instance of a class or structure that includes such an overload, be sure you understand its redefined behavior. For more information, see Operator Procedures.

## + Operator

Addition operator. Adds two numbers or returns the positive value of a numeric expression. Can also be used to concatenate two string expressions.

### Syntax

```expression1 + expression2
- or -
+ expression1
```

### Parts

expression1: Required. Any numeric or string expression.

expression2: Required unless the + operator is calculating a negative value. Any numeric or string expression.

### Result

If expression1 and expression2 are both numeric, the result is their arithmetic sum.

If expression2 is absent, the + operator is the unary identity operator for the unchanged value of an expression. In this sense, the operation consists of retaining the sign of expression1, so the result is negative if expression1 is negative.

If expression1 and expression2 are both strings, the result is the concatenation of their values.

If expression1 and expression2 are of mixed types, the action taken depends on their types, their contents, and the setting of the Option Strict Statement. For more information, see the tables in "Remarks."

### Supported Types

All numeric types, including the unsigned and floating-point types and Decimal, and String.

### Remarks

In general, + performs arithmetic addition when possible, and concatenates only when both expressions are strings.

If neither expression is an Object, Visual Basic takes the following actions.

Data types of expressions Action by compiler
Both expressions are numeric data types (SByte, Byte, Short, UShort, Integer, UInteger, Long, ULong, Decimal, Single, or Double) Add. The result data type is a numeric type appropriate for the data types of expression1 and expression2. See the "Integer Arithmetic" tables in Data Types of Operator Results.
Both expressions are of type String Concatenate.
One expression is a numeric data type and the other is a string If Option Strict is On, then generate a compiler error.

If Option Strict is Off, then implicitly convert the String to Double and add.

If the String cannot be converted to Double, then throw an InvalidCastException exception.
One expression is a numeric data type, and the other is Nothing Add, with Nothing valued as zero.
One expression is a string, and the other is Nothing Concatenate, with Nothing valued as "".

If one expression is an Object expression, Visual Basic takes the following actions.

Data types of expressions Action by compiler
Object expression holds a numeric value and the other is a numeric data type If Option Strict is On, then generate a compiler error.

If Option Strict is Off, then add.
Object expression holds a numeric value and the other is of type String If Option Strict is On, then generate a compiler error.

If Option Strict is Off, then implicitly convert the String to Double and add.

If the String cannot be converted to Double, then throw an InvalidCastException exception.
Object expression holds a string and the other is a numeric data type If Option Strict is On, then generate a compiler error.

If Option Strict is Off, then implicitly convert the string Object to Double and add.

If the string Object cannot be converted to Double, then throw an InvalidCastException exception.
Object expression holds a string and the other is of type String If Option Strict is On, then generate a compiler error.

If Option Strict is Off, then implicitly convert Object to String and concatenate.

If both expressions are Object expressions, Visual Basic takes the following actions (Option Strict Off only).

Data types of expressions Action by compiler
Both Object expressions hold numeric values Add.
Both Object expressions are of type String Concatenate.
One Object expression holds a numeric value and the other holds a string Implicitly convert the string Object to Double and add.

If the string Object cannot be converted to a numeric value, then throw an InvalidCastException exception.

If either Object expression evaluates to Nothing or DBNull, the + operator treats it as a String with a value of "".

Note

When you use the + operator, you might not be able to determine whether addition or string concatenation will occur. Use the & operator for concatenation to eliminate ambiguity and to provide self-documenting code.

The + operator can be overloaded, which means that a class or structure can redefine its behavior when an operand has the type of that class or structure. If your code uses this operator on such a class or structure, be sure you understand its redefined behavior. For more information, see Operator Procedures.

## - Operator

Subtraction operator. Returns the difference between two numeric expressions or the negative value of a numeric expression.

### Syntax

```      expression1 – expression2
- or -
– expression1
```

### Parts

expression1: Required. Any numeric expression.

expression2: Required unless the – operator is calculating a negative value. Any numeric expression.

### Result

The result is the difference between expression1 and expression2, or the negated value of expression1.

The result data type is a numeric type appropriate for the data types of expression1 and expression2. See the "Integer Arithmetic" tables in Data Types of Operator Results.

### Supported Types

All numeric types. This includes the unsigned and floating-point types and Decimal.

### Remarks

In the first usage shown in the syntax shown previously, the – operator is the binary arithmetic subtraction operator for the difference between two numeric expressions.

In the second usage shown in the syntax shown previously, the – operator is the unary negation operator for the negative value of an expression. In this sense, the negation consists of reversing the sign of expression1 so that the result is positive if expression1 is negative.

If either expression evaluates to Nothing, the – operator treats it as zero.

Note

The – operator can be overloaded, which means that a class or structure can redefine its behavior when an operand has the type of that class or structure. If your code uses this operator on such a class or structure, make sure that you understand its redefined behavior. For more information, see Operator Procedures.

## Source/Reference

ID: 200700031 Last Updated: 31/7/2020 Revision: 0 Ref: References

1. Active Server Pages,  , http://msdn.microsoft.com/en-us/library/aa286483.aspx
2. ASP Overview,  , http://msdn.microsoft.com/en-us/library/ms524929%28v=vs.90%29.aspx
3. ASP Best Practices,  , http://technet.microsoft.com/en-us/library/cc939157.aspx
4. ASP Built-in Objects,  , http://msdn.microsoft.com/en-us/library/ie/ms524716(v=vs.90).aspx
5. Response Object,  , http://msdn.microsoft.com/en-us/library/ms525405(v=vs.90).aspx
6. Request Object,  , http://msdn.microsoft.com/en-us/library/ms524948(v=vs.90).aspx
7. Server Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms525541(v=vs.90).aspx
8. Application Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms525360(v=vs.90).aspx
9. Session Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms524319(8v=vs.90).aspx
10. ASPError Object,  , http://msdn.microsoft.com/en-us/library/ms524942(v=vs.90).aspx
11. ObjectContext Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms525667(v=vs.90).aspx
12. Debugging Global.asa Files,  , http://msdn.microsoft.com/en-us/library/aa291249(v=vs.71).aspx
13. How to: Debug Global.asa files,  , http://msdn.microsoft.com/en-us/library/ms241868(v=vs.80).aspx
14. Calling COM Components from ASP Pages,  , http://msdn.microsoft.com/en-us/library/ms524620(v=VS.90).aspx
15. IIS ASP Scripting Reference,  , http://msdn.microsoft.com/en-us/library/ms524664(v=vs.90).aspx
16. ASP Keywords,  , http://msdn.microsoft.com/en-us/library/ms524672(v=vs.90).aspx
17. Creating Simple ASP Pages,  , http://msdn.microsoft.com/en-us/library/ms524741(v=vs.90).aspx
18. Including Files in ASP Applications,  , http://msdn.microsoft.com/en-us/library/ms524876(v=vs.90).aspx
19. ASP Overview,  , http://msdn.microsoft.com/en-us/library/ms524929(v=vs.90).aspx
20. FileSystemObject Object,  , http://msdn.microsoft.com/en-us/library/z9ty6h50(v=vs.84).aspx
21. http://msdn.microsoft.com/en-us/library/windows/desktop/ms675944(v=vs.85).aspx,  , ADO Object Model  Home 5

Management

HBR 3

Information

Recreation

Culture

Chinese 1097

English 337

Computer

Hardware 149

Software

Application 198

Manim 113

Numeric 19

Programming

Web 283

Unicode 494

HTML 65

CSS 58

ASP.NET 145

OS 389

Python 19

Knowledge

Mathematics

Algebra 25

Geometry 21

Calculus 67

Engineering

Mechanical

Rigid Bodies

Statics 92

Dynamics 37

Control

Physics

Electric 27