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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

yaooqinn
Copy link
Member

@yaooqinn yaooqinn commented Jan 14, 2025

Rationale for this change

What changes are included in this PR?

This PR uses string constructor for bigint to calculate the power of 10 with scale as the exponent value of the expression.

To Fix precision loss like

> BigInt(10) ** BigInt(25);
10000000000000000000000000n
> BigInt(Math.pow(10, 25))
10000000000000000905969664n

Also, we remove the unnecessary safe integer check for the fraction part.

Are these changes tested?

add some unit tests

Are there any user-facing changes?

no

Copy link

Thanks for opening a pull request!

If this is not a minor PR. Could you open an issue for this pull request on GitHub? https://github.com/apache/arrow/issues/new/choose

Opening GitHub issues ahead of time contributes to the Openness of the Apache Arrow project.

Then could you also rename the pull request title in the following format?

GH-${GITHUB_ISSUE_ID}: [${COMPONENT}] ${SUMMARY}

or

MINOR: [${COMPONENT}] ${SUMMARY}

See also:

@kou kou changed the title [GH-45250][JS] Fix denominator precision loss and remove unnecessary safe integer check for fractional part GH-45250: [JS] Fix denominator precision loss and remove unnecessary safe integer check for fractional part Jan 14, 2025
Copy link

⚠️ GitHub issue #45250 has been automatically assigned in GitHub to PR creator.

@github-actions github-actions bot added awaiting committer review Awaiting committer review and removed awaiting review Awaiting review labels Jan 17, 2025
return bigIntToNumber(quotient) + (bigIntToNumber(remainder) / bigIntToNumber(denominator));
const remainder = negative? -(number % denominator) : number % denominator;
const integerPart = bigIntToNumber(quotient);
const fractionPart = padStart((remainder).toString(), scale);
Copy link
Contributor

@trxcllnt trxcllnt Jan 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor suggestion, but we should be able to use the String built-in and remove the padStart below this:

Suggested change
const fractionPart = padStart((remainder).toString(), scale);
const fractionPart = `${remainder}`.padStart(scale, '0');

Comment on lines +106 to +112
function padStart(str: string, targetLength: number) {
while (str.length < targetLength) {
str = '0' + str;
}
return str;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

if (typeof scale === 'number') {
const denominator = BigInt(Math.pow(10, scale));
if (typeof scale === 'number' && scale > 0) {
const denominator = BigInt('1' + '0'.repeat(scale));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can use padEnd() here:

Suggested change
const denominator = BigInt('1' + '0'.repeat(scale));
const denominator = BigInt('1'.padEnd(scale + 1, '0'));

const remainder = negative? -(number % denominator) : number % denominator;
const integerPart = bigIntToNumber(quotient);
const fractionPart = padStart((remainder).toString(), scale);
const sign: string = negative && integerPart === 0 ? '-' : '';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const sign: string = negative && integerPart === 0 ? '-' : '';
const sign = negative && integerPart === 0 ? '-' : '';

const integerPart = bigIntToNumber(quotient);
const fractionPart = padStart((remainder).toString(), scale);
const sign: string = negative && integerPart === 0 ? '-' : '';
return Number(`${sign}${integerPart}.${fractionPart}`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC coercing via +str is a bit faster than using the Number constructor. Minifiers will typically do this as well:

Suggested change
return Number(`${sign}${integerPart}.${fractionPart}`);
return +`${sign}${integerPart}.${fractionPart}`;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants