I've been using the pow()
method on integers. I tried raising a u32
to a negative power, which resulted in an compile error.
fn main() {
println!("{}", 2u32.pow(-1));
}
error[E0600]: cannot apply unary operator `-` to type `u32`
--> src/main.rs:2:29
|
2 | println!("{}", 2u32.pow(-1));
| ^^
This makes perfect sense, because the pow()
function does not take a signed integer as a parameter.
Is there any way to raise an integer to a signed integer power, ideally without using an experimental nightly API? Is there any reason to only allow for unsigned integers as parameters to the pow()
function?
I expect that the result would be 0.5, and the type would be a float. I now see that a function which allows for this probably cannot be called directly on the unsigned integer, because that would result in its type changing.
Perhaps there is a function which then takes both the base and power as an argument and returns a float, if such a function cannot be called directly on the integer?