JavaScript Math.tan() 方法
JavaScript Math.tan() 方法允许使用数值作为参数(以弧度表示角度),并计算该数值的三角正切值。如果此方法提供的参数为 NaN 或无穷大值,则返回 NaN 作为结果。
语法
以下是 JavaScript Math.tan() 方法的语法 -
Math.tan(x)
参数
此方法仅接受一个参数。下文同样描述 -
- x: 以弧度表示角度的数值。
返回值
此方法返回给定数字的正切值(以弧度为单位)。
示例 1
在下面的示例中,我们使用 JavaScript Math.tan() 方法计算 2 弧度的正切值 -
<html> <body> <script> const result = Math.tan(4); document.write(result); </script> </body> </html>
输出
如果我们执行上述程序,它将返回近似值"1.1578"。
示例 2
这里,我们计算负 -4 弧度的正切值 -
<html> <body> <script> const result = Math.tan(-4); document.write(result); </script> </body> </html>
输出
结果将约为"-1.157"。
示例 3
在下面的示例中,我们计算数学常数"PI"的正切值 -
<html> <body> <script> const result = Math.tan(Math.PI); document.write(result); </script> </body> </html>
输出
输出 -1.2246467991473532e-16 代表 -1.2246467991473532 × 10-16
示例 4
如果我们尝试计算"字符串"的正切值,此方法将返回 NaN 作为结果 -
<html>
<body>
<script>
const result = Math.tan("Tutorialspoint");
document.write(result);
</script>
</body>
</html>
输出
正如我们在下面的输出中看到的,它返回了 NaN 作为结果。
示例 5
Math.tan() 方法不将"Infinity"视为数字,因此如果我们将其作为参数传递,则此方法将返回 NaN 作为结果 -
<html> <body> <script> const result1 = Math.tan(Infinity); const result2 = Math.tan(-Infinity); document.write(result1, "<br>", result2); </script> </body> </html>
输出
这是因为角度的正切不能为无穷大。

