解釋 CoffeeScript 中可使用的各種數學函式


CoffeeScript 於 2009 年推出,它編譯成 JavaScript。CoffeeScript 和 JavaScript 之間簡單的區別在於語法。CoffeeScript 的語法非常簡單。

由於 CoffeeScript 編譯成 JavaScript,因此我們可以在 CoffeeScript 中使用 JavaScript 的每種方法。因此,我們將解釋可以在 CoffeeScript 中使用的 Math 物件的每個數學函式。

在 JavaScript 中,Math 是一個靜態物件,因此我們可以直接使用它,而無需獲取任何元素的引用。我們可以透過將“Math”關鍵字作為引用來呼叫 Math 物件的方法。

CoffeeScript 中的數學函式

Math.abs()

Math 物件的 abs() 方法將數字值作為引數,並返回該數字的絕對值。

示例

在本例中,我們使用了不同的數字,並使用了 Math.abs() 方法來獲取它們的絕對值。

absolute_value = Math.abs(-30)
console.log "The absolute value of -30 is " + absolute_value
absolute_value = Math.abs(30)
console.log "The absolute value of -30 is " + absolute_value

編譯後,上述 CoffeeScript 生成以下 JavaScript 程式碼:

// Generated by CoffeeScript 2.4.1
(function() {
   var absolute_value;

   absolute_value = Math.abs(-30);

   console.log("The absolute value of -30 is " + absolute_value);

   absolute_value = Math.abs(30);

   console.log("The absolute value of -30 is " + absolute_value);

}).call(this);

輸出

執行後,它會產生以下輸出:

The absolute value of -30 is 30
The absolute value of -30 is 30

Math.sin()

我們可以使用 Math 物件的 sin() 方法獲取任何實數的正弦值。

示例

在下面的示例中,我們使用了 sin() 方法來獲取不同實數的正弦值。使用者可以觀察到輸出返回的值介於 -1 和 1 之間。

sine_value = Math.sin(0)
console.log "The sine value of 0 is " + sine_value

sine_value = Math.sin(3434343232340)
console.log "The sine value of 3434343232340 is " + sine_value

編譯後,上述 CoffeeScript 生成以下 JavaScript 程式碼:

// Generated by CoffeeScript 2.4.1
(function() {
   var sine_value;

   sine_value = Math.sin(0);

   console.log("The sine value of 0 is " + sine_value);

   sine_value = Math.sin(3434343232340);

   console.log("The sine value of 3434343232340 is " + sine_value);

}).call(this);

輸出

執行後,它會產生以下輸出。

The sine value of 0 is 0
The sine value of 3434343232340 is 0.6305294679473046

Math.cos()

Math 物件的 cos() 方法用於獲取任何實數的餘弦值,它返回介於 -1 和 1 之間的輸出。

示例

在下面的示例中,我們使用了 cos 方法,並將不同的值作為引數傳遞。

cosine_value = Math.cos(-4545)
console.log "The cosine value of -4545 is " + cosine_value

cosine_value = Math.cos(1)
console.log "The cosine value of 1 is " + cosine_value

編譯後,上述 CoffeeScript 生成以下 JavaScript 程式碼:

// Generated by CoffeeScript 2.4.1
(function() {
   var cosine_value;

   cosine_value = Math.cos(-4545);

   console.log("The cosine value of -4545 is " + cosine_value);

   cosine_value = Math.cos(1);

   console.log("The cosine value of 1 is " + cosine_value);

}).call(this);

輸出

執行後,它會產生以下輸出。

The cosine value of -4545 is -0.6336224240941999
The cosine value of 1 is 0.5403023058681398

Math.floor()

Math 物件的 floor() 方法向下舍入我們作為引數傳遞的數字。它返回小於或等於作為引數傳遞的數字值的最大整數。

示例

我們使用了不同的數字,並使用了 floor 方法來向下舍入它們。使用者可以在輸出中觀察到,0.999 最接近 1,但仍然被 floor() 方法向下舍入了。

floor_value = Math.floor(544.4343)
console.log "The floor value of 5444.4343 is " + floor_value

floor_value = Math.floor(0.9999)
console.log "The floor value of 0.9999 is " + floor_value

編譯後,上述 CoffeeScript 生成以下 JavaScript 程式碼:

