ES6 |数学
ES6 Math包含许多属性和方法来执行一些数学函数。 ES6 是一个无需创建即可调用的对象。借助 JavaScript 中的ES6 Math ,您可以以二进制和八进制表示法的形式存储整数。 ES6 Math 中有很多可用的属性和函数。同样在函数部分,还有很多下面提到的各种功能。
ES6 Math() 函数类型:
- 指数函数
- 对数函数
- 杂项代数函数
- 三角函数
带有简要说明的 ES6 数学属性列表:
Property | Description |
---|---|
Math-E Property | This property define the Euler’s constant(2.718) |
Math-LN2 Property | This property defines the natural log2(0.693) |
Math-LN10 Property | This property defines the natural log10(2.302) |
Math-LOG2E Property | This property defines the base 2 logarithm of E(1.442) |
Math-LOG10E Property | This property defines the base 10 logarithm of E(0.434) |
Math-PI Property | This property defines the ratio of the circumference of a circle to its diameter(3.14159) |
Math-SQRT1_2 Property | This property defines the square root of 1/2(0.707) |
Math-SQRT2 Property | This property defines the square root of 2(1.414) |
ES6 数学指数函数:有很多函数,如平方根、立方根和 e 的幂,如下表所示。 Math.pow()是指数函数的基本函数。Function Description Math.Pow() Function This function returns the base to the exponent power. Math.sqrt() Function This function returns the square root of a number. Math.cbrt() Function This function returns the cube root of a number. Math.exp() Function This function is equivalent to Math Pow() Function. Math.expm1(X) Function This function is equivalent to Math exp()-1. Math.hypot(x1, x2, …) Function This function returns the square root of the sum of the arguments.
ES6 数学对数函数:在对数部分有 4 个函数,但最基本的是 Math.log()函数,这是自然对数。 ES6 在 ES6 之前在 JavaScript 中引入了Math.log10()函数,整个 JavaScript 中没有 log10。
Function | Description |
---|---|
Math.log(x) Function | This function returns the natural logarithm of X. |
Math.log10(x) Function | This function returns the base 10 logarithm of X. |
Math.log2(x) Function | This function returns the base 2 logarithm of X. |
Math.log1p(x) Function | This function returns the natural logarithm of 1+x. |
ES6 Math Miscellaneous Algebraic Functions:本节中有很多函数,下面简要介绍一下。Function Description Math.abs() Function This functions returns the exact value of the number. Math.sign() Function This functions returns the sign of the exact number. Math.ceil() Function This function returns the smallest integers greater than or equal to the exact number mens the ceiling value. Math.floor() Function This function returns the floor value of the exact number. Math.trunc() Function This function returns the trim value of exact number means remove the decimal right digit. Math.round() Function This function returns the rounds of the exact number. Math.min() Function This function returns the smallest of zero or more numbers if no arguments are given the it will return the +infinity. Math.max() Function This function returns the largest of zero or more numbers if no arguments are given the it will return the -infinity.
ES6数学三角函数:三角函数中有很多函数,但所有函数都处理弧度,但度数在本节中无效。Function Description Math.sin() Function This function return the sin of exact number. Math.cos() Function This function return the cosine of exact number. Math.tan() Function This function return the tangent of exact number. Math.asin() Function This function return the inverse sin of exact number. Math.acos() Function This function return the inverse cosine of exact number. Math.atan() Function This function return the inverse tangent of exact number. Math.atan2() Function This function returns a numeric value between -pi and pi representing the angle theta of exact two point.
还有一个不属于上述任何类别的函数是Math.random()函数。该函数返回 0 到 1 之间的随机数。
下面的例子说明了这个函数:
例子:
javascript
输出
The random function value : 0.603934617864549
注意: Edge 和 Safari 是支持所有 ES6 功能的前两个浏览器。
支持的浏览器: ES6 Math 支持的浏览器如下:
- 谷歌浏览器
- IE浏览器
- 火狐
- 苹果浏览器
- 歌剧