Export (0) Print
Expand All

^ Operator (Visual Basic)

Raises a number to the power of another number.

number ^ exponent

number

Required. Any numeric expression.

exponent

Required. Any numeric expression.

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

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

Visual Basic always performs exponentiation in the Double Data Type (Visual Basic).

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.

NoteNote

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 (Visual Basic).

The following example uses the ^ operator to raise a number to the power of an exponent. The result is the first operand raised to the power of the second.


Dim exp1, exp2, exp3, exp4, exp5, exp6 As Double
exp1 = 2 ^ 2
exp2 = 3 ^ 3 ^ 3
exp3 = (-5) ^ 3
exp4 = (-5) ^ 4
exp5 = 8 ^ (1.0 / 3.0)
exp6 = 8 ^ (-1.0 / 3.0)


The preceding example produces the following results:

exp1 is set to 4 (2 squared).

exp2 is set to 19683 (3 cubed, then that value cubed).

exp3 is set to -125 (-5 cubed).

exp4 is set to 625 (-5 to the fourth power).

exp5 is set to 2 (cube root of 8).

exp6 is set to 0.5 (1.0 divided by the cube root of 8).

Note the importance of the parentheses in the expressions in the preceding example. Because of operator precedence, Visual Basic normally performs the ^ operator before any others, even the unary operator. If exp4 and exp6 had been calculated without parentheses, they would have produced the following results:

exp4 = -5 ^ 4 would be calculated as –(5 to the fourth power), which would result in -625.

exp6 = 8 ^ -1.0 / 3.0 would be calculated as (8 to the –1 power, or 0.125) divided by 3.0, which would result in 0.041666666666666666666666666666667.

Community Additions

ADD
Show:
© 2014 Microsoft