写在前面,最最常用几个方法:

绝对值:Math.abs(x)

最大值:Math.max([x[, y[, …]]])

最小值:Math.min([x[, y[, …]]])

随机值:Math.random()

向上取整:Math.ceil(x)

向下取整:Math.floor(x)

四舍五入:Math.round(x)

1.概述

Math是一个内置对象,它拥有一些数学常数属性和数学函数方法。Math 不是一个函数对象。Math 用于 Number 类型。

Math 不是一个构造器。Math 的所有属性与方法都是静态的。Math 的常量是使用 JavaScript 中的全精度浮点数来定义的。

2.方法

需要注意的是,三角函数 sin()、cos()、tan()、asin()、acos()、atan() 和 atan2()返回的值是弧度而非角度。

若要转换,弧度除以 (Math.PI / 180) 即可转换为角度,同理,角度乘以这个数则能转换为弧度。

很多 Math 函数都有一个精度,而且这个精度在不同实现中也是不相同的。这意味着不同的浏览器会给出不同的结果,甚至,在不同的系统或架构下,相同的 JS 引擎也会给出不同的结果!

2.1 常用方法

Math.abs(x)   返回一个数的绝对值。

Math.abs('-1');     // 1

Math.abs(-2);       // 2

Math.abs(null);     // 0

Math.abs("string"); // NaN

Math.abs();         // NaN

Math.ceil(x)   返回大于一个数的最小整数,即一个数向上取整后的值。

Math.ceil(.95);        // expected output: 1

Math.ceil(4);        // expected output: 4

Math.ceil(7.004);    // expected output: 8

Math.ceil(-7.004);    // expected output: -7

Math.floor(x)   返回小于一个数的最大整数,即一个数向下取整后的值。

Math.floor( 45.95);     // 45

Math.floor( 45.05);        // 45

Math.floor( 4 );         // 4

Math.floor(-45.05);     // -46

Math.floor(-45.95);     // -46

Math.max([x[, y[, …]]])   返回零到多个数值中最大值。

Math.max(10, 20);   //  20

Math.max(-10, -20); // -10

Math.max(-10, 20);  //  20

Math.min([x[, y[, …]]])   返回零到多个数值中最小值。

Math.max(10, 20);   //  10

Math.max(-10, -20); // -20

Math.max(-10, 20);  //  -10

Math.random()   返回一个 0 到 1 ([0, 1))之间的伪随机数。

Math.random();            // eg:0.3807786079066584

Math.round(x)   返回四舍五入后的整数。

Math.round(20.49);        // 20

Math.round(20.5);        // 21

Math.round(-20.5);        // -20

Math.round(-20.51);        // -21

2.2 其余方法

Math.acos(x)   返回一个数的反余弦值。

Math.acos(-2);  // NaN

Math.acos(-1);  // 3.141592653589793

Math.acos(0);   // 1.5707963267948966

Math.acos(0.5); // 1.0471975511965979

Math.acos(1);   // 0

Math.acos(2);   // NaN

对于小于 -1 或大于 1 的值,Math.acos 返回 NaN。

Math.acosh(x)   返回一个数的反双曲余弦值。

Math.acosh(0.5); // NaN

Math.acosh(1);   // 0

Math.acosh(2);   // 1.3169578969248166

当参数小于1时, Math.acosh()将返回NaN。

Math.asin(x)   返回一个数的反正弦值。

Math.asin(-2);  // NaN

Math.asin(-1);  // -1.5707963267948966 (-pi/2)

Math.asin(0);   // 0

Math.asin(0.5); // 0.5235987755982989

Math.asin(1);   // 1.570796326794897 (pi/2)

Math.asin(2);   // NaN

对于小于 -1 或大于 1 的参数值,Math.asin 返回 NaN。

Math.asinh(x)   返回一个数的反双曲正弦值。

Math.asinh(1);  // 0.881373587019543

Math.asinh(0);  // 0

Math.atan(x)   返回一个数的反正切值。

Math.atan(1);  // 0.7853981633974483

Math.atan(0);  // 0

Math.atanh(x)   返回一个数的反双曲正切值。

Math.atanh(-2);  // NaN

Math.atanh(-1);  // -Infinity

Math.atanh(0);   // 0

Math.atanh(0.5); // 0.5493061443340548

Math.atanh(1);   // Infinity

Math.atanh(2);   // NaN

对于大于1或是小于-1的值,函数返回 NaN 。

Math.atan2(y, x)   返回 y/x 的反正切值。

Math.atan2(90, 15) // 1.4056476493802699

Math.atan2(15, 90) // 0.16514867741462683

Math.atan2( ±0, -0 )               // ±PI.

Math.atan2( ±0, +0 )               // ±0.

Math.atan2( ±0, -x )               // ±PI for x > 0.

Math.atan2( ±0, x )                // ±0 for x > 0.

Math.atan2( -y, ±0 )               // -PI/2 for y > 0.

Math.atan2( y, ±0 )                // PI/2 for y > 0.

Math.atan2( ±y, -Infinity )        // ±PI for finite y > 0.

Math.atan2( ±y, +Infinity )        // ±0 for finite y > 0.

Math.atan2( ±Infinity, x )         // ±PI/2 for finite x.

Math.atan2( ±Infinity, -Infinity ) // ±3*PI/4.

Math.atan2( ±Infinity, +Infinity ) // ±PI/4.

Math.cbrt(x)   返回一个数的立方根。cbrt 是 “cube root” 的缩写, 意思是立方根。

Math.cbrt(NaN); // NaN

Math.cbrt(-1); // -1

Math.cbrt(-0); // -0

Math.cbrt(-Infinity); // -Infinity

Math.cbrt(0); // 0

Math.cbrt(1); // 1

Math.cbrt(Infinity); // Infinity

Math.cbrt(null); // 0

Math.cbrt(2);  // 1.2599210498948734

Math.clz32(x) 返回一个 32 位整数的前导零的数量。

Math.clz32(1)                // 31

Math.clz32(1000)             // 22

Math.clz32()                 // 32

[NaN, Infinity, -Infinity, 0, -0, null, undefined, "foo", {}, []].filter(function (n) {

return Math.clz32(n) !== 32

})                           // []

Math.clz32(true)             // 31

Math.clz32(3.5)

Math.cos(x) 返回一个数的余弦值。

Math.cos(0);           // 1

Math.cos(1);           // 0.5403023058681398

Math.cos(Math.PI);     // -1

Math.cos(2 * Math.PI); // 1

Math.cosh(x) 返回一个数的双曲余弦值。可通过函数模拟实现:

Math.cosh = Math.cosh || function(x) {

var y = Math.exp(x);

return (y + 1 / y) / 2;

};

Math.cosh(0);  // 1

Math.cosh(1);  // 1.5430806348152437

Math.cosh(-1); // 1.543080634815243

Math.exp(x) 返回欧拉常数的参数次方,Ex,其中 x 为参数,E 是欧拉常数(2.718…,自然对数的底数)。

Math.exp(-1); // 0.36787944117144233

Math.exp(0);  // 1

Math.exp(1);  // 2.718281828459045

Math.expm1(x) 返回 exp(x) - 1 的值。其中 x 是该函数的参数, E 是自然对数的底数 2.718281828459045.

Math.expm1(1)     // 1.7182818284590453

Math.expm1(-38)   // -1

Math.expm1("-38") // -1

Math.expm1("foo") // NaN

Math.fround(x)   返回最接近一个数的单精度浮点型表示。

数字1.5可以在二进制数字系统中精确表示,32位和64位的值相同:

Math.fround(1.5); // 1.5

Math.fround(1.5) === 1.5; // true

但是,数字1.337却无法在二进制数字系统中精确表示,所以32位和64位的值是不同的:

Math.fround(1.337); // 1.3370000123977661

Math.fround(1.337) === 1.337; // false

在某些精度不高的场合下,可以通过将二个浮点数转换成32位浮点数进行比较,以解决64位浮点数比较结果不正确的问题:

0.1 + 0.2 == 0.3;    //false

function equal(v1, v2) {

return Math.fround(v1) == Math.fround(v2);

}

equal(0.1 + 0.2, 0.3);   //true

Math.hypot([x[, y[, …]]])   返回其所有参数平方和的平方根。

Math.hypot(3, 4);        // 5

Math.hypot(3, 4, 5);     // 7.0710678118654755

Math.hypot();            // 0

Math.hypot(NaN);         // NaN

Math.hypot(3, 4, 'foo'); // NaN, +'foo' => NaN

Math.hypot(3, 4, '5');   // 7.0710678118654755, +'5' => 5

Math.hypot(-3);          // 3, the same as Math.abs(-3)

Math.imul(x, y)   返回 32 位整数乘法的结果。

Math.log(x)   返回一个数的自然对数(㏒e,即 ㏑)。

Math.log(-1); // NaN, out of range

Math.log(0); // -Infinity

Math.log(1); // 0

Math.log(10); // 2.302585092994046

Math.log1p(x)   返回一个数加 1 的和的自然对数(㏒e,即 ㏑)。

Math.log1p(Math.E-1)  // 1

Math.log1p(0)         // 0

Math.log1p("0")       // 0

Math.log1p(-1)        // -Infinity

Math.log1p(-2)        // NaN

Math.log1p("foo")     // NaN

如果参数的值小于 -1, 则返回 NaN。

Math.log10(x)   返回一个数以 10 为底数的对数。

Math.log10(10)   // 1

Math.log10(100)  // 2

Math.log10("100")// 2

Math.log10(1)    // 0

Math.log10(0)    // -Infinity

Math.log10(-2)   // NaN

Math.log10("foo")// NaN

Math.log2(x)   返回一个数以 2 为底数的对数。

Math.log2(2)     // 1

Math.log2(1024)  // 10

Math.log2(1)     // 0

Math.log2(0)     // -Infinity

Math.log2(-2)    // NaN

Math.log2("1024")// 10

Math.log2("foo") // NaN

Math.pow(x, y)   返回一个数的 y 次幂。

Math.pow(7, 3);        // 343

Math.pow(4, 0.5);    // 2

Math.pow(7, -2);    // 0.02040816326530612 (1/49)

Math.pow(-7, 0.5);    // NaN

Math.random()   返回一个 0 到 1 ([0, 1))之间的伪随机数。

Math.random();            // eg:0.3807786079066584

Math.sign(x)   返回一个数的符号,得知一个数是正数、负数还是 0。此函数共有5种返回值, 分别是 1, -1, 0, -0, NaN. 代表的各是正数, 负数, 正零, 负零, NaN。

Math.sign(3);     //  1

Math.sign(-3);    // -1

Math.sign("-3");  // -1

Math.sign(0);     //  0

Math.sign(-0);    // -0

Math.sign(NaN);   // NaN

Math.sign("foo"); // NaN

Math.sign();      // NaN

Math.sin(x)   返回一个数的正弦值。

Math.sin(0);           // 0

Math.sin(1);           // 0.8414709848078965

Math.sin(Math.PI / 2); // 1

Math.sinh(x)   返回一个数的双曲正弦值。 双曲正弦的图像如下:

Math.sinh(0)      // 0

Math.sinh(1)      // 1.1752011936438014

Math.sinh("-1")   // -1.1752011936438014

Math.sinh("foo")  // NaN

Math.sqrt(x)   返回一个数的平方根。

Math.sqrt(9); // 3

Math.sqrt(2); // 1.414213562373095

Math.sqrt(1);  // 1

Math.sqrt(0);  // 0

Math.sqrt(-1); // NaN

Math.sqrt(-0); // -0

Math.tan(x) 返回一个数的正切值。 Math.tanh(x)   返回一个数的双曲正切值。

Math.tanh(0);        // 0

Math.tanh(Infinity); // 1

Math.tanh(1);        // 0.7615941559557649

Math.toSource()   返回字符串 “Math”。 Math.trunc(x)   返回一个数的整数部分,仅仅是删除掉数字的小数部分和小数点,不管参数是正数还是负数。传入该方法的参数会被隐式转换成数字类型。

Math.trunc(13.37)    // 13

Math.trunc(42.84)    // 42

Math.trunc(0.123)    //  0

Math.trunc(-0.123)   // -0

Math.trunc("-1.123") // -1

Math.trunc(NaN)      // NaN

Math.trunc("foo")    // NaN

Math.trunc()         // NaN

3.属性

Math.E   欧拉常数,也是自然对数的底数,约等于 2.718。 Math.LN2   2 的自然对数,约等于 0.693。 Math.LN10   10 的自然对数,约等于 2.303。 Math.LOG2E   以 2 为底的 E 的对数,约等于 1.443。 Math.LOG10E   以 10 为底的 E 的对数,约等于 0.434。 Math.PI   圆周率,一个圆的周长和直径之比,约等于 3.14159。 Math.SQRT1_2   二分之一 ½ 的平方根,同时也是 2 的平方根的倒数,约等于 0.707。 Math.SQRT2   2 的平方根,约等于 1.414。

math的向上取整_Javascript中Math常用操作,向上取整、向下取整、四舍五入相关推荐

  1. python怎么取百位_#python计算结果百位500向下取整,(0-499取000,500-999取500)

    !/usr/bin/env python coding:utf-8 计算结果百位500向下取整,(0-499取000,500-999取500) import math calc_Amount = fl ...

  2. MYSQL 中 SQL 常用操作

    SQL常用操作大全 1.SQL简单的操作 -- 常见SQL-- 查询 SELECT id,username,password FROM admin;-- 插入 INSERT INTO admin(us ...

  3. C语言大数阶乘取余,python中math模块常用函数介绍 取模(取余)取绝对值 求阶乘 求最大公约数最小公倍数 取对数 取根号 取幂(取次方) 取整函数 三角函数与反三角函数...

    前提:import math 两个常用常量 e = 2.718281828459045 pi = 3.141592653589793 >>> import math >> ...

  4. 数据分析中的常用数学模型实战教程笔记(下)

    文章目录 SVM模型 代码操作 手写体字母识别 用最佳参数做预测 使用默认参数做预测 森林火灾可能性预测 Kmeans-K均值聚类模型 随机一个三组二元正态分布随机数 拐点法 轮廓系数法 函数代码 花 ...

  5. 【位运算】代码中的常用操作

    作者:力扣(LeetCode) 链接:https://www.zhihu.com/question/38206659/answer/736472332 来源:知乎 著作权归作者所有.商业转载请联系作者 ...

  6. Java中Cookie常用操作类(Spring中操作Cookie)

    说明:Cookie下用Key取值没有快速的方法,只能便利循环去取. 技巧:置0则cookie会立即删除,设置-1,负值则会在关闭浏览器后删除.切记一定要增加路径:setPath("/&quo ...

  7. linux中sed切割方法,Linux中Sed常用操作

    1. Sed简介 sed 是一种在线编辑器,它一次处理一行内容.处理时,把当前处理的行存储在临时缓冲区中,称为"模式空间"(pattern space),接着用sed命令处理缓冲区 ...

  8. CentOS中Mysql常用操作

    安装mysql yum -y install mysql-server 修改mysql配置vi /etc/my.cnf 这里会有很多需要注意的配置项,后面会有专门的笔记 暂时修改一下编码(添加在密码下 ...

  9. Shell中的常用操作

    文章目录 1 读取从键盘输入的数据 2 退出当前进程 3 对整数进行数学运算 4 逻辑与和或 5 检测某个条件是否成立 1 读取从键盘输入的数据 我们可以使用read进行读取,示例如下: read - ...

最新文章

  1. Android Editext监听光标位置
  2. 4、ShardingSphere 之 Sharding-JDBC 实现垂直分库
  3. figma下载_Figma和ProtoPie中的原型制作,比较
  4. IOS web app一些实用的属性设置
  5. html图片多边形怎么写,使用CSS3构建的图像多边形裁剪动画特效
  6. IE6、IE7、IE8、Firefox通用关闭窗口js
  7. Python使用threading实现多线程
  8. H3C实验H3CTE讲师京东翰林分享实验4 WLAN基本配置
  9. Ubuntu16安装GTK+2.0教程
  10. python 线程安全的数据类型_详解python多线程、锁、event事件机制的简单使用
  11. 字符串 - KMP模式匹配
  12. proc除了能用于oracle开发_能不能用于mysql开发_MySQL 和 Oracle Enterprise Manager 开发人员 VM...
  13. 两轮差速驱动机器人运动模型及应用分析
  14. svm各种工具箱(先放着了,省的找起来麻烦^.^)
  15. linux unix域socket_计算机通信之谜,带你彻底理解socket网络编程(一)
  16. 手把手教你升级到MySQL 8.0
  17. 数据抽取常见的几种模式
  18. 百度学术打开不了?怎么办
  19. 2021不堪回首,2022满路荆棘,但依然乐观努力
  20. 加速度计和陀螺仪设备

热门文章

  1. 2021年湖北个人怎么考安全员证也就是安全员C1C2C3证
  2. 全网最全15家银行提额秘籍
  3. html table专业样式,html 简单的table样式
  4. MySQL - SQL窗口函数
  5. 如何为网站标题添加logo图片
  6. 旧手机搭建Linux服务器
  7. 养生保健大全-熬夜吗
  8. 数据分析学习——数据清洗
  9. Java Bean解析。什么是Javabean?
  10. user not exist mysql_php连接mysql出错 Table 'test.user' doesn't exist