Coffeescript 简明教程

CoffeeScript - Math

JavaScript 的 Math 对象为您提供数学常数和函数的属性和方法。与其他全局对象不同的是, Math 不是一个构造函数。 Math 的所有属性和方法都是静态的,可以用 Math 作为对象来调用,而不用创建它。

The Math object of JavaScript provides you properties and methods for mathematical constants and functions. Unlike other global objects, Math is not a constructor. All the properties and methods of Math are static and can be called by using Math as an object without creating it.

因此,你可以将常量 pi 称为 Math.PI ,你可以将正弦函数称为 Math.sin(x) ,其中 x 是该方法的参数。我们可以在 CoffeeScript 代码中使用 JavaScript 的 Math 对象来执行数学运算。

Thus, you refer to the constant pi as Math.PI and you call the sine function as Math.sin(x), where x is the method’s argument. We can use the JavaScript’s Math object in our CoffeeScript code to perform math operations.

Mathematical constants

如果我们希望使用任何常用的数学常量,例如 pi 或 e,我们可以通过使用 JavaScript 的 Math 对象来使用它们。

If we want to use any common mathematical constants like pi or e we can use them using the JavaScript’s Math object.

以下是 JavaScript 的 Math 对象提供的 Math 常量的列表

Following is the list of the Math constants provided by the Math object of JavaScript

S.No.

Property & Description

1

E Euler’s constant and the base of natural logarithms, approximately 2.718.

2

LN2 Natural logarithm of 2, approximately 0.693.

3

LN10 Natural logarithm of 10, approximately 2.302.

4

LOG2E Base 2 logarithm of E, approximately 1.442.

5

LOG10E Base 10 logarithm of E, approximately 0.434.

6

PI Ratio of the circumference of a circle to its diameter, approximately 3.14159.

7

SQRT1_2 Square root of 1/2; equivalently, 1 over the square root of 2, approximately 0.707.

8

SQRT2Square root of 2, approximately 1.414.

Example

以下示例展示了 CoffeeScript 中 JavaScript 提供的数学常量的用法。将此代码保存在一个名为 math_example.coffee 的文件中

The following example demonstrates the usage of the mathematical constants provided by JavaScript in CoffeeScript. Save this code in a file with name 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

打开 command prompt 并按照以下所示编译 .coffee 文件。

Open the command prompt and compile the .coffee file as shown below.

c:\> coffee -c math_example.coffee

编译后,它会给你以下 JavaScript。

On compiling, it gives you the following 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);

现在,再次打开 command prompt 并按照以下所示运行 CoffeeScript 文件。

Now, open the command prompt again and run the CoffeeScript file as shown below.

c:\> coffee math_example.coffee

执行后,CoffeeScript 文件产生以下输出。

On executing, the CoffeeScript file produces the following output.

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 Methods

除了属性之外,Math 对象还提供方法。以下是 JavaScript 的 Math 对象的方法列表。点击这些方法的名称来获取一个示例,展示它们在 CoffeeScript 中的用法。

In addition to properties, the Math object also provides methods. Following is the list of methods of the Math object of JavaScript. Click on the name of these methods to get an example demonstrating their usage in CoffeeScript.

S.No.

Method & Description

1

abs()Returns the absolute value of a number.

2

acos()Returns the arccosine (in radians) of a number.

3

asin()Returns the arcsine (in radians) of a number.

4

atan()Returns the arctangent (in radians) of a number.

5

atan2()Returns the arctangent of the quotient of its arguments.

6

ceil()Returns the smallest integer greater than or equal to a number.

7

cos()Returns the cosine of a number.

8

exp()Returns EN, where N is the argument, and E is Euler’s constant, the base of the natural logarithm.

9

floor()Returns the largest integer less than or equal to a number.

10

log()Returns the natural logarithm (base E) of a number.

11

max()Returns the largest of zero or more numbers.

12

min()Returns the smallest of zero or more numbers.

13

pow()Returns base to the exponent power, that is, base exponent.

14

random()Returns a pseudo-random number between 0 and 1.

15

round()Returns the value of a number rounded to the nearest integer.

16

sin()Returns the sine of a number.

17

sqrt()Returns the square root of a number.

18

tan()Returns the tangent of a number.