^ 运算符 (Visual Basic)^ Operator (Visual Basic)

以一个数字为底、另一数字为幂求值。Raises a number to the power of another number.

语法Syntax

number ^ exponent

组成部分Parts

number
必需。Required. 任何数值表达式。Any numeric expression.

exponent
必需。Required. 任何数值表达式。Any numeric expression.

结果Result

结果会 number 引发的次幂 exponent ,总是作为 Double 值。The result is number raised to the power of exponent, always as a Double value.

支持的类型Supported Types

Double.Double. 任何不同类型的操作数都转换为 DoubleOperands of any different type are converted to Double.

备注Remarks

Visual Basic 总是对Double 数据类型执行幂运算。Visual Basic always performs exponentiation in the Double Data Type.

的值 exponent 可以是小数、负数或同时为两者。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.

备注

^运算符可以重载,这意味着当操作数具有该类或结构的类型时,该类或结构可以重新定义其行为。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. 有关详细信息,请参阅 Operator ProceduresFor 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设置为4(2 squared)。exp1 is set to 4 (2 squared).

exp2设置为19683(3个立方,然后是该值的立方)。exp2 is set to 19683 (3 cubed, then that value cubed).

exp3设置为-125 (-5 的立方)。exp3 is set to -125 (-5 cubed).

exp4设置为625(-5 到第四次幂)。exp4 is set to 625 (-5 to the fourth power).

exp5设置为2(多维数据集的第8个)。exp5 is set to 2 (cube root of 8).

exp6设置为0.5 (1.0 除以8的 cube 根)。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. 由于运算符优先级,Visual Basic 通常在 ^ 任何其他运算符(甚至一元运算符)之前执行运算符 Because of operator precedence, Visual Basic normally performs the ^ operator before any others, even the unary operator. 如果 exp4 和的 exp6 计算结果没有括号,则会生成以下结果:If exp4 and exp6 had been calculated without parentheses, they would have produced the following results:

exp4 = -5 ^ 4将计算为–(5到第四次幂),这会导致-625。exp4 = -5 ^ 4 would be calculated as –(5 to the fourth power), which would result in -625.

exp6 = 8 ^ -1.0 / 3.0将计算为(8到-1 个幂,即0.125)除以3.0,这将导致0.041666666666666666666666666666667。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