📜  CoffeeScript-数学

📅  最后修改于: 2020-10-26 05:49:54             🧑  作者: Mango


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

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

数学常数

如果我们想使用任何常见的数学常数(例如pi或e),则可以通过JavaScript的Math对象使用它们。

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

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 SQRT2

Square root of 2, approximately 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中的用法。

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.