温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么理解JavaScript math

发布时间:2021-09-30 13:46:40 来源:亿速云 阅读:103 作者:柒染 栏目:web开发

怎么理解JavaScript math,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

JavaScript的Math对象允许你对数字进行数学操作。上篇文章我们已经介绍了基本的Math函数用法,这篇文章我们来讲讲三角函数还有部分其他函数的用法。

一、三角函数
1. Math.sin()
Math.sin(x) 返回角度x的正弦值(-1到1之间)(以弧度)。

如果你想使用角度而不是弧度,你必须转换为弧度。

Angle in radians = Angle in degrees x PI / 180。

<!DOCTYPE html> <html lang="en"> <head>   <meta charset="UTF-8">   <title>项目</title> </head> <body  style="background-color: aqua;">    <h2>JavaScript Math.sin()</h2>    <p>Math.sin(x) 返回x的正弦值:</p>   <p>角弧度 = (度角) * PI / 180.</p>    <p id="demo"></p>    <script>     document.getElementById("demo").innerHTML =     "90 度的正弦值是:" + Math.sin(90 * Math.PI / 180); </script>  </body> </html>

2. Math.cos()
Math.cos(x) 返回x的余弦值(-1到1之间)(以弧度)。

如果你想使用角度而不是弧度,你必须转换为弧度。

Angle in radians = Angle in degrees x PI / 180。

<!DOCTYPE html> <html lang="en"> <head>   <meta charset="UTF-8">   <title>项目</title> </head> <body  style="background-color: aqua;">    <h2>JavaScript Math.cos()</h2>    <p>Math.cos(x) 返回x的余弦值(以弧度):</p>   <p>角弧度 = (度角) * PI / 180.</p>    <p id="demo"></p>    <script>     document.getElementById("demo").innerHTML =     "0度的余弦值是:" + Math.cos(0 * Math.PI / 180); </script>  </body> </html>

3. 其他函数
1. Math.min()
Math.min() 和 Math.max() 可用于在参数列表中查找最低或最高值。

<script>     document.getElementById("demo").innerHTML =     Math.min(0, 150, 30, 20, -8, -200); // returns -200 </script>

2. Math.max()

<script>     document.getElementById("demo").innerHTML =     Math.max(0, 150, 30, 20, -8, -200); </script>

二、Math 属性 (常量)
JavaScript 提供8个可以被Math对象访问的数学常数:(来源百度)。

Math.E        // returns Euler's number Math.PI       // returns PI Math.SQRT2    // returns the square root of 2 Math.SQRT1_2  // returns the square root of 1/2 Math.LN2      // returns the natural logarithm of 2 Math.LN10     // returns the natural logarithm of 10 Math.LOG2E    // returns base 2 logarithm of E Math.LOG10E   // returns base 10 logarithm of E

从最基本的函数开始,讲解Math函数中常见的方法,有三角函数方法,还有其他的一些常见的函数,都做了详细的讲解。用大量的案例进行分析,对Math函数如何去运用这些方法函数,以及在实际运用中遇到难点都做了详细讲解。关于怎么理解JavaScript math问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI