这是由语言定义的吗?有确定的最大值吗?在不同的浏览器中是否有所不同?
当前回答
火狐3似乎不存在庞大数字的问题。
1e+200 * 1e+100将计算到1e+300。
Safari似乎也没有问题。(顺便说一句,如果有人想测试的话,这是在Mac上进行的。)
除非我脑子进水了,否则这比64位整数大得多。
其他回答
在JavaScript中,有一个数字叫做Infinity。
例子:
(Infinity>100)
=> true
// Also worth noting
Infinity - 1 == Infinity
=> true
Math.pow(2,1024) === Infinity
=> true
对于这个主题的一些问题,这可能已经足够了。
火狐3似乎不存在庞大数字的问题。
1e+200 * 1e+100将计算到1e+300。
Safari似乎也没有问题。(顺便说一句,如果有人想测试的话,这是在Mac上进行的。)
除非我脑子进水了,否则这比64位整数大得多。
Jimmy的答案正确地表示了连续的JavaScript整数谱,从-9007199254740992到9007199254740992,包括(对不起,9007199254740993,您可能认为您是9007199254740993,但您错了! 下面或jsfiddle中的演示)。
console.log(9007199254740993);
然而,没有答案可以通过编程来发现/证明这一点(除了CoolAJ86在他的回答中提到的将在28.56年完成的答案),所以这里有一个稍微更有效的方法来做到这一点(准确地说,它更有效约28.559999999968312年:),以及一个测试小提琴:
/** * Checks if adding/subtracting one to/from a number yields the correct result. * * @param number The number to test * @return true if you can add/subtract 1, false otherwise. */ var canAddSubtractOneFromNumber = function(number) { var numMinusOne = number - 1; var numPlusOne = number + 1; return ((number - numMinusOne) === 1) && ((number - numPlusOne) === -1); } //Find the highest number var highestNumber = 3; //Start with an integer 1 or higher //Get a number higher than the valid integer range while (canAddSubtractOneFromNumber(highestNumber)) { highestNumber *= 2; } //Find the lowest number you can't add/subtract 1 from var numToSubtract = highestNumber / 4; while (numToSubtract >= 1) { while (!canAddSubtractOneFromNumber(highestNumber - numToSubtract)) { highestNumber = highestNumber - numToSubtract; } numToSubtract /= 2; } //And there was much rejoicing. Yay. console.log('HighestNumber = ' + highestNumber);
在谷歌Chrome内置javascript,你可以去大约2^1024之前的数字被称为无穷大。
我这样写:
var max_int = 0x20000000000000;
var min_int = -0x20000000000000;
(max_int + 1) === 0x20000000000000; //true
(max_int - 1) < 0x20000000000000; //true
int32也是一样
var max_int32 = 0x80000000;
var min_int32 = -0x80000000;
推荐文章
- 如何禁用文本选择使用jQuery?
- 如何停止事件冒泡复选框点击
- 向对象数组添加属性
- 如何在Redux应用程序中动态加载代码分割的减速器?
- Angular 2+和debounce
- 检测涡旋方向
- 在react native中隐藏/显示组件
- 在网页上用鼠标模拟震颤(例如帕金森病)?
- console.log()和console.debug()的区别?
- HyperLogLog算法是如何工作的?
- 如何删除和清除所有的本地存储数据
- 如何从给定的html字符串中删除前导和尾随空白?
- 如何为KnockoutJS调试模板绑定错误?
- 将一个二进制的NodeJS Buffer转换为JavaScript的ArrayBuffer
- 检测浏览器标签是否有焦点