1. Math对象允许您对数字执行数学任务。

2. Math属性(常量)

2.1. JavaScript提供了可由Math对象访问的8个数学常量:

Math.E // 返回算术常量e, 即自然对数的底数, 其值近似于2.71828
Math.PI // 返回圆周率(PI)(约等于3.14159)
Math.SQRT2 // 返回2的平方根(约等于1.414)
Math.SQRT1_2 // 返回1/2的平方根(约等于0.707)
Math.LN2 // 返回2的自然对数(约等于0.693)
Math.LN10 // 返回10的自然对数(约等于2.302)
Math.LOG2E // 返回以2为底的e的对数(约等于1.44)
Math.LOG10E // 返回以10为底的e的对数(约等于0.434)

2.2. 例子

2.2.1. 代码

<!DOCTYPE html>
<html lang="zh-cn"><head><meta charset="utf-8" /><title>Math对象常量</title></head><body><script type="text/javascript">document.write('Math.E = ' + Math.E + '<br />'); // 返回算术常量e, 即自然对数的底数, 其值近似于2.71828document.write('Math.PI = ' + Math.PI + '<br />'); // 返回圆周率(PI)(约等于3.14159)document.write('Math.SQRT2 = ' + Math.SQRT2 + '<br />'); // 返回2的平方根(约等于1.414)document.write('Math.SQRT1_2 = ' + Math.SQRT1_2 + '<br />'); // 返回1/2的平方根(约等于0.707)document.write('Math.LN2 = ' + Math.LN2 + '<br />'); // 返回2的自然对数(约等于0.693)document.write('Math.LN10 = ' + Math.LN10 + '<br />'); // 返回10的自然对数(约等于2.302)document.write('Math.LOG2E = ' + Math.LOG2E + '<br />'); // 返回以2为底的e的对数(约等于1.44)document.write('Math.LOG10E = ' + Math.LOG10E + '<br />'); // 返回以10为底的e的对数(约等于0.434)</script></body>
</html>

2.2.2. 效果图

3. Math.min()和Math.max()

3.1 Math.min()和Math.max()可用于查找参数列表中的最低或最高值。

3.2. 例子

3.2.1. 代码

<!DOCTYPE html>
<html lang="zh-cn"><head><meta charset="utf-8" /><title>Math对象最大值和最小值</title></head><body><script type="text/javascript">document.write('Math.min = ' + Math.min(0, 450, 35, 10, -8, -300, -78) + '<br />');document.write('Math.max = ' + Math.max(0, 450, 35, 10, -8, -300, -78) + '<br />');</script></body>
</html>

3.2.2. 效果图

4. Math.round()、Math.ceil()和Math.floor()舍入

4.1. Math.round(x)的返回值是x四舍五入为最接近的整数。

4.2. Math.ceil(x)的返回值是x上舍入最接近的整数。

4.3. Math.floor(x)的返回值是x下舍入最接近的整数。

4.4. 例子

4.4.1. 代码

<!DOCTYPE html>
<html lang="zh-cn"><head><meta charset="utf-8" /><title>Math.round()、Math.ceil()和Math.floor()舍入</title></head><body><script type="text/javascript">var a = 3.9, b = 5.2;document.write('Math.round(' + a + ') = ' + Math.round(a) + '<br />');document.write('Math.round(' + b + ') = ' + Math.round(b) + '<br />');document.write('Math.ceil(' + a + ') = ' + Math.ceil(a) + '<br />');document.write('Math.ceil(' + b + ') = ' + Math.ceil(b) + '<br />');document.write('Math.floor(' + a + ') = ' + Math.floor(a) + '<br />');document.write('Math.floor(' + b + ') = ' + Math.floor(b) + '<br />');</script></body>
</html>

4.4.2. 效果图

5. Math.pow()

5.1. Math.pow(x, y)的返回值是x的y次幂。

5.2. 实例

Math.pow(8, 2); // 返回 64

6. Math.sqrt()

6.1. Math.sqrt(x)返回x的平方根。

6.2. 实例

Math.sqrt(64);      // 返回 8

7. Math.abs()

7.1. Math.abs(x)返回x的绝对(正)值。

7.2. 实例

Math.abs(-4.7);     // 返回 4.7

7.3. 例子

7.3.1. 代码

<!DOCTYPE html>
<html lang="zh-cn"><head><meta charset="utf-8" /><title>Math.pow()、Math.sqrt()和Math.abs()</title></head><body><script type="text/javascript">document.write('Math.pow(8, 2) = ' + Math.pow(8, 2) + '<br />');document.write('Math.sqrt(64) = ' + Math.sqrt(64) + '<br />');document.write('Math.abs(-4.7) = ' + Math.abs(-4.7) + '<br />');</script></body>
</html>

7.3.2. 效果图

8. Math.random()

8.1. Math.random()返回介于0(包括)与1(不包括)之间的随机数。