// Generated by CoffeeScript 2.4.1
(function() {
   var floor_value;

   floor_value = Math.floor(544.4343);

   console.log("The floor value of 5444.4343 is " + floor_value);

   floor_value = Math.floor(0.9999);

   console.log("The floor value of 0.9999 is " + floor_value);

}).call(this);

輸出

執行後,它會產生以下輸出。

The floor value of 5444.4343 is 544
The floor value of 0.9999 is 0

Math.ceil()

Math 物件的 ceil() 方法用於向上舍入數字。它返回大於或等於作為引數傳遞的數字的最小整數。

示例

在下面的示例的輸出中,我們可以看到,即使 0.001 最接近 0,ceil() 方法仍然返回 1,因為它始終向上舍入數字。

ceil_value = Math.ceil(0.001)
console.log "The ceil value of 0.001 is " + ceil_value

ceil_value = Math.ceil(-10)
console.log "The ceil value of -10 is " + ceil_value

編譯後,上述 CoffeeScript 生成以下 JavaScript 程式碼:

// Generated by CoffeeScript 2.4.1
(function() {
   var ceil_value;

   ceil_value = Math.ceil(0.001);

   console.log("The ceil value of 0.001 is " + ceil_value);

   ceil_value = Math.ceil(-10);

   console.log("The ceil value of -10 is " + ceil_value);

}).call(this);

輸出

執行後,它會產生以下輸出。

The ceil value of 0.001 is 1
The ceil value of -10 is -10

Math.log()

Math 物件的 log() 方法用於查詢任何正數值的自然對數。它透過以 E 為底計算數字的對數,其中 E 是尤拉常數。

示例

我們使用 log() 方法找到了不同正值的自然對數。

log_value = Math.log(10)
console.log "The log value of 10 is " + log_value

log_value = Math.log(4545)
console.log "The log value of 4545 is " + log_value

編譯後,上述 CoffeeScript 生成以下 JavaScript 程式碼:

// Generated by CoffeeScript 2.4.1
(function() {
  var log_value;

  log_value = Math.log(10);

  console.log("The log value of 10 is " + log_value);

  log_value = Math.log(4545);

  console.log("The log value of 4545 is " + log_value);

}).call(this);

輸出

執行後,它會產生以下輸出。

The log value of 10 is 2.302585092994046
The log value of 4545 is 8.421783006611578

Math.random()

我們可以使用 Math 物件的 random() 方法在 CoffeeScript 中查詢隨機數。它返回 0 到 1 之間的隨機數。

示例

在本例中,我們多次呼叫了 random() 方法,使用者可以在輸出中看到不同的值。

random_value = Math.random()
console.log "The random value is " + random_value

random_value = Math.random()
console.log "The random value is " + random_value

編譯後,上述 CoffeeScript 生成以下 JavaScript 程式碼:

// Generated by CoffeeScript 2.4.1
(function() {
   var random_value;

   random_value = Math.random();

   console.log("The random value is " + random_value);

   random_value = Math.random();

   console.log("The random value is " + random_value);

}).call(this);

輸出

執行後,它會產生以下輸出。

The random value is 0.3468464659210826
The random value is 0.4870361090153488

Math.max()

使用者可以使用 Math 物件的 max() 方法從多個數值中查詢最大值。

示例

在本例中,我們將多個值作為 max() 方法的引數傳遞,以查詢它們中的最大值。

max_value = Math.max(20, 345, 23, 23, 12, 12)
console.log "The max value among 20, 345, 23, 23, 12, 12 is " + max_value

max_value = Math.max(43, 43, 43)
console.log "The max value among 43, 43, 43 is " + max_value

編譯後,上述 CoffeeScript 生成以下 JavaScript 程式碼:

// Generated by CoffeeScript 2.4.1
(function() {
   var max_value;

   max_value = Math.max(20, 345, 23, 23, 12, 12);

   console.log("The max value among 20, 345, 23, 23, 12, 12 is " + max_value);

   max_value = Math.max(43, 43, 43);

   console.log("The max value among 43, 43, 43 is " + max_value);

}).call(this);

輸出

執行後,它會產生以下輸出。

The max value among 20, 345, 23, 23, 12, 12 is 345
The max value among 43, 43, 43 is 43

Math.min()

