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

Added modMultiplicativeInverseNumber to the number arithmetic module #1749

Closed
wants to merge 3 commits into from

Conversation

ohpyupi
Copy link

@ohpyupi ohpyupi commented Feb 23, 2020

Addressed #1744

So far, I added a function modMultiplicativeInverseNumber in the number arithmetic module. And I guess remaining task is to implement this function to the library. But before doing it, I wanted to open this PR to get feedbacks and comments in early steps.

@josdejong
Copy link
Owner

Thanks @ohpyupi , this is a nice start! Next step will indeed be to make an implementation that supports data types like BigNumber, and expose it in the library as a new function.

What would make most sense to return when there is no result? Currently you return null, but maybe NaN is more logic and consistent with the rest of the library. What do you think.

@ohpyupi
Copy link
Author

ohpyupi commented Feb 28, 2020

@josdejong Totally agree. I will start implementing supporting other types and replace null with NaN. Thanks!

@josdejong
Copy link
Owner

Hi @ohpyupi, did you find some time already to work further on this PR?

@ohpyupi
Copy link
Author

ohpyupi commented Apr 5, 2020

@josdejong Hi. I was busy for last few weeks. I will have time to work on it from next weekend.

@josdejong
Copy link
Owner

Thanks for the update! Take it easy :)

if (b === 0) {
throw new Error('divisor must be an integer')
}
const [div, m] = xgcdNumber(a, b)
Copy link
Author

Choose a reason for hiding this comment

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

@josdejong I think I cannot extend the argument type to both float and big number since xgcdNumber throws an error if the params are non-integer. I need to update this function's logic in a way it can handle non-integer as well.

Copy link
Owner

Choose a reason for hiding this comment

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

@ohpyupi The xgcd function indeed only works with integers.

To get a working solution for BigNumber, you can inject xgcd instead of using xgcdNumber, this works for number and BigNumber. Like:

const dependencies = ['typed', 'xgcd']
// ...
export const createModMultiplicativeInverse = /* #__PURE__ */ factory(name, dependencies, ({ typed, xgcd }) => {
  // ...
  const [div, m] = xgcd(a, b)
  // ...
}

Copy link
Author

Choose a reason for hiding this comment

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

@josdejong Oh cool! But what about the float type? Does it work with the float type as well?

Copy link
Owner

Choose a reason for hiding this comment

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

The function xgcd does not support floats, only integer values. Function mod does support floats. If needed for modMultiplicativeInverseNumber I guess we have to extend xgcd?

Copy link
Author

Choose a reason for hiding this comment

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

What about this? In this scope of PR, I ignore the float support for the modular inverse. And if the float support is requested, it can be implemented then. let me know if this approach works on your end.

Copy link
Owner

Choose a reason for hiding this comment

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

Yes that sounds like a pragmatic approach 👍

@gwhitney
Copy link
Collaborator

gwhitney commented Oct 2, 2023

Was this superseded by #2368 and should be closed?

@josdejong
Copy link
Owner

I think so. Let's close this PR. Feel free to reopen if needed Hoseong Lee.

@josdejong josdejong closed this Oct 4, 2023
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.

4 participants