Math.pow()
Baseline
Widely available
This feature is well established and works across many devices and browser versions. Itâs been available across browsers since 2015å¹´7æ.
Math.pow() ã¯éçã¡ã½ããã§ãåºæ°ãã¹ãä¹ããå¤ãè¿ãã¾ãã
試ãã¦ã¿ã¾ããã
console.log(Math.pow(7, 3));
// äºæ³ãããçµæ: 343
console.log(Math.pow(4, 0.5));
// äºæ³ãããçµæ: 2
console.log(Math.pow(7, -2));
// äºæ³ãããçµæ: 0.02040816326530612
// (1/49)
console.log(Math.pow(-7, 0.5));
// äºæ³ãããçµæ: NaN
æ§æ
Math.pow(base, exponent)
弿°
è¿å¤
base ãè¡¨ãæ°å¤ã exponent ä¹ããå¤ã以ä¸ã®ããããã®å ´åã¯ã NaN ãè¿ãã¾ãã
exponentãNaNã§ãããbaseãNaNã§ãexponentã0以å¤ã§ãããbaseã ±1 ã§ãexponentã ±Infinityã§ãããbase < 0ã§ãexponentãæ´æ°ã§ã¯ãªãã
解説
Math.pow() 㯠** æ¼ç®åã¨åçã§ããã Math.pow() ã¯æ°å¤ã®ã¿ãåãå
¥ããã¨ããç¹ãç°ãªãã¾ãã
Math.pow(NaN, 0)ï¼ããã³åçã® NaN ** 0ï¼ã¯ã NaN ãæ°å¦æ¼ç®ã§ä¼æããªãå¯ä¸ã®ã±ã¼ã¹ã§ããããã¯ããªãã©ã³ãã NaN ã§ããã«ãããããã 1 ãè¿ãã¾ããããã«ã base ã 1 ã§ exponent ãç¡é大ï¼Â±Infinity ã¾ã㯠NaNï¼ã§ããå ´åã®åä½ã¯ãçµæã 1 ã¨ãªããã¨ãè¦å®ãã¦ãã IEEE 754 ã¨ã¯ç°ãªãã JavaScript ã§ã¯å
ã®åä½ã¨ã®å¾æ¹äºææ§ãç¶æããããã« NaN ãè¿ãã¾ãã
pow() 㯠Math ã®éçã¡ã½ãããªã®ã§ã常㫠Math.pow() ã¨ãã¦ä½¿ç¨ããèªåã§ Math ãªãã¸ã§ã¯ããçæãã¦ãã®ã¡ã½ããã¨ãã¦ä½¿ç¨ããªãã§ãã ããã (Math ã«ã¯ã³ã³ã¹ãã©ã¯ã¿ã¼ãããã¾ãã)ã
ä¾
>Math.pow() ã®ä½¿ç¨
// åºæ¬çãªä¾
Math.pow(7, 2); // 49
Math.pow(7, 3); // 343
Math.pow(2, 10); // 1024
// å°æ°ã®ã¹ãä¹
Math.pow(4, 0.5); // 2 (4 ã®å¹³æ¹æ ¹)
Math.pow(8, 1 / 3); // 2 (8 ã®ç«æ¹æ ¹)
Math.pow(2, 0.5); // 1.4142135623730951 (2 ã®å¹³æ¹æ ¹)
Math.pow(2, 1 / 3); // 1.2599210498948732 (2 ã®ç«æ¹æ ¹)
// è² ã®æ°ã®ã¹ãä¹
Math.pow(7, -2); // 0.02040816326530612 (1/49)
Math.pow(8, -1 / 3); // 0.5
// è² ã®æ°ã®åº
Math.pow(-7, 2); // 49 (2 ä¹ã¯æ£ã®æ°)
Math.pow(-7, 3); // -343 (3 ä¹ã¯è² ã®æ°)
Math.pow(-7, 0.5); // NaN (è² ã®æ°ã«ã¯å®æ°ã®å¹³æ¹æ ¹ããªã)
// 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 (ä»»æã®æ° ** ±0 is 1)
Math.pow(Infinity, 0.1); // Infinity (æ£ã®ææ°)
Math.pow(Infinity, -1); // 0 (è² ã®ææ°)
Math.pow(-Infinity, 1); // -Infinity (æ£ã®å¥æ°ã®æ´æ°ã®ææ°)
Math.pow(-Infinity, 1.5); // Infinity (æ£ã®ææ°)
Math.pow(-Infinity, -1); // -0 (è² ã®å¥æ°ã®æ´æ°ã®ææ°)
Math.pow(-Infinity, -1.5); // 0 (è² ã®ææ°)
Math.pow(0, 1); // 0 (æ£ã®ææ°)
Math.pow(0, -1); // Infinity (è² ã®ææ°)
Math.pow(-0, 1); // -0 (æ£ã®å¥æ°ã®æ´æ°ã®ææ°)
Math.pow(-0, 1.5); // 0 (æ£ã®ææ°)
Math.pow(-0, -1); // -Infinity (è² ã®å¥æ°ã®æ´æ°ã®ææ°)
Math.pow(-0, -1.5); // Infinity (è² ã®ææ°)
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® 2027 Language Specification> # sec-math.pow> |