JavaScript Math tan()

tan() 方法计算指定角度的三角正切值并返回。

示例

// 1 represents angle in radian
let value = Math.tan(1);
console.log(value); 

// Output: 1.5574077246549023

tan() 语法

Math.tan() 方法的语法是:

Math.tan(angle)

在这里,tan() 是一个静态方法。因此,我们使用类名 Math 来访问该方法。


tan() 参数

tan() 方法接受一个参数:

  • angle - 以弧度为单位,需要计算其正切值。

tan() 返回值

tan() 方法返回:

  • 给定 angle (以 **弧度** 为单位)的正切值。
  • 对于非数字参数,返回 NaN(非数字)。

示例 1:JavaScript Math.tan()

// tangent of 5 radian let value1 = Math.tan(5);
console.log(value1);
// negative radians are allowed let value2 = Math.tan(-2);
console.log(value2); // Output: // -3.380515006246586 // 2.185039863261519

在上面的例子中:

  • Math.tan(5) - 计算 5 的正切值。
  • Math.tan(-2) - 计算 -2 的正切值。

示例 2:Math.tan() 与 Math 常量

// math constants can be used let value = Math.tan(Math.PI);
console.log(value); // Output: -1.2246467991473532e-16

在上面的示例中,我们使用 tan() 方法来计算数学常量 PI 的正切值。

这里,输出 -1.2246467991473532e-16 表示 -1.2246467991473532 * 10-16


示例 3:Math.tan() 与非数字参数

let string = "Luke";

// tan() with a string argument let value = Math.tan(string);
console.log(value); // Output: NaN

在上面的示例中,我们使用了字符串参数调用 tan() 方法。

当使用字符串参数时,该方法返回 NaN


示例 4:Math.tan() 与 Infinity 参数

// tan() with infinity let value1 = Math.tan(Infinity);
console.log(value1);
// tan() with negative infinity let value2 = Math.tan(-Infinity);
console.log(value2); // Output: // NaN // NaN

tan() 方法不将 -InfinityInfinity 视为数字,并返回 NaN

这是因为角度的正切值永远不可能是无限的。


另请阅读

你觉得这篇文章有帮助吗?

我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

以前所未有的方式学习和提高您的编程技能。

试用 Programiz PRO
  • 交互式课程
  • 证书
  • AI 帮助
  • 2000+ 挑战