
- CoffeeScript 教程
- CoffeeScript - 首頁
- CoffeeScript - 概述
- CoffeeScript - 環境
- CoffeeScript - 命令列工具
- CoffeeScript - 語法
- CoffeeScript - 資料型別
- CoffeeScript - 變數
- CoffeeScript - 運算子和別名
- CoffeeScript - 條件語句
- CoffeeScript - 迴圈
- CoffeeScript - 列表推導式
- CoffeeScript - 函式
- CoffeeScript 面向物件
- CoffeeScript - 字串
- CoffeeScript - 陣列
- CoffeeScript - 物件
- CoffeeScript - 範圍
- CoffeeScript - 展開運算子
- CoffeeScript - 日期
- CoffeeScript - 數學
- CoffeeScript - 異常處理
- CoffeeScript - 正則表示式
- CoffeeScript - 類和繼承
- CoffeeScript 高階
- CoffeeScript - Ajax
- CoffeeScript - jQuery
- CoffeeScript - MongoDB
- CoffeeScript - SQLite
- CoffeeScript 有用資源
- CoffeeScript - 快速指南
- CoffeeScript - 有用資源
- CoffeeScript - 討論
CoffeeScript - 數學
JavaScript 的Math物件提供用於數學常量和函式的屬性和方法。與其他全域性物件不同,Math不是建構函式。Math的所有屬性和方法都是靜態的,可以使用Math作為物件來呼叫,無需建立它。
因此,您可以將常量pi引用為Math.PI,並將正弦函式呼叫為Math.sin(x),其中x是方法的引數。我們可以在CoffeeScript程式碼中使用JavaScript的Math物件執行數學運算。
數學常量
如果我們想使用任何常見的數學常量,例如pi或e,我們可以使用JavaScript的Math物件。
以下是JavaScript的Math物件提供的Math常量列表:
序號 | 屬性和描述 |
---|---|
1 | E 尤拉常數和自然對數的底數,約為2.718。 |
2 | LN2 2的自然對數,約為0.693。 |
3 | LN10 10的自然對數,約為2.302。 |
4 | LOG2E E的以2為底的對數,約為1.442。 |
5 | LOG10E E的以10為底的對數,約為0.434。 |
6 | PI 圓周長與其直徑的比率,約為3.14159。 |
7 | SQRT1_2 1/2的平方根;等效於2的平方根的倒數,約為0.707。 |
8 | SQRT2
2的平方根,約為1.414。 |
示例
以下示例演示了在CoffeeScript中使用JavaScript提供的數學常量。將此程式碼儲存在名為math_example.coffee的檔案中。
e_value = Math.E console.log "The value of the constant E is: " + e_value LN2_value = Math.LN2 console.log "The value of the constant LN2 is: " + LN2_value LN10_value = Math.LN10 console.log "The value of the constant LN10 is: " + LN10_value LOG2E_value = Math.LOG2E console.log "The value of the constant LOG2E is: " + LOG2E_value LOG10E_value = Math.LOG10E console.log "The value of the constant LOG10E is: " + LOG10E_value PI_value = Math.PI console.log "The value of the constant PI is: " + PI_value SQRT1_2_value = Math.SQRT1_2 console.log "The value of the constant SQRT1_2 is: " + SQRT1_2_value SQRT2_value = Math.SQRT2 console.log "The value of the constant SQRT2 is: " + SQRT2_value
開啟命令提示符並編譯.coffee檔案,如下所示。
c:\> coffee -c math_example.coffee
編譯後,它會提供以下JavaScript程式碼。
// Generated by CoffeeScript 1.10.0 (function() { var LN10_value, LN2_value, LOG10E_value, LOG2E_value, PI_value, SQRT1_2_value, SQRT2_value, e_value; e_value = Math.E; console.log("The value of the constant E is: " + e_value); LN2_value = Math.LN2; console.log("The value of the constant LN2 is: " + LN2_value); LN10_value = Math.LN10; console.log("The value of the constant LN10 is: " + LN10_value); LOG2E_value = Math.LOG2E; console.log("The value of the constant LOG2E is: " + LOG2E_value); LOG10E_value = Math.LOG10E; console.log("The value of the constant LOG10E is: " + LOG10E_value); PI_value = Math.PI; console.log("The value of the constant PI is: " + PI_value); SQRT1_2_value = Math.SQRT1_2; console.log("The value of the constant SQRT1_2 is: " + SQRT1_2_value); SQRT2_value = Math.SQRT2; console.log("The value of the constant SQRT2 is: " + SQRT2_value); }).call(this);
現在,再次開啟命令提示符並執行CoffeeScript檔案,如下所示。
c:\> coffee math_example.coffee
執行後,CoffeeScript檔案會產生以下輸出。
The value of the constant E is: 2.718281828459045 The value of the constant LN2 is: 0.6931471805599453 The value of the constant LN10 is: 2.302585092994046 The value of the constant LOG2E is: 1.4426950408889634 The value of the constant LOG10E is: 0.4342944819032518 The value of the constant PI is: 3.141592653589793 The value of the constant SQRT1_2 is: 0.7071067811865476 The value of the constant SQRT2 is: 1.4142135623730951
數學方法
除了屬性外,Math物件還提供方法。以下是JavaScript的Math物件的方法列表。點選這些方法的名稱,可以獲得一個演示它們在CoffeeScript中用法的示例。
序號 | 方法和描述 |
---|---|
1 | abs()
返回數字的絕對值。 |
2 | acos()
返回數字的反餘弦值(以弧度表示)。 |
3 | asin()
返回數字的反正弦值(以弧度表示)。 |
4 | atan()
返回數字的反正切值(以弧度表示)。 |
5 | atan2()
返回其引數商的反正切值。 |
6 | ceil()
返回大於或等於數字的最小整數。 |
7 | cos()
返回數字的餘弦值。 |
8 | exp()
返回EN,其中N是引數,E是尤拉常數,即自然對數的底數。 |
9 | floor()
返回小於或等於數字的最大整數。 |
10 | log()
返回數字的自然對數(以E為底)。 |
11 | max()
返回零個或多個數字中最大的數字。 |
12 | min()
返回零個或多個數字中最小的數字。 |
13 | pow()
返回底數的指數次冪,即底數指數。 |
14 | random()
返回0到1之間的偽隨機數。 |
15 | round()
返回四捨五入到最接近整數的數字的值。 |
16 | sin()
返回數字的正弦值。 |
17 | sqrt()
返回數字的平方根。 |
18 | tan()
返回數字的正切值。 |