Math 是javaScript的内置对象,包含了部分数学常数属性和数学函数方法。
Math 不是一个函数对象,用户Number类型进行使用,不支持BigInt。
Math 的所有属性与方法都是静态的。
比如说当我们使用圆周率的时候,写法是 Math.PI
当使用正余弦函数的写法是 Math.sin(x),x 是要传入的参数。
Math 的常量是使用 JavaScript 中的全精度浮点数来定义的。
// 欧拉常数,也是自然对数的底数,约等于 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.4142135623730951Math.abs() // 指定数字 “x“ 的绝对值Math.abs("-1"); // 1Math.abs(-2); // 2Math.abs(null); // 0Math.abs("string"); // NaNMath.abs(); // NaN// 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() 函数返回一个浮点数 * 伪随机数在范围从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更多方法请查阅文档
Math文档
文档地址
https://mathjs.org/examples/index.html
mathjs的插件提供的方法比较全面,涵盖了从代数计算到函数计算,货币运算等方法,矩阵序列化等,更多方法可以查看官方文档。
基础使用方法:
npm install mathjsimport { sqrt } from 'mathjs'console.log(sqrt(-4).toString()) // 2i文章个人博客地址:javaScript中Math内置对象基本方法入门
欢迎关注公众号:程序猿布欧,不定期更新一些前端入门文章
创作不易,转载请注明出处和作者。