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

fix: Handle bigints properly in more cases #3345

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from

Conversation

gwhitney
Copy link
Collaborator

@gwhitney gwhitney commented Jan 3, 2025

  • Allow a bigint to appear in a max/min of nonhomogeneous arguments
  • If randomInt is called with a bigint or a pair of bigints, return a bigint.
  • Preserve uniformity of results if randomInt is called with a very large range.
  • Extend log, log2, and log10 to bigints.
  • Allow mixed Fraction and BigNumber in smaller and larger.
  • Add tests for all of the above issues.
  • Bonus: fix one JSDoc comment issue. If every PR fixes one, we will soon get through the 150+ of them.

Resolves #3344.

  * Allow a bigint to appear in a max/min of nonhomogeneous arguments
  * If randomInt is called with a bigint or a pair of bigints, return
    a bigint.
  * Preserve uniformity of results if randomInt is called with a very large
    range.
  * Extend log, log2, and log10 to bigints
  * Add tests for all of the above issues.
  * Bonus: fix one JSDoc comment issue. If every PR fixes one, we will soon
    get through the 150+
Copy link
Owner

@josdejong josdejong left a comment

Choose a reason for hiding this comment

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

Thanks @gwhitney, looks good!

I have made a few comments with some questions and minor feedback, can you have a look at those?

bigint: function (x) {
if (x > 0 || config.predictable) {
if (x <= 0) return NaN
const s = x.toString(16)
Copy link
Owner

Choose a reason for hiding this comment

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

Can you explain how this coversion to hex and comparing lengths works?

Is there a way to move this logic in a helper function and use that in log, log10, and log2?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done, moved to src/utils/bigint.js. If you prefer a different file name, let me know.

@@ -48,9 +50,11 @@ export const createRandomInt = /* #__PURE__ */ factory(name, dependencies, ({ ty
* @return {number | Array | Matrix} A random integer value
*/
return typed(name, {
'': () => _randomInt(0, 1),
'': () => _randomInt(0, 2),
Copy link
Owner

Choose a reason for hiding this comment

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

Ouch, this was a bug in randomInt, right? Before it always returns zero.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Correct, before it was a fancy constant function ;-)
Sorry about the delay in getting back on this, I was at a conference last week.

@@ -55,20 +62,28 @@ export const createLarger = /* #__PURE__ */ factory(name, dependencies, ({ typed
* @param {number | BigNumber | bigint | Fraction | boolean | Unit | string | Array | Matrix} y Second value to compare
* @return {boolean | Array | Matrix} Returns true when the x is larger than y, else returns false
*/
const bignumLarger = bigLarger(config)
Copy link
Owner

Choose a reason for hiding this comment

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

I think the factory function bigLarger is not needed and we can just create a function bignumLarger here? Like:

function bignumLarger (x, y) {
  return x.gt(y) && !bigNearlyEqual(x, y, config.relTol, config.absTol)
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Agreed, that is simpler; refactored as suggested.

@@ -55,20 +62,28 @@ export const createSmaller = /* #__PURE__ */ factory(name, dependencies, ({ type
* @param {number | BigNumber | bigint | Fraction | boolean | Unit | string | Array | Matrix} y Second value to compare
* @return {boolean | Array | Matrix} Returns true when the x is smaller than y, else returns false
*/
const bignumSmaller = bigSmaller(config)
Copy link
Owner

Choose a reason for hiding this comment

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

Same as with the larger function: just put the returned function of bigSmaller here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ditto.

@gwhitney
Copy link
Collaborator Author

OK, I believe I have incorporated all feedback and gotten this PR back into a good state. Thanks for your review.

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

Successfully merging this pull request may close these issues.

instances of lack of support for bigints
2 participants