[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Math/pow [Back]  [Original]

Math.pow() - JavaScript | MDN

Dieser Inhalt wurde automatisch aus dem Englischen bersetzt, und kann Fehler enthalten. Erfahre mehr ber dieses Experiment.

View in English Always switch to English

Math.pow()

Baseline Weitgehend verfgbar

Diese Funktion ist gut etabliert und funktioniert auf vielen Gerten und in vielen Browserversionen. Sie ist seit Juli 2015 browserbergreifend verfgbar.

Die statische Methode Math.pow() gibt den Wert einer Basis zurck, die mit einer Potenz potenziert wird. Das bedeutet

.(,)=xy\mathtt{\operatorname{Math.pow}(x, y)}} = x^y

In diesem Artikel

Probieren Sie es aus

console.log(Math.pow(7, 3));
// Expected output: 343

console.log(Math.pow(4, 0.5));
// Expected output: 2

console.log(Math.pow(7, -2));
// Expected output: 0.02040816326530612
//                  (1/49)

console.log(Math.pow(-7, 0.5));
// Expected output: NaN

Syntax

js
Math.pow(base, exponent)

Parameter

base

Die Basiszahl.

exponent

Die Exponentenzahl.

Rckgabewert

Eine Zahl, die die Basis (base) in der Potenz des Exponenten darstellt. Gibt NaN in einem der folgenden Flle zurck:

  • exponent ist NaN.
  • base ist NaN und exponent ist nicht 0.
  • base ist 1 und exponent ist Infinity.
  • base < 0 und exponent ist keine ganze Zahl.

Beschreibung

Math.pow() ist quivalent zum **-Operator, auer dass Math.pow() nur Zahlen akzeptiert.

Math.pow(NaN, 0) (und das quivalente NaN ** 0) ist der einzige Fall, in dem NaN nicht durch mathematische Operationen propagiert wird es gibt 1 zurck, obwohl der Operand NaN ist. Darber hinaus ist das Verhalten, wenn die Basis 1 ist und der Exponent nicht endlich ist (Infinity oder NaN) anders als bei IEEE 754, das angibt, dass das Ergebnis 1 sein sollte, whrend JavaScript NaN zurckgibt, um die Rckwrtskompatibilitt mit seinem ursprnglichen Verhalten zu bewahren.

Da pow() eine statische Methode von Math ist, verwenden Sie es als Math.pow(), anstatt als Methode eines von Ihnen erstellten Math-Objekts (Math ist kein Konstruktor).

Beispiele

Verwendung von Math.pow()

js
// Basic 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

Spezifikationen

Spezifikation
ECMAScript 2027 LanguageSpecification
# sec-math.pow

Browser-Kompatibilitt

Siehe auch


Web Proxy Viewer  |  New URL  |  Original Page