CoffeeScript - 数学

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

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

数学常量

如果我们想要使用任何常见的数学常量pi或e我们可以使用JavaScript的 Math 对象来使用它们.

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

S.No.物业&说明
1

E

Euler常数和自然对数的基数,约为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的平方根;等价地,1比2的平方根,大约0.707.

8 SQRT2

平方根2,大约1.414.

示例

以下示例演示了JavaScript提供的数学常量的用法CoffeeScript的.将此代码保存在名为 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

打开命令提示符并编译.co ffee文件如下所示.

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.方法&描述
1abs()

返回数字的绝对值.

2acos()

返回数字的反余弦(以弧度表示).

3asin()

返回数字的反正弦(以弧度表示).

4atan()

返回数字的反正切(以弧度表示).

5atan2()

返回其参数商的反正切值.

6ceil()

返回大于或等于数字的最小整数.

7cos()

返回数字的余弦.

8exp()

返回E N ,其中N是参数,E是欧拉常数,是自然对数的基数.

9floor()

返回小于或等于的最大整数到一个数字.

10log()

返回数字的自然对数(基数E).

11max()

返回0或0以上的最大数字.

12min()

返回0或0以上的最小数字.

13pow()

返回指数幂的基数,即基本指数.

14random()

返回0到1之间的伪随机数.

15round()

返回舍入到最接近整数的数字的值.

16sin()

返回数字的正弦值.

17sqrt ()

返回数字的平方根.

18tan()

返回a的正切值号码.