Node.js最大安全浮点数 [英] Node.js Maximum Safe Floating-point Number

查看:72
本文介绍了Node.js最大安全浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Node.js中,是否有一个最大的安全浮点数,例如 Number.MAX_SAFE_INTEGER ?

In Node.js, is there a maximum safe floating-point number like Number.MAX_SAFE_INTEGER?

我做了一个小实验,找出可以用来从中减去0.13的(近似)数字:

I had a little experiment to find out the (approximate) number I can use for subtracting 0.13 from it:

console.log(Math.floor(Number.MAX_SAFE_INTEGER));  // 9007199254740991
console.log(Math.floor(Number.MAX_SAFE_INTEGER)-0.13);  // 9007199254740991

console.log(Math.floor(Number.MAX_SAFE_INTEGER/2));  // 4503599627370495
console.log(Math.floor(Number.MAX_SAFE_INTEGER/2)-0.13);  // 4503599627370495

console.log(Math.floor(Number.MAX_SAFE_INTEGER/4));  // 2251799813685247
console.log(Math.floor(Number.MAX_SAFE_INTEGER/4)-0.13);  // 2251799813685246.8

console.log(Math.floor(Number.MAX_SAFE_INTEGER/64));  // 140737488355327
console.log(Math.floor(Number.MAX_SAFE_INTEGER/64)-0.13);  // 140737488355326.88

console.log(Math.floor(Number.MAX_SAFE_INTEGER/128));  // 70368744177663
console.log(Math.floor(Number.MAX_SAFE_INTEGER/128)-0.13);  // 70368744177662.87

我的猜测是,随着目标精度的提高,最大值会降低.

My guess is that as the target precision increases, the maximum value decreases.

推荐答案

要精确到1个十进制数字,您可以使用的最大数字为 562949953421311 .

要精确到2个十进制数字,它是 70368744177663 .有趣的是,第一个数字等于:

To a precision of 1 decimal digits, the maximum number you can work with is 562949953421311.

To a precision of 2 decimal digits, it's 70368744177663. Interestingly, the first number is equal to:

(Number.MAX_SAFE_INTEGER + 1) / 16 - 1

第二个数字等于:

(Number.MAX_SAFE_INTEGER + 1) / 128 - 1

我们要寻找的是支持小数点后 d 位精度的最大安全数字.支持"是指可以可靠地进行基本算术".

What we're looking for, is the maximum safe number to support a precision of d digits after the decimal point. By "support" I mean "can reliably do basic arithmetic".

例如,我们知道 Number.MAX_SAFE_INTEGER(aka 2 ** 53-1)安全的,因为基本算术已损坏:

For example, we know that Number.MAX_SAFE_INTEGER (aka 2**53-1) is not safe, because basic arithmetic is broken:

Number.MAX_SAFE_INTEGER - 0.1 === Number.MAX_SAFE_INTEGER
>>> true // unsafe

我们知道0 是安全的,因为:

And we know that 0 is safe, since:

0 + 0.1 === 0
>>> false // safe

BTW, 0 1e-323 (包括)方面是可靠的:

BTW, 0 is reliable as far as 1e-323 (including):

0 + 1e-323 === 0
>>> false // safe

0 + 1e-324 === 0
>>> true // unsafe

我在0到 Number.MAX_SAFE_INTEGER 之间进行了二进制搜索,找到了满足该定义的最大数字,并得出了这些数字.

I binary-searched between 0 and Number.MAX_SAFE_INTEGER for the biggest number that answers that definition, and came up with these numbers.

这是代码(在代码段末尾将其他任何数字传递给 findMaxSafeFloat())

Here's the code (pass any other number to findMaxSafeFloat() at the end of snippet)

/**Returns whether basic arithmetic breaks between n and n+1, to a precision of `digits` after the decimal point*/
function isUnsafe(n, digits) {
  // digits = 1 loops 10 times with 0.1 increases.
  // digits = 2 means 100 steps of 0.01, and so on.
  let prev = n;
  for (let i = 10 ** -digits; i < 1; i += 10 ** -digits) {
    if (n + i === prev) { // eg 10.2 === 10.1
      return true;
    }
    prev = n + i;
  }
  return false;


}

/**Binary search between 0 and Number.MAX_SAFE_INTEGER (2**53 - 1) for the biggest number that is safe to the `digits` level of precision.
 * digits=9 took ~30s, I wouldn't pass anything bigger.*/
function findMaxSafeFloat(digits, log = false) {
  let n = Number.MAX_SAFE_INTEGER;
  let lastSafe = 0;
  let lastUnsafe = undefined;
  while (true) {
    if (log) {
      console.table({
        '': {
          n,
          'Relative to Number.MAX_SAFE_INTEGER': `(MAX + 1) / ${(Number.MAX_SAFE_INTEGER + 1) / (n + 1)} - 1`,
          lastSafe,
          lastUnsafe,
          'lastUnsafe - lastSafe': lastUnsafe - lastSafe
        }
      });
    }
    if (isUnsafe(n, digits)) {
      lastUnsafe = n;
    } else { // safe
      if (lastSafe + 1 === n) { // Closed in as far as possible
        console.log(`\n\nMax safe number to a precision of ${digits} digits after the decimal point: ${n}\t((MAX + 1) / ${(Number.MAX_SAFE_INTEGER + 1) / (n + 1)} - 1)\n\n`);
        return n;
      } else {
        lastSafe = n;
      }
    }
    n = Math.round((lastSafe + lastUnsafe) / 2);
  }
}

console.log(findMaxSafeFloat(1));

通过对安全数字进行排序,我发现了一件有趣的事情,那就是指数没有以一致的方式上升.请看下表.偶尔,指数增加(或减少)4,而不是3.不知道为什么.

An interesting thing I've found by lining up the safe numbers, is that the exponents don't step up in a consistent manner. Look at the table below; once in a while, the exponent increases (or decreases) by 4, and not 3. Not sure why.

| Precision | First UNsafe                | 2^53/x                   |
|-----------|-----------------------------|--------------------------|
| 1         | 5,629,499,534,21,312 = 2^49 | x = 16 = 2^4             |
| 2         | 703,687,441,77,664 = 2^46   | x = 128 = 2^7            |
| 3         | 87,960,930,22,208 = 2^43    | x = 1,024 = 2^10         |
| 4         | 5,497,558,13,888 = 2^39     | x = 16,384 = 2^14        |
| 5         | 68,719,476,736 = 2^36       | x = 131,072 = 2^17       |
| 6         | 8,589,934,592 = 2^33        | x = 1,048,576 = 2^20     |
| 7         | 536,870,912 = 2^29          | x = 16,777,216 = 2^24    |
| 8         | 67,108,864 = 2^26           | x = 134,217,728 = 2^27   |
| 9         | 8,388,608 = 2^23            | x = 1,073,741,824 = 2^30 |

这篇关于Node.js最大安全浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