我正在尝试将字符串转换为大整数以执行一些算术计算。但是,当我尝试这样做时:
Number("9007199254740993")
...我得到了这个意外的结果:
9007199254740992
我怀疑这可能是由于Number
能够使用的整数大小受到限制。
基本上,我想检查两个字符串是否是连续数字。由于Number
未返回正确的值,因此我得到了"9007199254740993"
和"9007199254740992"
的不正确差值。具体来说,我期望为1,但得到0。
我考虑过的一种可能性是将每个数字除以一个系数,使每个数字变小。还有其他解决方案吗?
答案 0 :(得分:3)
JavaScript的Number
类型是numeric data type in the double-precision 64-bit floating point format (IEEE 754)。
如果要处理大整数,请使用BigInt
或相应的库。
答案 1 :(得分:1)
您可以像在JAVA中一样使用BIG整数库。 check here
npm install big-integer
var bigInt = require("big-integer");
var largeNumber1 = bigInt("9007199254740993");
var largeNumber2 = bigInt("9007199254740994"); // any other number
var ans = largeNumber1.minus(largeNumber2);
if(ans == 1 || ans == -1){
console.log('consecutive ')
}else{
console.log('not consecutive ')
}
答案 2 :(得分:1)
注意:如果您要处理大数,我建议您使用BigInt
(@ Andreas在评论中建议)。
已更新
使用此代码比较大正整数(参数应为字符串格式)
function compareBigNumber(num1, num2) {
if (num1 > Number.MAX_SAFE_INTEGER && num2 > Number.MAX_SAFE_INTEGER) {
var newNum1 = num1.split('').reverse();
var newNum2 = num2.split('').reverse();
do {
newNum1.pop();
newNum2.pop();
} while (newNum1[newNum1.length-1] === '0' || newNum2[newNum2.length-1] === '0')
return compareBigNumber(newNum1.reverse().join(''), newNum2.reverse().join(''));
} else if(num1 > Number.MAX_SAFE_INTEGER){
return 'num1 is greater'
} else if (num2 > Number.MAX_SAFE_INTEGER) {
return 'num2 is greater'
}
else {
var num1Int = parseInt(num1);
var num2Int = parseInt(num2);
if (num1Int > num2Int) {
return 'Num1 is greater';
} else if (num2Int > num1Int){
return 'Num2 is greater'
} else {
return 'Num1 is equal to Num2';
}
}
}
console.log(compareBigNumber("9007199254740992", "9007199254740993"))
console.log(compareBigNumber("100000000000000000000", "0"))
答案 3 :(得分:1)
如果您不想依靠BigInt
,并且只考虑正整数,也可以编写自己的后继测试。以下代码段中包含完整代码。
正整数的字符串表示形式很容易转换为十进制数组,其中索引表示以10为底的指数。例如"42" ~> [2, 4]
(自42 = 2*10^0 + 4*10^1
起)。您也可以轻松地将其转换回去。
现在,对于后继测试,您只需要定义增量操作(只需将1与进位相加即可)。这样一来,您就可以比较一个数字的增量是否等于另一个数字的增量(反之亦然)。
// Convert a string representation of positive decimal integer to an array of decimals.
const toArray = numberString => Array.from(numberString, c => parseInt(c))
.reverse();
// Convert the array representation of a positive decimal integer string back to the corresponding string representation (this is the inverse of `toArray`).
const fromArray = numberArray => numberArray.map(String)
.reverse()
.join('');
console.log(fromArray(toArray("9007199254740993")) === "9007199254740993"); // true
// Perform the increment operation on the array representation of the positive decimal integer.
const increment = numberArray => {
let carry = 1;
const incrementedNumberArray = [];
numberArray.forEach(i => {
let j;
if (carry === 0) {
j = i;
} else if (carry === 1) {
if (i === 9) {
j = 0;
} else {
j = i + 1;
carry = 0;
}
}
incrementedNumberArray.push(j);
});
if (carry === 1) {
incrementedNumberArray.push(1);
}
return incrementedNumberArray;
};
console.log(fromArray(increment(toArray("9007199254740993"))) === "9007199254740994"); // true
console.log(fromArray(increment(toArray("9999999999999999"))) === "10000000000000000"); // true
// Test if two strings represent positive integers where one is the other's successor.
const isSuccessor = (a, b) => {
const a_ = increment(toArray(a));
const b_ = increment(toArray(b));
return fromArray(a_) === b || fromArray(b_) === a;
};
console.log(isSuccessor("9007199254740993", "9007199254740994")); // true
console.log(isSuccessor("9007199254740994", "9007199254740993")); // true
console.log(isSuccessor("9999999999999999", "10000000000000000")); // true
console.log(isSuccessor("10000000000000000", "9999999999999999")); // true
console.log(isSuccessor("10000000000000000", "10000000000000002")); // false