Node.js最大安全浮点数

时间:2017-08-29 01:40:13

标签: javascript node.js floating-point

在Node.js中,是否存在最大安全浮点数,如Number.MAX_SAFE_INTEGER

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

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

我的猜测是,随着目标精度的增加,最大值会减小。

4 个答案:

答案 0 :(得分:2)

更新:我对这个问题的理解是:是否存在最大浮点数,介于0和之间,可以安全地传递所有浮点数操作。

如果这是问题,简短的回答是:

实际上,所有编程语言中都没有MAX_SAFE_FLOAT(如果有的话会非常高兴)。编程语言中的数字由 module.export = function (sequelize,DataTypes){ const User = sequelize.define('User',{ id:......, ..........},{ tableName:'user', classMethods:{ associate:function(models){ //assocoation.....}, allUserName:async function(){ let list = await this.findAll({...}) return list; } } return User; } 0位存储。只要存储有限制(32位,64位等),可以表示的数字是有限的。但是,浮点数是无限的。

考虑10之间的浮点数,需要表示多少个数字?无穷。让计算机准确存储无限可能性是不可能的。这就是为什么永远不会有MAX_SAFE_FLOAT。

P.S。在JavaScript中,所有数字都是64位双精度浮点数。没有浮动数字。 JavaScript中的interger-number。

答案 1 :(得分:1)

为了精确到1个小数位数,您可以使用的最大数字为562949953421311

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

(Number.MAX_SAFE_INTEGER + 1) / 16 - 1

第二个数字等于:

(Number.MAX_SAFE_INTEGER + 1) / 128 - 1

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

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

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

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

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

顺便说一句,01e-323(包括)为止都是可靠的:

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

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

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

以下是代码(在代码段末尾将其他任何数字传递到findMaxSafeFloat()

/**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。不知道为什么。

| 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 |

答案 2 :(得分:0)

指数以一致的方式减少,因为它取决于小数部分的大小(二进制)。

  • 对于精度 1,最大小数部分大小为 9 -> 1001 -> 大小 = 4
  • 对于精度 2,最大小数部分大小为 99 -> 1100011 -> 尺寸 = 7
  • 对于精度 3,最大小数部分大小为 999 -> 1111100111 -> 尺寸 = 10
  • ...

如您所见,我们的数字与@giladbarnea 发现的数字相同。

基于这一观察,我们可以轻松地编写一个最简单的解决方案来找到可以与任何整数相关的所有精度。

const precisions = [...new Array(16).keys()]
  .reverse()
  .map(value => {
    const int = 53 - (value && BigInt('9'.repeat(value)).toString(2).length)

    return int > 0
      ? 2 ** int - 1
      : 0
  })

function getSafePrecision (value) {
  const abs = Math.abs(value)

  return 15 - precisions.findIndex(precision => precision >= abs)
}

示例:

getSafePrecision(2 ** 43 - 1) // 3
getSafePrecision(2 ** 43) // 2

答案 3 :(得分:-1)

您正在寻找 Number.MAX_VALUE Number.MIN_VALUE

&#xA;&#xA;

Number。 MAX_VALUE 1.7976931348623157e + 308 Number.MIN_VALUE 5e-324

&#xA;