8.2. Math.random() 总是返回小于1的数。

8.3. 返回介于min(包括)和max(不包括)之间的随机数:

function getRndInteger(min, max) {return Math.floor(Math.random() * (max - min) ) + min;
}

8.4. 返回介于min(包括)和max(包括)之间的随机数:

function getRndInteger(min, max) {return Math.floor(Math.random() * (max - min + 1) ) + min;
}

8.5. 返回介于min(不包括)和max(包括)之间的随机数:

function getRndInteger(min, max) {return Math.floor(Math.random() * (max - min) ) + min + 1;
}

8.6. 例子

8.6.1. 代码

<!DOCTYPE html>
<html lang="zh-cn"><head><meta charset="utf-8" /><title>Math.random()随机数</title></head><body><script type="text/javascript">document.write('Math.random() = ' + Math.random() + '<br /><hr />');document.write('<p>返回 0 至 9 之间的数</p>');document.write('Math.floor(Math.random() * 10) = ' + Math.floor(Math.random() * 10) + '<br /><hr />');document.write('<p>返回 0 至 10 之间的数</p>');document.write('Math.floor(Math.random() * 11) = ' + Math.floor(Math.random() * 11) + '<br />');document.write('Math.ceil(Math.random() * 10) = ' + Math.ceil(Math.random() * 10) + '<br /><hr />');document.write('<p>返回 1 至 10 之间的数</p>');document.write('Math.floor(Math.random() * 10) + 1 = ' + (Math.floor(Math.random() * 10) + 1) + '<br /><hr />');function getRndInteger1(min, max) {return Math.floor(Math.random() * (max - min) ) + min;}function getRndInteger2(min, max) {return Math.floor(Math.random() * (max - min + 1) ) + min;}function getRndInteger3(min, max) {return Math.floor(Math.random() * (max - min) ) + min + 1;}document.write('<p>返回 0 至 99 之间的数</p>');document.write('getRndInteger1(0, 100) = ' + getRndInteger1(0, 100) + '<br /><hr />');document.write('<p>返回 0 至 100 之间的数</p>');document.write('getRndInteger2(0, 100) = ' + getRndInteger2(0, 100) + '<br /><hr />');document.write('<p>返回 1 至 100 之间的数</p>');document.write('getRndInteger3(0, 100) = ' + getRndInteger3(0, 100) + '<br />');</script></body>
</html>

8.6.2. 效果图

9. 三角函数

9.1. 根号2约等于1.414。

9.2. 根号3约等于1.732。

9.3. 弧度值2PI / 360。

9.4. 特殊角的三角函数值表

9.5. Math.sin(x)返回角x(以弧度计)的正弦(介于-1与1之间的值)。

9.6. Math.cos(x)返回角x(以弧度计)的余弦(介于-1与1之间的值)。

9.7. Math.tan(x)方法可返回一个表示某个角的正切的数字。

9.8. 例子

9.8.1. 代码

<!DOCTYPE html>
<html lang="zh-cn"><head><meta charset="utf-8" /><title>三角函数</title></head><body><script type="text/javascript">document.write('Math.sin(0 * Math.PI / 180) = ' + Math.sin(0 * Math.PI / 180) + '<br />');document.write('Math.sin(30 * Math.PI / 180) = ' + Math.sin(30 * Math.PI / 180) + '<br />'); document.write('Math.sin(45 * Math.PI / 180) = ' + Math.sin(45 * Math.PI / 180) + '<br />'); document.write('Math.sin(60 * Math.PI / 180) = ' + Math.sin(60 * Math.PI / 180) + '<br />');document.write('Math.sin(90 * Math.PI / 180) = ' + Math.sin(90 * Math.PI / 180) + '<br /><hr />');document.write('Math.cos(0 * Math.PI / 180) = ' + Math.cos(0 * Math.PI / 180) + '<br />');document.write('Math.cos(30 * Math.PI / 180) = ' + Math.cos(30 * Math.PI / 180) + '<br />');document.write('Math.cos(45 * Math.PI / 180) = ' + Math.cos(45 * Math.PI / 180) + '<br />');document.write('Math.cos(60 * Math.PI / 180) = ' + Math.cos(60 * Math.PI / 180) + '<br />');document.write('Math.cos(90 * Math.PI / 180) = ' + Math.cos(90 * Math.PI / 180) + '<br /><hr />');document.write('Math.tan(0 * Math.PI / 180) = ' + Math.tan(0 * Math.PI / 180) + '<br />');document.write('Math.tan(30 * Math.PI / 180) = ' + Math.tan(30 * Math.PI / 180) + '<br />');document.write('Math.tan(45 * Math.PI / 180) = ' + Math.tan(45 * Math.PI / 180) + '<br />');document.write('Math.tan(60 * Math.PI / 180) = ' + Math.tan(60 * Math.PI / 180) + '<br />');</script></body>
</html>

9.8.2. 效果图

