Skip to content

Missing pow implementation for integers #11499

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

Closed
flaper87 opened this issue Jan 12, 2014 · 3 comments · Fixed by #11503
Closed

Missing pow implementation for integers #11499

flaper87 opened this issue Jan 12, 2014 · 3 comments · Fixed by #11503

Comments

@flaper87
Copy link
Contributor

$ cat test.rs                                                                                                                                                                                   101 ↵  
fn main() {
    2.pow(3);
}
$ rustc test.rs                                                                                                                                                                                        
test.rs:2:5: 2:14 error: type `<VI0>` does not implement any method in scope named `pow`
test.rs:2     2.pow(3);
              ^~~~~~~~~
error: aborting due to previous error
task 'rustc' failed at 'explicit failure', /home/flaper87/workspace/personal/rust/src/libsyntax/diagnostic.rs:75
task '<main>' failed at 'explicit failure', /home/flaper87/workspace/personal/rust/src/librustc/lib.rs:453
@adrientetar
Copy link
Contributor

Upon investigation, this is the relevant method:
http://static.rust-lang.org/doc/master/std/num/trait.Real.html#tymethod.pow

Real numbers must implement Signed + Orderable + Round + Div, yet ints aren't considered real numbers because they don't implement Round (only float types do as of now).
http://static.rust-lang.org/doc/master/std/num/trait.Round.html

@flaper87
Copy link
Contributor Author

Yeah, @bjz is doing some work on cleaning the num package. As for this bug, we can either wait for that work to be complete or implement pow as part of the Integer trait.

There are some methods in the Real trait that we can't implement for Int or at least are not worth it. For example: http://static.rust-lang.org/doc/master/std/num/trait.Real.html#tymethod.pi

I'm already working on an implementation of pow for the Integer trait. This covers Int, Uint, BigInt and BigUint

@adrientetar
Copy link
Contributor

So, Real trait is designed as to exclude subsets it seems.

flaper87 added a commit to flaper87/rust that referenced this issue Jan 17, 2014
The patch adds a `pow` function for types implementing `One`, `Mul` and
`Clone` trait.

The patch also renames f32 and f64 pow into powf in order to still have
a way to easily have float powers. It uses llvms intrinsics.

The pow implementation for all num types uses the exponentiation by
square.

Fixes bug rust-lang#11499
@bors bors closed this as completed in c58d2ba Jan 18, 2014
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

Successfully merging a pull request may close this issue.

2 participants