Implementing Rem for StandardForm

I have a crate and I want to improve it by adding num-trait intergeration to it however trait Num requires trait std::ops::Rem to be implemented : standardform file

The way I thought to do this was using the method I was taught in school
For example : 26 % 7 returns 5 as the remainder

26 - (26 / 7 * 7) = 5

So I could do

impl std::ops::Rem for StandardForm {
    type Output = Self;
    fn rem(&self,other : Self) -> Self::Output {
         self.clone() - (*self / other * other) 
    }
}

This approach works, however I want to avoid as + - * / for StandardForm require more complex operations compared to simple intergers.

Any help would be appreciated in solving this!

Typically remainder comes "for free" when you implement division, for example processors don't usually have a separate rem instruction because div already calculates both. So you might be able to exploit that fact and have a divrem function used by both the Div and Rem impls. But the division you can't avoid in the general case, it's just inherent in what a remainder is.

1 Like

For the divrem function I only found a crate and in num-interges there in method. However both require a implementation.which this question is about, whether a more Efficient implementation is possible . Since standard form numbers can't be shown as integers like f64.
In case, I missed something, I don't think my struct can take advantage of that

Side note: the link on docs.rs and crates.io to your github repository is broken, giving a 404.

Thanks for that side-note , I will look into that. I believe I know the reason

f64 isn't an integer and this doesn't seem to have anything to do with "showing" anything. What are you trying to say?

With that statement I meant to say that , I can't take advantage of the built-in methods that provide both remainder and the dividend for 'free'. To use that I have to implement it , hopefully something more efficient compared to the school method of possible

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.