rust-num / num-traits

Numeric traits for generic mathematics in Rust
Apache License 2.0
694 stars 131 forks source link

Modular arithmetic #209

Open Armavica opened 3 years ago

Armavica commented 3 years ago

Hello, Modular arithmetic being a common mathematical and computational concept, would it make sense to add support for it to this library? I was thinking that implementing traits such as

trait CheckedAddMod {
    fn checked_add_mod(&self, v: &Self, m: &Self) -> Option<Self>;
}

representing the checked result of the operation (self + v) % m and similar, would make easier and less verbose the writing of these programs.

cuviper commented 3 years ago

I think there are a lot of API possibilities to explore here, which makes me hesitate to add it to the conservatively-stable API of num-traits. There are a few crates that look relevant in searching for "modular" on crates.io, or maybe you'd like to start your own?

cmpute commented 2 years ago

In case someone is interested, I have created two traits in my num-modular crate for modular arithmetics, namely ModularCoreOps and ModularOps. They are already implemented for several primitive types.