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

Count number of "wraps" when adding to ModNum* #1

Closed
linclelinkpart5 opened this issue Dec 9, 2021 · 3 comments
Closed

Count number of "wraps" when adding to ModNum* #1

linclelinkpart5 opened this issue Dec 9, 2021 · 3 comments

Comments

@linclelinkpart5
Copy link

linclelinkpart5 commented Dec 9, 2021

I've found myself in need of a library like this, and it fulfills almost all of my uses! However, I also need to count how many times the modulated value is wrapped around the modulus when performing an addition. For example, if I have 8 mod 17, and add 42 to it, I'd get the usual 16 mod 17, along with a 2 to indicate that the modulated value wrapped around the modulus twice.

I'm happy to add this functionality myself and make a PR, but wanted to get feedback beforehand. Namely, I'd love some advice on what to name this method.

@gjf2a
Copy link
Owner

gjf2a commented Jan 4, 2022 via email

@gjf2a
Copy link
Owner

gjf2a commented Oct 27, 2022

I have thought of a way to implement this. It needs to take into account the original dividend. All operations ultimately funnel down to a call to .with(), which will actually perform the modulo. So I think the solution is to introduce a new data type that tracks the total "wraparounds" it has experienced. I just finished a major refactoring of the crate that will facilitate adding new data types.

So, to do this:

  • Create a WrapTrackNum that keeps track of wrapping and implements the MNum trait.
    • It will have a state variable that tracks the total wrap count that occurred when it was created.
  • Then invoke the derive_modulo_arithmetic! macro to generate all the arithmetic operator implementations.

@gjf2a
Copy link
Owner

gjf2a commented Oct 28, 2022

Thanks again for the suggestion, @linclelinkpart5! I have now implemented it as follows:

  • WrapCountNum and WrapCountNumC are drop-in replacements for ModNum and ModNumC that each have a wraps() method.
  • That method returns accumulated wraparounds as defined above.
  • See the documentation for a full illustration of how they work.

Due to the delay between this implementation and when you posted the issue, it may or may not be useful to you at this point, but I am guessing that your suggestion indicates something that would be useful to many people, so I am pleased that I was finally able to incorporate it.

@gjf2a gjf2a closed this as completed Oct 28, 2022
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

No branches or pull requests

2 participants