ES6 Math方法和Number新特性简介 « 张鑫旭

您所在的位置:网站首页 es6 ES6 Math方法和Number新特性简介 « 张鑫旭

ES6 Math方法和Number新特性简介 « 张鑫旭

2022-07-07 11:39| 来源: 网络整理| 查看: 265

if (!Math.fround) Math.fround = function(arg) { arg = Number(arg); // 如果是 ±0 和 NaN 直接返回 if (!arg) return arg; var sign = arg // 计算指数(8位,有符号)。 var exp = Math.floor(Math.log(arg) / Math.LN2); var powexp = Math.pow(2, Math.max(-126, Math.min(exp, 127))); // 处理比较小的值:如果指数位都为零,则前导数字为零。 var leading = exp // 尾数23位之后的变成0。 var mantissa = Math.round((leading - arg / powexp) * 0x800000); if (mantissa Math.imul(x, y)

将两个32位整数x和y相乘,并返回类C的32位结果。

这是目前JS中唯一一个32位的基本数学运算,可能有人使用过JavaScript操作符模拟32位运算。例如,idiv可以实现如下:

function idiv(x, y) { return (x / y) | 0; }

这种模拟是不行的,将两个32位大整数相乘可能会产生一个过大的双精度数,导致靠后的很多位数丢失。

使用示意:

Math.imul(2, 4); // 8 Math.imul(-1, 8); // -8 Math.imul(-2, -2); // 4 Math.imul(0xffffffff, 5); // -5 Math.imul(0xfffffffe, 5); // -10

IE浏览器的Polyfill如下:

if (!Math.imul) Math.imul = function(a, b) { var aHi = (a >>> 16) & 0xffff; var aLo = a & 0xffff; var bHi = (b >>> 16) & 0xffff; var bLo = b & 0xffff; // 0的移位固定了高位部分的符号 // 最后的| 0将无符号值转换为有符号值 return ((aLo * bLo) + (((aHi * bLo + aLo * bHi) >> 0) | 0); }; Math.clz32(x)

Math.clz32()返回参数x的32位二进制表示形式中的前导零位数。

“clz32”是CountLeadingZeroes32的缩写。

如果x不是数字,则它将首先转换为数字,然后转换为32位无符号整数。

如果转换后的32位无符号整数是0,则返回32,因为所有位都是0。

这个函数对于编译成JS的系统特别有用,比如Emscripten。

使用示意:

// 00000000000000000000000000000001 console.log(Math.clz32(1)); // 预期输出: 31 // 00000000000000000000000000000100 console.log(Math.clz32(4)); // 预期输出: 29 // 00000000000000000000001111101000 console.log(Math.clz32(1000)); // 预期输出: 22 Math.clz32(1); // 31 Math.clz32(1000); // 22 Math.clz32(); // 32 var stuff = [NaN, Infinity, -Infinity, 0, -0, null, undefined, 'foo', {}, []]; stuff.every(n => Math.clz32(n) == 32); // true Math.clz32(true); // 31 Math.clz32(3.5); // 30

下面的polyfill是最有效的:

if (!Math.clz32) Math.clz32 = (function(log, LN2){ return function(x) { var asUint = x >>> 0; if (asUint === 0) { return 32; } return 31 - (log(asUint) / LN2 | 0) |0; // "| 0" 作用类似于math.floor }; })(Math.log, Math.LN2); Math.hypot(...values)

Math.hypot()返回参数平方和的平方根。

语法:

Math.hypot([value1[, value2[, ...]]]);

使用示意:

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)

IE浏览器不支持此方法,可以引入下面的JS代码使之兼容:

if (!Math.hypot) Math.hypot = function() { var y = 0, i = arguments.length; while (i--) y += arguments[i] * arguments[i]; return Math.sqrt(y); };

此方法适合计算两个点之间的距离。

let distance = Math.hypot(y2-y1, x2-x1); Math.sinh(x)

Math.sinh()返回参数x的双曲正弦值。

使用示意:

Math.sinh(0); // 0 Math.sinh(1); // 1.1752011936438014 Math.sinh(-1); // -1.1752011936438014 Math.sinh(2); // 3.626860407847019

Polyfill代码如下:

Math.sinh = Math.sinh || function(x) { return (Math.exp(x) - Math.exp(-x)) / 2; } Math.cosh(x)

Math.cosh()返回参数x的双曲余弦值。

使用示意:

Math.cosh(0); // 1 Math.cosh(1); // 1.543080634815244 Math.cosh(-1); // 1.543080634815244 Math.cosh(2); // 3.7621956910836314

Polyfill代码如下:

Math.cosh = Math.cosh || function(x) { return (Math.exp(x) + Math.exp(-x)) / 2; } Math.tanh(x)

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

使用示意:

Math.tanh(0); // 0 Math.tanh(Infinity); // 1 Math.tanh(1); // 0.7615941559557649 Math.tanh(-1); // -0.7615941559557649

Polyfill代码如下:

Math.tanh = Math.tanh || function(x){ var a = Math.exp(+x), b = Math.exp(-x); return a == Infinity ? 1 : b == Infinity ? -1 : (a - b) / (a + b); } Math.asinh(x)

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

使用示意:

Math.asinh(1); // 0.881373587019543 Math.asinh(0); // 0 Math.asinh(-1); // -0.881373587019543 Math.asinh(2) // 1.4436354751788103

Polyfill代码如下:

Math.asinh = Math.asinh || function(x) { if (x === -Infinity) { return x; } else { return Math.log(x + Math.sqrt(x * x + 1)); } }; Math.acosh(x)

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

小于1的参数会返回NaN。

使用示意:

Math.acosh(-1); // NaN Math.acosh(0); // NaN Math.acosh(0.5); // NaN Math.acosh(1); // 0 Math.acosh(2); // 1.3169578969248166

Polyfill代码如下:

Math.acosh = Math.acosh || function(x) { return Math.log(x + Math.sqrt(x * x - 1)); }; Math.atanh(x)

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

小于1或者大于1的参数会返回NaN。

使用示意:

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

Polyfill代码如下:

Math.atanh = Math.atanh || function(x) { return Math.log((1+x)/(1-x)) / 2; }; 二、ES6中的Number方法 Number.isFinite(number)

Number.isFinite()方法用来确定传递的值是否为有限值。

使用示意:

Number.isFinite(Infinity); // false Number.isFinite(NaN); // false Number.isFinite(-Infinity); // false Number.isFinite(0); // true Number.isFinite(2e64); // true Number.isFinite('0'); // false, 全局的isFinite('0')返回值是true Number.isFinite(null); // false, 全局的isFinite(null)返回值是true

如果想要在低版本浏览器中使用,可以引入下面这段JavaScript脚本:

if (Number.isFinite === undefined) Number.isFinite = function(value) { return typeof value === 'number' && isFinite(value); } Number.isNaN(number)

Number.isNaN()方法用来判断参数x是否是NaN,同时类型是Number,这是一个比最初的、全局的isNaN()方法更健壮的版本。

使用示意:

Number.isNaN(NaN); // true Number.isNaN(Number.NaN); // true Number.isNaN(0 / 0); // true // 下面这些参数如果使用全局的isNaN()方法都是返回true Number.isNaN('NaN'); // false Number.isNaN(undefined); // false Number.isNaN({}); // false Number.isNaN('blabla'); // false // 下面的全部返回false Number.isNaN(true); Number.isNaN(null); Number.isNaN(37); Number.isNaN('37'); Number.isNaN('37.37'); Number.isNaN(''); Number.isNaN(' ');

此方法并不是所有浏览器都支持的,需要打个小补丁:

Number.isNaN = Number.isNaN || function isNaN(input) { return typeof input === 'number' && input !== input; } Number.parseFloat和Number.parseInt

语法如下:

Number.parseFloat(string) Number.parseInt(string, radix)

Number.parseFloat方法和全局的parseFloat方法是一样的方法。

Number.parseFloat === parseFloat; // true

Number.parseInt方法和全局的parseInt方法是一样的方法。

Number.parseInt === parseInt; // true Number.EPSILON

Number.EPSILON是一个数值很小的常量:

Number.EPSILON == 2.220446049250313e-16; // true

可以用来准确比较浮点值。

例如:

0.1 + 0.2 === 0.3; // false

可以借助Number.EPSILON进行准备比较:

function epsEqu(x, y) { return Math.abs(x - y) // true Number.isInteger(number)

JavaScript只有浮点数(双精度)。因此,整数只是不带小数的浮点数。

Number.isInteger()方法可以判定数字是不是没有小数,如果没有,则返回true。

使用示意:

Number.isInteger(0); // true Number.isInteger(1); // true Number.isInteger(-100000); // true Number.isInteger(99999999999999999999999); // true Number.isInteger(0.1); // false Number.isInteger(Math.PI); // false Number.isInteger(NaN); // false Number.isInteger(Infinity); // false Number.isInteger(-Infinity); // false Number.isInteger('10'); // false Number.isInteger(true); // false Number.isInteger(false); // false Number.isInteger([1]); // false Number.isInteger(5.0); // true Number.isInteger(5.000000000000001); // false Number.isInteger(5.0000000000000001); // true

由于是新特性,不是所有浏览器都支持这个方法,如果想要额外兼容下,可以参考下面的语句:

Number.isInteger = Number.isInteger || function(value) { return typeof value === 'number' && isFinite(value) && Math.floor(value) === value; }; Safe Integers

检测是否是合法范围内的整数。包括下面1个方法和2个常量:



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3