Skip to content

Commit

Permalink
fix(uint64): avoid rounding errors in Uint64.toString with large high…
Browse files Browse the repository at this point in the history
… values (#577)
  • Loading branch information
chrisj committed Apr 16, 2024
1 parent daf82f3 commit d5cc035
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/util/uint64.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ describe("uint64", () => {
expect(new Uint64(4294967295, 4294967295).toString(36)).toEqual(
"3w5e11264sgsf",
);
expect(new Uint64(0, 79741775).toString(29)).toEqual("s2276dsssss2");
});

it("conversion from string", () => {
Expand Down Expand Up @@ -110,7 +111,7 @@ describe("uint64", () => {
expect(u.toString(13)).toEqual("153c9125c642b111b8");
check(u, 13);
}

check(new Uint64(0, 79741775), 29);
for (let base = 2; base <= 36; ++base) {
for (let i = 0; i < count; ++i) {
check(Uint64.random(), base);
Expand Down
2 changes: 1 addition & 1 deletion src/util/uint64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export class Uint64 {
vHigh *= trueBase;
const { lowBase, lowDigits } = stringConversionData[base];
const vHighExtra = vHigh % lowBase;
vHigh = Math.floor(vHigh / lowBase);
vHigh = Number(BigInt(vHigh) / BigInt(lowBase));
vLow += vHighExtra;
vHigh += Math.floor(vLow / lowBase);
vLow = vLow % lowBase;
Expand Down

0 comments on commit d5cc035

Please sign in to comment.