diff options
Diffstat (limited to 'files/zh-tw/web/javascript/reference/global_objects/math/index.html')
-rw-r--r-- | files/zh-tw/web/javascript/reference/global_objects/math/index.html | 196 |
1 files changed, 196 insertions, 0 deletions
diff --git a/files/zh-tw/web/javascript/reference/global_objects/math/index.html b/files/zh-tw/web/javascript/reference/global_objects/math/index.html new file mode 100644 index 0000000000..c67999150e --- /dev/null +++ b/files/zh-tw/web/javascript/reference/global_objects/math/index.html @@ -0,0 +1,196 @@ +--- +title: Math +slug: Web/JavaScript/Reference/Global_Objects/Math +tags: + - JavaScript + - Math + - NeedsTranslation + - Reference + - TopicStub +translation_of: Web/JavaScript/Reference/Global_Objects/Math +--- +<div>{{JSRef}}</div> + +<p><strong><code>Math</code></strong> 是一個擁有數學常數及數學函數(非函式物件)屬性及方法的內建物件。</p> + +<h2 id="描述">描述</h2> + +<p>不像其他的全域物件,<code>Math</code> 並非建構函式。所有 <code>Math</code> 的屬性及方法皆為靜態。你可以使用 <code>Math.PI</code> 來參考到圓周率 pi 的常數值,以及可以呼叫 <code>Math.sin(x)</code> 函式來計算三角函數正弦曲線 sine(<code>x</code> 為方法的引數)。常數是由 JavaScript 中實數的完整精度來定義。</p> + +<h2 id="屬性">屬性</h2> + +<dl> + <dt>{{jsxref("Math.E")}}</dt> + <dd>歐拉常數 (此指自然常數) ,也是自然對數的底數,約為2.718。</dd> + <dt>{{jsxref("Math.LN2")}}</dt> + <dd>2 的自然對數,約為0.693。</dd> + <dt>{{jsxref("Math.LN10")}}</dt> + <dd>10 的自然對數,約為2.303。</dd> + <dt>{{jsxref("Math.LOG2E")}}</dt> + <dd>以 2 為底的 E 的對數,約為1.443。</dd> + <dt>{{jsxref("Math.LOG10E")}}</dt> + <dd>以 10 為底的 E 的對數,約為0.434。</dd> + <dt>{{jsxref("Math.PI")}}</dt> + <dd>一個圓的圓周和其直徑比值,約為 3.14159。</dd> + <dt>{{jsxref("Math.SQRT1_2")}}</dt> + <dd>1/2的平方根;也就是1除以2的平方根,約為 0.707。</dd> + <dt>{{jsxref("Math.SQRT2")}}</dt> + <dd>2的平方根,約為 1.414。</dd> +</dl> + +<h2 id="方法">方法</h2> + +<div class="note"> +<p>注意三角函數 <span style="font-size: 1rem; letter-spacing: -0.00278rem;">(</span><code style="font-style: normal; letter-spacing: -0.00278rem;">sin()</code><span style="font-size: 1rem; letter-spacing: -0.00278rem;">, </span><code style="font-style: normal; letter-spacing: -0.00278rem;">cos()</code><span style="font-size: 1rem; letter-spacing: -0.00278rem;">, </span><code style="font-style: normal; letter-spacing: -0.00278rem;">tan()</code><span style="font-size: 1rem; letter-spacing: -0.00278rem;">, </span><code style="font-style: normal; letter-spacing: -0.00278rem;">asin()</code><span style="font-size: 1rem; letter-spacing: -0.00278rem;">, </span><code style="font-style: normal; letter-spacing: -0.00278rem;">acos()</code><span style="font-size: 1rem; letter-spacing: -0.00278rem;">, </span><code style="font-style: normal; letter-spacing: -0.00278rem;">atan()</code><span style="font-size: 1rem; letter-spacing: -0.00278rem;">, </span><code style="font-style: normal; letter-spacing: -0.00278rem;">atan2()</code><span style="font-size: 1rem; letter-spacing: -0.00278rem;">) 的參數或回傳值的角度皆以弧度為單位。把角度乘上 </span><code style="font-style: normal; letter-spacing: -0.00278rem;">(Math.PI / 180)</code> 會得到弧度單位,將弧度除以該數則會轉換回一般所用的角度單位。</p> +</div> + +<div class="note"> +<p>注意許多數學方法的精度是取決於實作方式的。這意味著不同的瀏覽器可能會得到不同的結果,甚至同一個 JS引擎在不同的作業系統或架構上所得到的結果都有可能相異。</p> +</div> + +<dl> + <dt>{{jsxref("Global_Objects/Math/abs", "Math.abs(x)")}}</dt> + <dd>回傳 x 的絕對值。</dd> + <dt>{{jsxref("Global_Objects/Math/acos", "Math.acos(x)")}}</dt> + <dd>回傳 x 的反餘弦值。</dd> + <dt>{{jsxref("Global_Objects/Math/acosh", "Math.acosh(x)")}}</dt> + <dd>Returns the hyperbolic arccosine of a number.</dd> + <dt>{{jsxref("Global_Objects/Math/asin", "Math.asin(x)")}}</dt> + <dd>回傳 x 的反正弦值。</dd> + <dt>{{jsxref("Global_Objects/Math/asinh", "Math.asinh(x)")}}</dt> + <dd>Returns the hyperbolic arcsine of a number.</dd> + <dt>{{jsxref("Global_Objects/Math/atan", "Math.atan(x)")}}</dt> + <dd>回傳 x 的反正切值。</dd> + <dt>{{jsxref("Global_Objects/Math/atanh", "Math.atanh(x)")}}</dt> + <dd>Returns the hyperbolic arctangent of a number.</dd> + <dt>{{jsxref("Global_Objects/Math/atan2", "Math.atan2(y, x)")}}</dt> + <dd>Returns the arctangent of the quotient of its arguments.</dd> + <dt>{{jsxref("Global_Objects/Math/cbrt", "Math.cbrt(x)")}}</dt> + <dd>回傳 x 的立方根值。</dd> + <dt>{{jsxref("Global_Objects/Math/ceil", "Math.ceil(x)")}}</dt> + <dd>回傳不小於 x 的最小整數值。</dd> + <dt>{{jsxref("Global_Objects/Math/clz32", "Math.clz32(x)")}}</dt> + <dd>Returns the number of leading zeroes of a 32-bit integer.</dd> + <dt>{{jsxref("Global_Objects/Math/cos", "Math.cos(x)")}}</dt> + <dd>回傳 x 的餘弦值。</dd> + <dt>{{jsxref("Global_Objects/Math/cosh", "Math.cosh(x)")}}</dt> + <dd>Returns the hyperbolic cosine of a number.</dd> + <dt>{{jsxref("Global_Objects/Math/exp", "Math.exp(x)")}}</dt> + <dd>回傳 E<sup>x</sup>,x 為給定數值,E 為歐拉常數 (自然對數的底數)。</dd> + <dt>{{jsxref("Global_Objects/Math/expm1", "Math.expm1(x)")}}</dt> + <dd>回傳 <code>exp(x)</code> 減去1的值。</dd> + <dt>{{jsxref("Global_Objects/Math/floor", "Math.floor(x)")}}</dt> + <dd>回傳不大於 x 的最大整數值。</dd> + <dt>{{jsxref("Global_Objects/Math/fround", "Math.fround(x)")}}</dt> + <dd>Returns the nearest <a href="http://en.wikipedia.org/wiki/Single-precision_floating-point_format" title="link to the wikipedia page on single precision">single precision</a> float representation of a number.</dd> + <dt>{{jsxref("Global_Objects/Math/hypot", "Math.hypot([x[, y[, …]]])")}}</dt> + <dd>回傳參數平方之和的平方根。</dd> + <dt>{{jsxref("Global_Objects/Math/imul", "Math.imul(x, y)")}}</dt> + <dd>Returns the result of a 32-bit integer multiplication.</dd> + <dt>{{jsxref("Global_Objects/Math/log", "Math.log(x)")}}</dt> + <dd>回傳 x 的自然對數值。</dd> + <dt>{{jsxref("Global_Objects/Math/log1p", "Math.log1p(x)")}}</dt> + <dd>回傳 <code>1 + x</code> 的自然對數值。</dd> + <dt>{{jsxref("Global_Objects/Math/log10", "Math.log10(x)")}}</dt> + <dd>回傳以 10 為底,x 的對數值。</dd> + <dt>{{jsxref("Global_Objects/Math/log2", "Math.log2(x)")}}</dt> + <dd>回傳以 2 為底,x 的對數值。</dd> + <dt>{{jsxref("Global_Objects/Math/max", "Math.max([x[, y[, …]]])")}}</dt> + <dd>回傳給定數值中的最大值。</dd> + <dt>{{jsxref("Global_Objects/Math/min", "Math.min([x[, y[, …]]])")}}</dt> + <dd>回傳給定數值中的最小值。</dd> + <dt>{{jsxref("Global_Objects/Math/pow", "Math.pow(x, y)")}}</dt> + <dd>回傳 x 的 y 次方,也就是 <code>x<sup>y</sup></code>。</dd> + <dt>{{jsxref("Global_Objects/Math/random", "Math.random()")}}</dt> + <dd>回傳一個 0 到 1 之間的偽隨機值。</dd> + <dt>{{jsxref("Global_Objects/Math/round", "Math.round(x)")}}</dt> + <dd>回傳 x 的四捨五入值。</dd> + <dt>{{jsxref("Global_Objects/Math/sign", "Math.sign(x)")}}</dt> + <dd>回傳 x 的正負號,也就是回傳 x 的正負。</dd> + <dt>{{jsxref("Global_Objects/Math/sin", "Math.sin(x)")}}</dt> + <dd>回傳 x 的正弦值。</dd> + <dt>{{jsxref("Global_Objects/Math/sinh", "Math.sinh(x)")}}</dt> + <dd>Returns the hyperbolic sine of a number.</dd> + <dt>{{jsxref("Global_Objects/Math/sqrt", "Math.sqrt(x)")}}</dt> + <dd>回傳 x 的正平方根。</dd> + <dt>{{jsxref("Global_Objects/Math/tan", "Math.tan(x)")}}</dt> + <dd>回傳 x 的正切值。</dd> + <dt>{{jsxref("Global_Objects/Math/tanh", "Math.tanh(x)")}}</dt> + <dd>Returns the hyperbolic tangent of a number.</dd> + <dt><code>Math.toSource()</code> {{non-standard_inline}}</dt> + <dd>回傳字串 <code>"Math"</code>。</dd> + <dt>{{jsxref("Global_Objects/Math/trunc", "Math.trunc(x)")}}</dt> + <dd>Returns the integral part of the number x, removing any fractional digits.</dd> +</dl> + +<h2 id="擴充_Math_物件">擴充 <code>Math</code> 物件</h2> + +<p>As most of the built-in objects in JavaScript, the <code>Math</code> object can be extended with custom properties and methods. To extend the <code>Math</code> object, you do not use 'prototype'. Instead, you directly extend <code>Math</code>:</p> + +<pre>Math.propName = propValue; +Math.methodName = methodRef;</pre> + +<p>For instance, the following example adds a method to the <code>Math</code> object for calculating the <em>greatest common divisor</em> of a list of arguments.</p> + +<pre class="brush: js">/* Variadic function -- Returns the greatest common divisor of a list of arguments */ +Math.gcd = function() { + if (arguments.length == 2) { + if (arguments[1] == 0) + return arguments[0]; + else + return Math.gcd(arguments[1], arguments[0] % arguments[1]); + } else if (arguments.length > 2) { + var result = Math.gcd(arguments[0], arguments[1]); + for (var i = 2; i < arguments.length; i++) + result = Math.gcd(result, arguments[i]); + return result; + } +};</pre> + +<p>Try it:</p> + +<pre class="brush: js">console.log(Math.gcd(20, 30, 15, 70, 40)); // `5`</pre> + +<h2 id="規範">規範</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('ES1')}}</td> + <td>{{Spec2('ES1')}}</td> + <td>Initial definition. Implemented in JavaScript 1.1.</td> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-15.8', 'Math')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-math-object', 'Math')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>New methods {{jsxref("Math.log10()", "log10()")}}, {{jsxref("Math.log2()", "log2()")}}, {{jsxref("Math.log1p()", "log1p()")}}, {{jsxref("Math.expm1()", "expm1()")}}, {{jsxref("Math.cosh()", "cosh()")}}, {{jsxref("Math.sinh()", "sinh()")}}, {{jsxref("Math.tanh()", "tanh()")}}, {{jsxref("Math.acosh()", "acosh()")}}, {{jsxref("Math.asinh()", "asinh()")}}, {{jsxref("Math.atanh()", "atanh()")}}, {{jsxref("Math.hypot()", "hypot()")}}, {{jsxref("Math.trunc()", "trunc()")}}, {{jsxref("Math.sign()", "sign()")}}, {{jsxref("Math.imul()", "imul()")}}, {{jsxref("Math.fround()", "fround()")}}, {{jsxref("Math.cbrt()", "cbrt()")}} and {{jsxref("Math.clz32()", "clz32()")}} added.</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-math-object', 'Math')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="瀏覽器相容性">瀏覽器相容性</h2> + +<p class="hidden">The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</p> + +<p>{{Compat("javascript.builtins.Math")}}</p> + +<h2 id="參見">參見</h2> + +<ul> + <li>{{jsxref("Number")}}</li> +</ul> |