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

Idea: Pow trait #38

Closed
clarfonthey opened this issue Feb 18, 2018 · 4 comments
Closed

Idea: Pow trait #38

clarfonthey opened this issue Feb 18, 2018 · 4 comments

Comments

@clarfonthey
Copy link
Contributor

clarfonthey commented Feb 18, 2018

I've been working on a crate which works with runtime-level units, and I think that having a generic Pow trait would be useful.

Essentially, it would be very similar to the other unary op traits, namely:

pub trait Pow<RHS> {
    type Output;
    fn pow(self, rhs: RHS) -> Self::Output;
}

And in most cases, this would simply do exponentiation by squaring as included in the existing pow functions. However, it may be easier to implement for some types, namely a unit could simply multiply a field that indicates what power of the given unit you have. Another example is that a type which constrains which values it has, e.g. a matrix which specifically performs scaling operations, could be modified to do less work.

This would be weird to implement and may cause confusion with the existing pow function, which is why I haven't offered a PR for it. But it might be nice to have in this crate.

@cuviper
Copy link
Member

cuviper commented Feb 19, 2018

It makes sense to me, especially since types can make internal decisions about stuff like #18.

@cuviper
Copy link
Member

cuviper commented Feb 19, 2018

(We can de-emphasize the generic fn pow() in favor of this.)

@cuviper
Copy link
Member

cuviper commented Feb 19, 2018

It might also be interesting that floats can implement both Pow<Self> and Pow<i32> using powf and powi, though only with std enabled. All of the integers can forward to their inherent pow methods too, instead of using our own.

@termoshtt
Copy link
Contributor

Looks good :) I think it is very useful since it can be implemented for num_complex::Complex<T>.

bors bot added a commit that referenced this issue Feb 27, 2018
37: Add Inv and Pow traits. r=cuviper a=clarcharr

This is not a breaking change, and closes #34 and #38.

This doesn't add any impls for the other `num` crates, just floats with `std` enabled. The trait has to be added before those other crates can be updated.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants