概念
- Math 是javaScript的内置对象,蕴含了局部数学常数属性和数学函数办法。
- Math 不是一个函数对象,用户Number类型进行应用,不反对BigInt。
- Math 的所有属性与办法都是动态的。
- 比如说当咱们应用圆周率的时候,写法是 Math.PI
- 当应用正余弦函数的写法是 Math.sin(x),x 是要传入的参数。
- Math 的常量是应用 JavaScript 中的全精度浮点数来定义的。
math原生属性
// 欧拉常数,也是自然对数的底数,约等于 2.718。console.log("Math.E", Math.E); // Math.E 2.718281828459045// 2 的自然对数,约等于 0.693。console.log("Math.LN2", Math.LN2); // Math.LN2 0.6931471805599453// 10 的自然对数,约等于 2.303。console.log("Math.LN10", Math.LN10); // Math.LN10 2.302585092994046// 以 2 为底的 E 的对数,约等于 1.443。console.log("Math.LOG2E", Math.LOG2E); // Math.LOG2E 1.4426950408889634// 以 10 为底的 E 的对数,约等于 0.434。console.log("Math.LOG10E", Math.LOG10E); // Math.LOG10E 0.4342944819032518// 圆周率,一个圆的周长和直径之比,约等于 3.14159。console.log("Math.PI", Math.PI); // Math.PI 3.141592653589793// 计算圆周长function calculateCircumference(radius) { return 2 * Math.PI * radius;}console.log("calculateCircumference(1)", calculateCircumference(1)); // calculateCircumference(1) 6.283185307179586// 二分之一 ½ 的平方根,同时也是 2 的平方根的倒数 1 2 ,约等于 0.707。console.log("Math.SQRT1_2", Math.SQRT1_2); // Math.SQRT1_2 0.7071067811865476// 2 的平方根,约等于 1.414。console.log("Math.SQRT2", Math.SQRT2); // Math.SQRT2 1.4142135623730951
math罕用办法
Math.abs() // 指定数字 “x“ 的绝对值Math.abs("-1"); // 1Math.abs(-2); // 2Math.abs(null); // 0Math.abs("string"); // NaNMath.abs(); // NaN
math在日常开发中的数字解决办法
// Math.round() 函数返回一个数字四舍五入后最靠近的整数。console.log(Math.round(20.49)); //20console.log(Math.round(20.5)); //21console.log(Math.round(-20.5)); //-20console.log(Math.round(-20.51)); //-21// Math.ceil() 返回大于或等于一个给定数字的最小整数,向上取整。console.log(Math.ceil(0.95));// 1console.log(Math.ceil(4));// 4console.log(Math.ceil(7.004));// 8console.log(Math.ceil(-7.004));// -7// Math.floor() 返回小于或等于一个给定数字的最大整数, Math.floor()为向下取整。Math.floor(45.95);// 45Math.floor(45.05);// 45Math.floor(4);// 4Math.floor(-45.05);// -46Math.floor(-45.95);// -46// Math.max() 返回一组数当中的最大值console.log(Math.max(1, 3, 2));// 3console.log(Math.max(-1, -3, -2));// -1const array1 = [1, -3, 2];console.log(Math.max(...array1));// 3// Math.min() 返回零个或更多个数值的最小值。console.log(Math.min()); // Infinityconsole.log(Math.min(1, 2, 3, -4)); // -4// 应用 Math.min() 裁剪值(Clipping a value)function f(x) { if (x > 5) { return (x = 5); } return (x = 6);}var finalMin = Math.min(f(2), 2, 3, 4, 5, 30);console.log("finalMin", finalMin); // 2// Math.sqrt() 返回一个数的平方根function calcHypotenuse(a, b) { return Math.sqrt(a * a + b * b);}console.log(calcHypotenuse(3, 4));// 5console.log(calcHypotenuse(5, 12));// 13console.log(calcHypotenuse(0, 0));// 0
应用Math.random()生成随机数
/** * * Math.random() 函数返回一个浮点数 * 伪随机数在范畴从0到小于1,也就是说,从0(包含0)往上,然而不包含1(排除1), * 而后您能够缩放到所需的范畴。实现将初始种子抉择到随机数生成算法;它不能被用户抉择或重置。 * * */console.log(Math.random());function getRandomNumber(min, max) { min = Math.ceil(min); max = Math.floor(max); return Math.floor(Math.random() * (max - min + 1)) + min; //含最大值,含最小值}console.log(getRandomNumber(2, 100));
小结
- 以上例子蕴含了math罕用的办法和属性的api
- math在应用过程中,能够联合random以及max和min办法等,生成须要的随机数
- 通过round、floor、ceil,咱们能够针对数字进行进一步地取值,失去符合要求的数字格局
Math更多办法请查阅文档
Math文档
Mathjs插件
[文档地址
https://mathjs.org/examples/i...](https://note.youdao.com/)
- mathjs的插件提供的办法比拟全面,涵盖了从代数计算到函数计算,货币运算等办法,矩阵序列化等,更多办法能够查看官网文档。
- 根底应用办法:
npm install mathjsimport { sqrt } from 'mathjs'console.log(sqrt(-4).toString()) // 2i
源码地址
码云 https://gitee.com/lewyon/vue-note
githup https://github.com/akari16/vue-note
文章集体博客地址:javaScript中Math内置对象根本办法入门
欢送关注公众号:程序猿布欧,不定期更新一些前端入门文章
创作不易,转载请注明出处和作者。