Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-45250: [JS] Fix denominator precision loss and remove unnecessary safe integer check for fractional part #45251

Merged
merged 10 commits into from
Jan 22, 2025
18 changes: 14 additions & 4 deletions js/src/util/bn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,25 @@ export function bigNumToNumber<T extends BN<BigNumArray>>(bn: T, scale?: number)
number |= word * (BigInt(1) << BigInt(64 * i++));
}
}
if (typeof scale === 'number') {
const denominator = BigInt(Math.pow(10, scale));
if (typeof scale === 'number' && scale > 0) {
const denominator = BigInt('1' + '0'.repeat(scale));
yaooqinn marked this conversation as resolved.
Show resolved Hide resolved
const quotient = number / denominator;
const remainder = number % denominator;
return bigIntToNumber(quotient) + (bigIntToNumber(remainder) / bigIntToNumber(denominator));
const remainder = negative? -(number % denominator) : number % denominator;
const integerPart = bigIntToNumber(quotient);
const fractionPart = padStart((remainder).toString(), scale);
yaooqinn marked this conversation as resolved.
Show resolved Hide resolved
const sign: string = negative && integerPart === 0 ? '-' : '';
yaooqinn marked this conversation as resolved.
Show resolved Hide resolved
return Number(`${sign}${integerPart}.${fractionPart}`);
yaooqinn marked this conversation as resolved.
Show resolved Hide resolved
}
return bigIntToNumber(number);
}

function padStart(str: string, targetLength: number) {
while (str.length < targetLength) {
str = '0' + str;
}
return str;
}

yaooqinn marked this conversation as resolved.
Show resolved Hide resolved
/** @ignore */
export function bigNumToString<T extends BN<BigNumArray>>(a: T): string {
// use BigInt native implementation
Expand Down
13 changes: 12 additions & 1 deletion js/test/unit/bn-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// under the License.

import * as Arrow from 'apache-arrow';
const { BN } = Arrow.util;
const { BN, bigNumToNumber } = Arrow.util;

describe(`BN`, () => {
test(`to detect signed numbers, unsigned numbers and decimals`, () => {
Expand Down Expand Up @@ -98,4 +98,15 @@ describe(`BN`, () => {
// const n6 = new BN(new Uint32Array([0x00000000, 0x00000000, 0x00000000, 0x80000000]), false);
// expect(n6.valueOf(1)).toBe(1.7014118346046923e+37);
});

test(`bigNumToNumber`, () => {
const n1 = new BN(new Uint32Array([3, 2, 1, 0]));
expect(() => bigNumToNumber(n1)).toThrow('18446744082299486211');
/* eslint-disable @typescript-eslint/no-loss-of-precision */
expect(bigNumToNumber(n1, 10)).toBeCloseTo(1844674408.2299486);
expect(bigNumToNumber(n1, 15)).toBeCloseTo(18446.744082299486);
expect(bigNumToNumber(n1, 20)).toBeCloseTo(0.18446744082299486);
expect(bigNumToNumber(n1, 25)).toBeCloseTo(0.0000018446744082299486);
/* eslint-enable @typescript-eslint/no-loss-of-precision */
});
});
Loading