📜  JavaScript数学sin()

📅  最后修改于: 2020-09-27 06:37:38             🧑  作者: Mango

JavaScript Math.sin() 函数返回指定数字的正弦值。

Math.sin() 函数的语法为:

Math.sin(x)

使用Math类名称调用sin()作为静态方法。


Math.sin()参数

Math.sin() 函数接受:

  • x-需要正弦值的数字(以弧度为单位)。

从Math.sin()返回值
  • 返回给定角度(介于-11之间的数值)的正弦值。

示例1:使用Math.sin()
// sine of 1 radian
var value1 = Math.sin(1);
console.log(value1); // Output: 0.8414709848078965

// negative radians are allowed
var value2 = Math.sin(-2);
console.log(value2); // Output: -0.9092974268256817

// Math constants can be used
var value3 = Math.sin(Math.PI / 2);
console.log(value3); // Output: 1

输出

0.8414709848078965
-0.9092974268256817
1

示例2:将Math.sin()与度一起使用
// custom function for angle in degrees
function sin(degrees) {
  var radians = (degrees * Math.PI) / 180;
  return Math.sin(radians);
}

// sine of 57 degrees
value1 = sin(57);
console.log(value1); // Output: 0.8386705679454239

// sine of negative degrees
value2 = sin(-90);
console.log(value2); // Output: -1

value3 = sin(90);
console.log(value3); // Output: 1

输出

0.8386705679454239
-1
1

在这里,我们定义了一个sin() 函数 ,该函数将度值转换为弧度,然后将其传递给Math.sin()

我们可以以类似的方式定义自定义函数,以扩展此类内置函数的功能。


推荐读物:

  • JavaScript Math.asin()
  • JavaScript Math.cos()