-
Notifications
You must be signed in to change notification settings - Fork 135
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
Add Inv and Pow traits. #37
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR - it's a good start. Besides the comments below, can you also add some testing? A simple example in the documentation should suffice. Maybe also note that some types may panic on "invalid" input, like a divide-by-zero, non-square matrix, etc.
src/ops/mod.rs
Outdated
@@ -1,3 +1,4 @@ | |||
pub mod saturating; | |||
pub mod checked; | |||
pub mod wrapping; | |||
pub mod new; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be pub mod inv;
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes
src/ops/inv.rs
Outdated
|
||
#[inline] | ||
fn inv(self) -> $out { | ||
($fn)(self) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't this function call will actually work for the reference versions, and it's weird having a lifetime parameter on the value versions. There are only 4 implementations here, so maybe it's not worth trying to make a unified macro.
To avoid cfg(feature = "std")
, we could just implement these as 1.0 / self
. That's all the standard library is doing anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll do that later today
I figured I'd implement #38 in this as well considering how I was going to offer both, and I don't want to deal with merge conflicts. Let me know if everything looks alright! |
src/ops/pow.rs
Outdated
pow_impl!(isize, u32, pow); | ||
|
||
#[cfg(feature = "std")] | ||
mod float_impls { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You'll have to use super::Pow;
to get it into this module.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
…that is correct. I'll just factor out the cfg
.
Let's put |
src/ops/inv.rs
Outdated
/// let x = 7.0; | ||
/// let y = -0.0; | ||
/// assert_eq!(x.inv() * x, One::one()); | ||
/// assert_eq!(y.inv() * y, One::one()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is -inf * -0.0
, which is actually NAN
. Maybe just assert the -inf
instead?
src/pow.rs
Outdated
/// | ||
/// ``` | ||
/// use num_traits::Pow; | ||
/// assert_eq!(10.pow(2), 100); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CI failed here because it inferred this like 10i32.pow(2i32)
, which is not implemented.
Should be ready to merge now. |
Sorry if I misspoke, but I did not mean that we should have so many variations of |
@cuviper I accidentally added the My main reasoning was to do something similar to how |
Ah, I got confused by the macros, and thought you had some signed->unsigned casts in there too. Can we do it without the closures though? I think if you leverage Should floats do I count 372 |
I think that doing without the closures is very fair and I'll refactor this in a bit to achieve that. Also I just noticed rust-lang/rust#48321 and I think that simply deferring to I'll see what I can do about benchmarking compile times and how that can be improved. |
To put real numbers on it, |
I don't think a double in compile time is acceptable; I'll see what I can do. |
@cuviper with the latest change, my compile time went from 2.29s to 1.84s, which is only a 20% increase. Could you provide benchmarks on your end? Longer-term it may be best to make the standalone |
For me it's now 0.86s vs master's 0.72s -- that seems fine. There are still a lot of implementations -- 292 -- so I'd guess the closures were the main culprit for the slowdown. Let's merge! bors r+ |
Build succeeded |
Awesome! Could you push a new version of |
Yeah, but I still want to revisit |
That's fair; no rush. :) |
This is not a breaking change, and closes #34 and #38.
This doesn't add any impls for the other
num
crates, just floats withstd
enabled. The trait has to be added before those other crates can be updated.