什么是JavaScript的最大整数值,数字可以去而不失精度? [英] What is JavaScript's highest integer value that a Number can go to without losing precision?

查看:1088
本文介绍了什么是JavaScript的最大整数值,数字可以去而不失精度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是由语言定义的吗?是否有定义的最大值?在不同的浏览器中它是不同的吗?

解决方案

+/- 9007199254740991 b

ECMA第8.5节 - 数字


请注意,其大小不大于2 53 的所有正整数和负整数都可以在Number类型中表示,整数0具有两个表示,+ 0和-0)。


它们是64位浮点值,最大的精确整数值是2 53 9007199254740991 。在ES6中,这被定义为 Number.MAX_SAFE_INTEGER



请注意,按位运算符和移位运算符对32位int运算,因此在这种情况下,最大安全整数为2 31 -1或2147483647.





测试一下!

  var x = 9007199254740992; 
var y = -x;
x == x + 1; // true!
y == y - 1; //也是真的!
//算术运算符工作,但按位/移位只对int32操作:
x / 2; // 4503599627370496
x>> 1; // 0
x | 1; // 1


Is this defined by the language? Is there a defined maximum? Is it different in different browsers?

解决方案

+/- 9007199254740991

ECMA Section 8.5 - Numbers

Note that all the positive and negative integers whose magnitude is no greater than 253 are representable in the Number type (indeed, the integer 0 has two representations, +0 and −0).

They are 64-bit floating point values, the largest exact integral value is 253-1, or 9007199254740991. In ES6, this is defined as Number.MAX_SAFE_INTEGER.

Note that the bitwise operators and shift operators operate on 32-bit ints, so in that case, the max safe integer is 231-1, or 2147483647.


Test it out!

var x = 9007199254740992;
var y = -x;
x == x + 1; // true !
y == y - 1; // also true !
// Arithmetic operators work, but bitwise/shifts only operate on int32:
x / 2;      // 4503599627370496
x >> 1;     // 0
x | 1;      // 1

这篇关于什么是JavaScript的最大整数值,数字可以去而不失精度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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