072_Math对象相关推荐

  1. java_B站_面试题

    Java面向对象有哪些特征,如何应用 ​ 面向对象编程是利用类和对象编程的一种思想.万物可归类,类是对于世界事物的高度抽象 ,不同的事物之间有不同的关系 ,一个类自身与外界的封装关系,一个父类和子类的 ...

  2. 史上最详细的23中设计模式解析,一个不落,理论搭配简单案例,更好理解哦

    目录 一.软件设计模式的产生背景 二.软件设计模式的概念与意义 1. 软件设计模式的概念 2. 学习设计模式的意义 三.23 种设计模式的分类和功能 1. 根据目的来分 2. 根据作用范围来分 3. ...

  3. 在kotlin companion object中读取Bean,注入Bean对象

    在kotlin companion object中读取Bean,注入Bean对象 在使用kotlin时,或多或少地会使用到一些公共组件,如 http. mongo. redis相关的组件.   使用组 ...

  4. IDEA自动生成对象所有set方法

    idea中有一款插件能够生成对象所有的set方法,GenerateAllSetter :下载地址 步骤1:将下载好的压缩包放在自己记得的文件夹中,在idea中进行导入 步骤2:在本地选中刚才的压缩包, ...

  5. 基于Golang的对象序列化的程序包开发——myJsonMarshal

    基于Golang的对象序列化的程序包开发--myJsonMarshal[阅读时间:约10分钟] 一.对象序列化概述 二.系统环境&项目介绍 1.系统环境 2.项目的任务要求 三.具体程序设计及 ...

  6. java 捕获异常并存入数据库_java异常处理,报异常的话怎么处理对象值,并持久化到数据库中...

    展开全部 //没看到有人回e68a843231313335323631343130323136353331333365646233答你,我还没学到框架,不知道那个是不是可以很便捷操作你说的这样过程 / ...

  7. python程序如何执行死刑图片_如何判断对象已死

    已死的对象就是不可能被任何途径使用的对象,有以下几种方法判断一个对象是否已经死了: 引用计数 给对象添加一个引用计数器,每当有一个地方引用他,计算器就加 1:当引用失效时,计数器减 1:任何时刻计数器 ...

  8. Go 学习笔记(64)— Go error.New 创建接口错误对象、fmt.Errorf 创建接口错误对象、errors.Is 和 errors.As

    1. error 接口定义 除用 panic 引发中断性错误外,还可返回 error 类型错误对象来表示函数调用状态.error 接口是 Go 原生内置的类型,它的定义如下: // $GOROOT/s ...

  9. OpenCV 笔记(06)— Mat 结构、像素值存储方法、创建 Mat 对象各种方法、Mat 对象的运算

    数字图像中的每个点都称为像素(对于图像元素),并且每个像素可以存储一个或多个值,这取决于它是否是仅存储一个值的黑白图像(也称为二进制图像,比如只存储0或1),还是存储两个值的灰度图像,或者是存储三个值 ...

最新文章

  1. 特斯拉撤诉和解,小鹏汽车沉冤得雪:警惕自动驾驶领域的“美国陷阱”
  2. NOIP2018 模拟 9.11
  3. 一个用于 Entity Framework 对象拷贝的方法
  4. CentOS 6.5 升级 PHP 到5.6
  5. DPI释疑--What is mean of Dpi?
  6. 关于优酷SDK之setOnADPlayListener
  7. Struct2_定义拦截器并使用注解方式作用在Action的方法中
  8. python同步oracle_Python cx_Oracle 7引入苏打文档存储
  9. xml mysql 模糊查询_mybatis+Spring mysql的模糊查询问题
  10. [Linux]共享内存
  11. 多任务Python爬虫
  12. thinkphp-page
  13. Input.GetAxis(Mouse ScrollWheel)控制摄像机视野缩放
  14. 无法编辑PDF文档?看完这篇文章即刻解决~
  15. P3853 路标设置
  16. mysql存储图片特征向量_图像特征提取之(一)HOG特征
  17. unity 预编译指令
  18. 揭开物联网的神秘面纱--物联网小灯
  19. 计算机组成与系统结构2018,计算机组成原理与系统结构2018-2019试卷a.doc
  20. 【英语阅读】纽约时报 | 台湾美食为什么那么“Q”?

热门文章

  1. 合肥市电力大数据应用工程技术研究中心成立
  2. 【1】ASP.NET异步(1)
  3. ASP.NET Core 中文文档 第二章 指南(4.5)使用 SQL Server LocalDB
  4. iOS UI基础-6.0 UIActionSheet的使用
  5. ​redis实现消息队列
  6. BGP信息类型和分组公共首部
  7. 【指标统计】根据遥控补全遥信
  8. 在CentOS7上编译GreenPlum5.3.0
  9. WPF中的动画——(二)From/To/By 动画(二)
  10. 给大家分享微信小说域名防封最新的解决方案