The exponentiation operator is right-associative: a ** b ** c is equal to a ** [b ** c].
In most languages, such as PHP, Python, and others that have an exponentiation operator [**], the exponentiation operator is defined to have a higher precedence than unary operators, such as unary + and unary -, but there are a few exceptions. For example, in Bash, the ** operator is defined to have a lower precedence than unary operators.
In JavaScript, it is impossible to write an ambiguous exponentiation expression. That is, you cannot put a unary operator [+/-/~/!/delete/void/typeof] immediately before the base number; doing so will cause a SyntaxError.
For example, -2 ** 2 is 4 in Bash, but is -4 in other languages [such as Python]. This is invalid in JavaScript, as the operation is ambiguous. You have to parenthesize either side — for example, as -[2 ** 2] — to make the intention unambiguous.
Note that some programming languages use the caret symbol ^ for exponentiation, but JavaScript uses that symbol for the bitwise logical XOR operator.
NaN ** 0 [and the equivalent Math.pow[NaN, 0]] is the only case where NaN doesn't propagate through mathematical operations — it returns 1 despite the operand being NaN. In addition, the behavior where base is 1 and exponent is non-finite [±Infinity or NaN] is different from IEEE 754, which specifies that the result should be 1, whereas JavaScript returns NaN to preserve backward compatibility with its original behavior.
Skip to main content
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
^ operator
- Article
- 09/13/2021
- 2 minutes to read
In this article
Used to raise a number to the power of an exponent.
Syntax
result=number^exponent
The ^ operator syntax has these parts:
result | Required; any numeric variable. |
number | Required; any numeric expression. |
exponent | Required; any numeric expression. |
Remarks
A number can be negative only if exponent is an integer value. When more than one exponentiation is performed in a single expression, the ^ operator is evaluated as it is encountered from left to right.
Usually, the data type of result is a Double or a Variant containing a Double. However, if either number or exponent is a Null expression, result is Null.
Example
This example uses the ^ operator to raise a number to the power of an exponent.
Dim MyValue MyValue = 2 ^ 2 ' Returns 4. MyValue = 3 ^ 3 ^ 3 ' Returns 19683. MyValue = [-5] ^ 3 ' Returns -125.Note
For 64-bit users: Because the caret operator is used to create Long Long data types in a 64-bit environment, the VBA IDE may not interpret this operator correctly. To ensure proper interpretation, add a space character immediately before the caret as shown.
x=y^2 ' Will generate "expected ]" from VBA IDE. x=y ^2 ' Will be interpreted as x equals y squared.See also
- Operator summary
Support and feedback
Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.
Skip to main content
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
^ Operator [Visual Basic]
- Article
- 09/15/2021
- 2 minutes to read
In this article
Raises a number to the power of another number.
Syntax
number ^ exponentParts
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.
Example
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.
See also
- ^= Operator
- Arithmetic Operators
- Operator Precedence in Visual Basic
- Operators Listed by Functionality
- Arithmetic Operators in Visual Basic
Feedback
Submit and view feedback for