我們可以使用 Math 物件的 min() 方法從兩個或多個數值中查詢最小值。它返回一個值,即作為引數傳遞的所有值中的最小值。

示例

在本例中,我們使用了不同的值,並使用了 Math 物件的 min() 方法來查詢它們中的最小值。

min_value = Math.min(20, 345, 23, 23, 12, 12)
console.log "The min value among 20, 345, 23, 23, 12, 12 is " + min_value

min_value = Math.min(43, 43, 43)
console.log "The min value among 43, 43, 43 is " + min_value

編譯後,上述 CoffeeScript 生成以下 JavaScript 程式碼:

// Generated by CoffeeScript 2.4.1
(function() {
   var min_value;

   min_value = Math.min(20, 345, 23, 23, 12, 12);

   console.log("The min value among 20, 345, 23, 23, 12, 12 is " + min_value);

   min_value = Math.min(43, 43, 43);

   console.log("The min value among 43, 43, 43 is " + min_value);

}).call(this);

輸出

執行後,它會產生以下輸出。

The min value among 20, 345, 23, 23, 12, 12 is 12
The min value among 43, 43, 43 is 43

Math.sqrt()

Math 物件的 sqrt() 方法將單個值作為引數,並返回該數字的平方根。

示例

在本例中,我們使用了 sqrt() 方法來查詢不同實數的平方根。

sqrt_value = Math.sqrt(12)
console.log "The sqrt of the 12 is " + sqrt_value

sqrt_value = Math.sqrt(1.32)
console.log "The sqrt of 1.32 is " + sqrt_value

編譯後,上述 CoffeeScript 生成以下 JavaScript 程式碼:

// Generated by CoffeeScript 2.4.1
(function() {
   var sqrt_value;

   sqrt_value = Math.sqrt(12);

   console.log("The sqrt of the 12 is " + sqrt_value);

   sqrt_value = Math.sqrt(1.32);

   console.log("The sqrt of 1.32 is " + sqrt_value);

}).call(this);

輸出

執行後,它會產生以下輸出。

The sqrt of the 12 is 3.4641016151377544
The sqrt of 1.32 is 1.1489125293076057

Math.exp()

我們可以使用 exp() 方法查詢 Ex 的值,其中 E 是尤拉常數,x 是我們作為引數傳遞的值。

示例

在本例中,我們使用了 exp() 方法,並將不同的實數作為 exp() 方法的引數傳遞,以獲取 Ex 的值。

exp_value = Math.exp(19)
console.log "The exp value of the 19 is " + exp_value

exp_value = Math.exp(2.21)
console.log "The exp of value 2.21 is " + exp_value

編譯後,上述 CoffeeScript 生成以下 JavaScript 程式碼:

// Generated by CoffeeScript 2.4.1
(function() {
   var exp_value;

   exp_value = Math.exp(19);

   console.log("The exp value of the 19 is " + exp_value);

   exp_value = Math.exp(2.21);

   console.log("The exp of value 2.21 is " + exp_value);

}).call(this);

輸出

執行後,它會產生以下輸出。

The exp value of the 19 is 178482300.96318728
The exp of value 2.21 is 9.115716393040305

Math.round()

我們可以使用 math 物件的 round() 方法將數字舍入到最接近的整數。

示例

在本例中,使用者可以觀察到 1.49 如何舍入到 1,而 1.51 如何舍入到 2。

round_value = Math.round(1.49)
console.log "The round value of the 1.49 is " + round_value

round_value = Math.round(1.51)
console.log "The round of value 1.51 is " + round_value

編譯後,上述 CoffeeScript 生成以下 JavaScript 程式碼:

// Generated by CoffeeScript 2.4.1
(function() {
   var round_value;

   round_value = Math.round(1.49);

   console.log("The round value of the 1.49 is " + round_value);

   round_value = Math.round(1.51);

   console.log("The round of value 1.51 is " + round_value);

}).call(this);

輸出

執行後,它會產生以下輸出。

The round value of the 1.49 is 1
The round of value 1.51 is 2

在本教程中,我們介紹了 Math 物件的不同函式。此外,我們還可以看到,當我們在 JavaScript 中執行 Math 方法時,我們在這裡也執行了它們。此外,Math 物件在 CoffeeScript 中還包含一些其他方法,使用者也可以探索這些方法。

更新於:2023-04-05

74 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.