Math 对象es6上面的扩展
2016-11-16 17:30
375 查看
Math.trunc()
Math.trunc方法用于去除一个数的小数部分,返回整数部分。
Math.trunc(4.1) // 4
Math.trunc(4.9) // 4
Math.trunc(-4.1) // -4
Math.trunc(-4.9) // -4
Math.trunc(-0.1234) // -0
Math.trunc('123.456')// 123
Math.trunc(NaN); // NaN
Math.trunc('foo'); // NaN
Math.trunc(); // NaN
Math.sign()
Math.sign方法用来判断一个数到底是正数、负数、还是零。它会返回五种值。
参数为正数,返回 +1 ;
参数为负数,返回 -1 ;
参数为 0 ,返回 0 ;
参数为 -0 ,返回 -0;
其他值,返回 NaN
Math.sign(-5) // -1
Math.sign(5) // +1
Math.sign(0) // +0
Math.sign(-0) // -0
Math.sign(NaN) // NaN
Math.sign('foo'); // NaN
Math.sign(); // NaN
Math.cbrt()
Math.cbrt方法用于计算一个数的立方根。
Math.cbrt(-1) // -1
Math.cbrt(0) // 0
Math.cbrt(1) // 1
Math.cbrt(2) // 1.2599210498948734
Math.cbrt('8') // 2
Math.cbrt('hello') // NaN
Math.clz32()
JavaScript 的整数使用 32 位二进制形式表示,Math.clz32方法返回一个数的 32 位无符号整数形式有多少个前导 0 。Math.clz32(0) // 32
Math.clz32(1) // 31
Math.clz32(1000) // 22
Math.clz32(0b01000000000000000000000000000000) // 1
Math.clz32(0b00100000000000000000000000000000) // 2
Math.imul()
Math.imul方法返回两个数以 32 位带符号整数形式相乘的结果,返回的也是一个 32 位的带符号整数。Math.imul(2, 4) // 8
Math.imul(-1, 8) // -8
Math.imul(-2, -2) // 4
Math.fround()
Math.fround 方法返回一个数的单精度浮点数形式。
对于整数来说,Math.fround方法返回结果不会有任何不同,区别主要是那些无法用 64 个二进制位精确表示的小数。这时,Math.fround方法会返回最接近这个小数的单精度浮点数Math.fround(0) // 0
Math.fround(1) // 1
Math.fround(1.337) // 1.3370000123977661
Math.fround(1.5) // 1.5
Math.fround(NaN) // NaN
Math.hypot()
Math.hypot方法返回所有参数的平方和的平方根。
如果参数不是数值,Math.hypot方法会将其转为数值。只要有一个参数无法转为数值,就会返回 NaN 。
ES6 新增了 6 个三角函数方法。
Math.sinh(x) 返回x的双曲正弦( hyperbolic sine )
Math.cosh(x) 返回x的双曲余弦( hyperbolic cosine )
Math.tanh(x) 返回x的双曲正切( hyperbolic tangent )
Math.asinh(x) 返回x的反双曲正弦( inverse hyperbolic sine )
Math.acosh(x) 返回x的反双曲余弦( inverse hyperbolic cosine )
Math.atanh(x) 返回x的反双曲正切( inverse hyperbolic tangent )
Math.trunc方法用于去除一个数的小数部分,返回整数部分。
Math.trunc(4.1) // 4
Math.trunc(4.9) // 4
Math.trunc(-4.1) // -4
Math.trunc(-4.9) // -4
Math.trunc(-0.1234) // -0
Math.trunc('123.456')// 123
Math.trunc(NaN); // NaN
Math.trunc('foo'); // NaN
Math.trunc(); // NaN
Math.sign()
Math.sign方法用来判断一个数到底是正数、负数、还是零。它会返回五种值。
参数为正数,返回 +1 ;
参数为负数,返回 -1 ;
参数为 0 ,返回 0 ;
参数为 -0 ,返回 -0;
其他值,返回 NaN
Math.sign(-5) // -1
Math.sign(5) // +1
Math.sign(0) // +0
Math.sign(-0) // -0
Math.sign(NaN) // NaN
Math.sign('foo'); // NaN
Math.sign(); // NaN
Math.cbrt()
Math.cbrt方法用于计算一个数的立方根。
Math.cbrt(-1) // -1
Math.cbrt(0) // 0
Math.cbrt(1) // 1
Math.cbrt(2) // 1.2599210498948734
Math.cbrt('8') // 2
Math.cbrt('hello') // NaN
Math.clz32()
JavaScript 的整数使用 32 位二进制形式表示,Math.clz32方法返回一个数的 32 位无符号整数形式有多少个前导 0 。Math.clz32(0) // 32
Math.clz32(1) // 31
Math.clz32(1000) // 22
Math.clz32(0b01000000000000000000000000000000) // 1
Math.clz32(0b00100000000000000000000000000000) // 2
Math.imul()
Math.imul方法返回两个数以 32 位带符号整数形式相乘的结果,返回的也是一个 32 位的带符号整数。Math.imul(2, 4) // 8
Math.imul(-1, 8) // -8
Math.imul(-2, -2) // 4
Math.fround()
Math.fround 方法返回一个数的单精度浮点数形式。
对于整数来说,Math.fround方法返回结果不会有任何不同,区别主要是那些无法用 64 个二进制位精确表示的小数。这时,Math.fround方法会返回最接近这个小数的单精度浮点数Math.fround(0) // 0
Math.fround(1) // 1
Math.fround(1.337) // 1.3370000123977661
Math.fround(1.5) // 1.5
Math.fround(NaN) // NaN
Math.hypot()
Math.hypot方法返回所有参数的平方和的平方根。
如果参数不是数值,Math.hypot方法会将其转为数值。只要有一个参数无法转为数值,就会返回 NaN 。
Math.hypot(3, 4); // 5 3 的平方加上 4 的平方,等于 5 的平方。 Math.hypot(3, 4, 5); // 7.0710678118654755 Math.hypot(); // 0 Math.hypot(NaN); // NaN Math.hypot(3, 4, 'foo'); // NaN Math.hypot(3, 4, '5'); // 7.0710678118654755 Math.hypot(-3); // 3
ES6 新增了 6 个三角函数方法。
Math.sinh(x) 返回x的双曲正弦( hyperbolic sine )
Math.cosh(x) 返回x的双曲余弦( hyperbolic cosine )
Math.tanh(x) 返回x的双曲正切( hyperbolic tangent )
Math.asinh(x) 返回x的反双曲正弦( inverse hyperbolic sine )
Math.acosh(x) 返回x的反双曲余弦( inverse hyperbolic cosine )
Math.atanh(x) 返回x的反双曲正切( inverse hyperbolic tangent )
相关文章推荐
- ES6 对象的扩展 简单测试
- 【ES6】对象的扩展2
- ES6(八) 对象的扩展
- ES6对象扩展
- ES6 对象扩展
- ES6学习——新的语法:对象字面量扩展(Object Literal Extensions)
- ES6--对象的扩展
- JavaScript对象扩展(Object.is(), assign(), 对象遍历keys, es6原型介绍)
- 如何理解es6语法的,对象前面加三个点(...) 对象的解构赋值和扩展
- [js高手之路] es6系列教程 - 对象功能扩展详解
- ES6对象的扩展
- ES6学习——新的语法:对象字面量扩展(Object Literal Extensions)
- ES6新特性之数组、Math和扩展操作符用法示例
- ES6对象扩展
- Javascript对象扩展 - Math
- ES6新特性-------数组、Math和扩展操作符(续)
- ECMAScript6(ES6)标准之对象Object扩展方法及新特性
- ES6--对象、函数的扩展
- es6对象扩展(七)
- ES6 —(对象的扩展)