JavaScript 中的 Math.sin() 方法用于计算所提供数字的 sune 值(以弧度为单位)。此方法返回一个“数值”,该值表示给定角度的正弦值(以弧度为单位)。如果为此方法提供的参数是 NaN 或 Infinity 值,则返回 NaN 作为结果。
语法
以下是 JavaScript Math.sin() 方法的语法 -
Math.sin(x)
参数
此方法只接受一个参数。下面描述相同 -
- x: 一个数值,表示以弧度为单位的角度。
返回值
此方法返回一个数值(介于 -1 和 1 之间,包括 -1 和 1),表示指定角度的正弦值。
示例 1
在以下示例中,我们使用 JavaScript Math.sin() 方法来计算 2 弧度的正弦值 -
<html>
<body>
<script>
const result = Math.sin(2);
document.write(result);
</script>
</body>
</html>
输出
如果我们执行上述程序,它将返回大约 “0.9092”。
示例 2
在这里,我们计算的是负 5 弧度的正弦值 -
<html>
<body>
<script>
const result = Math.sin(-5);
document.write(result);
</script>
</body>
</html>
输出
结果,它将返回大约 0.95892。
示例 3
在下面的示例中,我们正在计算数学常数 “PI” 的正弦值 -
<html>
<body>
<script>
const result = Math.sin(Math.PI); //3.14
document.write(result);
</script>
</body>
</html>
输出
输出 1.2246467991473532e-16 表示 -1.2246467991473532 × 10-16
示例 4
如果我们尝试计算 “string” 的正弦值,此方法将返回 NaN 作为结果 -
<html>
<body>
<script>
const result = Math.sin("qikepu");
document.write(result);
</script>
</body>
</html>
输出
正如我们在下面的输出中看到的,它返回了 NaN 作为结果。
示例 5
Math.sin() 方法不将 “Infinity” 视为一个数字,因此如果我们将其作为参数传递,此方法将返回 NaN 作为结果 −
<html>
<body>
<script>
const result1 = Math.sin(Infinity);
const result2 = Math.sin(-Infinity);
document.write(result1, "<br>", result2);
</script>
</body>
</html>
输出
正如我们在下面的输出中看到的,它为正和负 Infinity 都返回了 NaN。