Skip to content

Commit

Permalink
Fix big number accuracy
Browse files Browse the repository at this point in the history
  • Loading branch information
KunZhou-at committed Nov 14, 2023
1 parent 1aec4fd commit 3ccf7e4
Showing 1 changed file with 9 additions and 13 deletions.
22 changes: 9 additions & 13 deletions lib/packets/packet.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,9 @@ class Packet {
return word1 * 0x100000000 + word0;
}
res = new Long(word0, word1, !signed); // Long need unsigned
const resNumber = res.toNumber();
const resString = res.toString();
res = resNumber.toString() === resString ? resNumber : resString;
return bigNumberStrings ? resString : res;
const safeResNumberPreferred = res.greaterThan(Number.MAX_SAFE_INTEGER) || res.lessThan(Number.MIN_SAFE_INTEGER) ? resString : res.toNumber();
return bigNumberStrings ? resString : safeResNumberPreferred;
}
// eslint-disable-next-line no-console
console.trace();
Expand Down Expand Up @@ -425,7 +424,7 @@ class Packet {
return StringParser.decode(
this.buffer,
encoding,
this.offset - len,
this.offset - len,
this.offset
);
}
Expand Down Expand Up @@ -456,15 +455,12 @@ class Packet {
if (supportBigNumbers) {
if (numDigits >= 15) {
str = this.readString(end - this.offset, 'binary');
result = parseInt(str, 10);
if (result.toString() === str) {
return sign * result;
}
return sign === -1 ? `-${str}` : str;
}
if (numDigits > 16) {
str = this.readString(end - this.offset);
return sign === -1 ? `-${str}` : str;
str = sign === -1 ? `-${str}` : str;
result = Long.fromString(str, false, 10);
if (result.greaterThan(Number.MAX_SAFE_INTEGER) || result.lessThan(Number.MIN_SAFE_INTEGER)) {
return result.toString();
}
return result.toNumber();
}
}
if (this.buffer[this.offset] === plus) {
Expand Down

0 comments on commit 3ccf7e4

Please sign in to comment.