开发学院

您的位置:首页>教程>正文

教程正文

JavaScript cos() 方法

JavaScript cos() 方法

定义和用法

cos() 方法可返回一个数字的余弦值。

语法

Math.cos(x)
参数描述
x必需。必须是一个数值。

返回值

x 的余弦值。返回的是 -1.0 到 1.0 之间的数。

实例

在本例中,我们将返回不同数值的余弦值:

<script type="text/javascript">

document.write(Math.cos(3) + "<br />")
document.write(Math.cos(-3) + "<br />")
document.write(Math.cos(0) + "<br />")
document.write(Math.cos(Math.PI) + "<br />")
document.write(Math.cos(2*Math.PI))

</script>

输出:

-0.9899924966004454
-0.9899924966004454
1
-1
1