1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
---
title: Math.pow()
slug: Web/JavaScript/Reference/Global_Objects/Math/pow
tags:
- 次方
translation_of: Web/JavaScript/Reference/Global_Objects/Math/pow
---
<div>{{JSRef}}</div>
<p><strong><code>Math.pow()</code></strong> 函式回傳 <code>base</code> 的 <code>exponent</code> 次方(幂)值,也就是 <code>base<sup>exponent</sup></code>。</p>
<h2 id="語法">語法</h2>
<pre class="syntaxbox"><code>Math.pow(<var>base</var>, <var>exponent</var>)</code></pre>
<h3 id="參數">參數</h3>
<dl>
<dt><code>base</code></dt>
<dd>基數。</dd>
<dt><code>exponent</code></dt>
<dd>要乘上 <code>base</code> 幾次的指數。</dd>
</dl>
<h3 id="回傳值">回傳值</h3>
<p>A number representing the given base taken to the power of the given exponent.</p>
<h2 id="敘述">敘述</h2>
<p>The <strong><code>Math.pow()</code></strong> function returns the <code>base</code> to the <code>exponent</code> power, that is, <code>base<sup>exponent</sup></code>, the base and the exponent are in decimal numeral system.</p>
<p>由於 <code>pow()</code> 是 <code>Math</code> 的靜態方法,you always use it as <code>Math.pow()</code>, rather than as a method of a <code>Math</code> object you created(<code>Math</code> 並沒有建構子)。</p>
<h2 id="示例">示例</h2>
<h3 id="使用_Math.pow">使用 <code>Math.pow()</code></h3>
<pre class="brush: js">// simple
Math.pow(7, 2); // 49
Math.pow(7, 3); // 343
Math.pow(2, 10); // 1024
// fractional exponents
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 的立方根)
// signed exponents
Math.pow(7, -2); // 0.02040816326530612 (1/49)
Math.pow(8, -1/3); // 0.5
// signed bases
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
Math.pow(-7, 1/3); // NaN
</pre>
<h2 id="規範">規範</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">規範</th>
<th scope="col">狀態</th>
<th scope="col">註解</th>
</tr>
<tr>
<td>{{SpecName('ES1')}}</td>
<td>{{Spec2('ES1')}}</td>
<td>Initial definition. Implemented in JavaScript 1.0.</td>
</tr>
<tr>
<td>{{SpecName('ES5.1', '#sec-15.8.2.13', 'Math.pow')}}</td>
<td>{{Spec2('ES5.1')}}</td>
<td></td>
</tr>
<tr>
<td>{{SpecName('ES6', '#sec-math.pow', 'Math.pow')}}</td>
<td>{{Spec2('ES6')}}</td>
<td></td>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-math.pow', 'Math.pow')}}</td>
<td>{{Spec2('ESDraft')}}</td>
<td></td>
</tr>
</tbody>
</table>
<h2 id="瀏覽器相容性">瀏覽器相容性</h2>
<p>{{Compat("javascript.builtins.Math.pow")}}</p>
<h2 id="參見">參見</h2>
<ul>
<li>{{jsxref("Math.cbrt()")}}</li>
<li>{{jsxref("Math.exp()")}}</li>
<li>{{jsxref("Math.log()")}}</li>
<li>{{jsxref("Math.sqrt()")}}</li>
<li><a href="https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Exponentiation" title="Arithmetic operators take numerical values (either literals or variables) as their operands and return a single numerical value. The standard arithmetic operators are addition (+), subtraction (-), multiplication (*), and division (/).">Exponentiation operator</a> {{experimental_inline}}</li>
</ul>
|