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 c79466c
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions lib/packets/packet.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,8 @@ 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;
res = res.greaterThan(Number.MAX_SAFE_INTEGER) ? res.toNumber() : resString;
return bigNumberStrings ? resString : res;
}
// eslint-disable-next-line no-console
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;
}
return result.toNumber();
}
}
if (this.buffer[this.offset] === plus) {
Expand Down

0 comments on commit c79466c

Please sign in to comment.