Math.pow()

Math.pow() 静态方法返回基数的幂值。那是

¥The Math.pow() static method returns the value of a base raised to a power. That is

𝙼𝚊𝚝𝚑.𝚙𝚘𝚠 ( 𝚡 , 𝚢 ) = x y \mathtt{\operatorname{Math.pow}(x, y)} = x^y

Try it

语法

¥Syntax

js
Math.pow(base, exponent)

参数

¥Parameters

base

基数。

exponent

指数数。

返回值

¥Return value

代表 baseexponent 次方的数字。在以下情况之一返回 NaN

¥A number representing base taken to the power of exponent. Returns NaN in one of the following cases:

  • exponentNaN
  • baseNaNexponent 不是 0
  • base 为±1,exponent 为±Infinity
  • base < 0exponent 不是整数。

描述

¥Description

Math.pow() 相当于 ** 运算符,只不过 Math.pow() 只接受数字。

¥Math.pow() is equivalent to the ** operator, except Math.pow() only accepts numbers.

Math.pow(NaN, 0)(以及等效的 NaN ** 0)是 NaN 不通过数学运算传播的唯一情况 - 尽管操作数是 NaN,它仍返回 1。此外,base 为 1 且 exponent 为非有限(±Infinity 或 NaN)的行为与 IEEE 754 不同,IEEE 754 指定结果应为 1,而 JavaScript 返回 NaN 以保持与其原始行为的向后兼容性。

¥Math.pow(NaN, 0) (and the equivalent 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.

因为 pow()Math 的静态方法,所以将其用作 Math.pow(),而不是用作你创建的 Math 对象的方法(Math 不是构造函数)。

¥Because pow() is a static method of Math, use it as Math.pow(), rather than as a method of a Math object you created (Math is not a constructor).

示例

¥Examples

使用 Math.pow()

¥Using Math.pow()

js
// Simple cases
Math.pow(7, 2); // 49
Math.pow(7, 3); // 343
Math.pow(2, 10); // 1024

// Fractional exponents
Math.pow(4, 0.5); // 2 (square root of 4)
Math.pow(8, 1 / 3); // 2 (cube root of 8)
Math.pow(2, 0.5); // 1.4142135623730951 (square root of 2)
Math.pow(2, 1 / 3); // 1.2599210498948732 (cube root of 2)

// Signed exponents
Math.pow(7, -2); // 0.02040816326530612 (1/49)
Math.pow(8, -1 / 3); // 0.5

// Signed bases
Math.pow(-7, 2); // 49 (squares are positive)
Math.pow(-7, 3); // -343 (cubes can be negative)
Math.pow(-7, 0.5); // NaN (negative numbers don't have a real square root)
// Due to "even" and "odd" roots laying close to each other,
// and limits in the floating number precision,
// negative bases with fractional exponents always return NaN,
// even when the mathematical result is real
Math.pow(-7, 1 / 3); // NaN

// Zero and infinity
Math.pow(0, 0); // 1 (anything ** ±0 is 1)
Math.pow(Infinity, 0.1); // Infinity (positive exponent)
Math.pow(Infinity, -1); // 0 (negative exponent)
Math.pow(-Infinity, 1); // -Infinity (positive odd integer exponent)
Math.pow(-Infinity, 1.5); // Infinity (positive exponent)
Math.pow(-Infinity, -1); // -0 (negative odd integer exponent)
Math.pow(-Infinity, -1.5); // 0 (negative exponent)
Math.pow(0, 1); // 0 (positive exponent)
Math.pow(0, -1); // Infinity (negative exponent)
Math.pow(-0, 1); // -0 (positive odd integer exponent)
Math.pow(-0, 1.5); // 0 (positive exponent)
Math.pow(-0, -1); // -Infinity (negative odd integer exponent)
Math.pow(-0, -1.5); // Infinity (negative exponent)
Math.pow(0.9, Infinity); // 0
Math.pow(1, Infinity); // NaN
Math.pow(1.1, Infinity); // Infinity
Math.pow(0.9, -Infinity); // Infinity
Math.pow(1, -Infinity); // NaN
Math.pow(1.1, -Infinity); // 0

// NaN: only Math.pow(NaN, 0) does not result in NaN
Math.pow(NaN, 0); // 1
Math.pow(NaN, 1); // NaN
Math.pow(1, NaN); // NaN

规范

Specification
ECMAScript Language Specification
# sec-math.pow

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看