-
Notifications
You must be signed in to change notification settings - Fork 13.9k
Open
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Description
While trying to raise a float to an integer power, if the user tries to write float.pow(int), rustc suggests using powf, which then results in a type error. rustc then suggests casting the int to a float with as. The correct fix would instead be to use powi.
Given the following code: [playground]
fn main() {
let x: f32 = 10.0;
let p: i32 = 3;
let x_cubed = x.pow(p);
println!("{} cubed is {}", x, x_cubed);
}The current output is:
error[[E0599]](https://doc.rust-lang.org/stable/error-index.html#E0599): no method named `pow` found for type `f32` in the current scope
--> src/main.rs:4:21
|
4 | let x_cubed = x.pow(p);
| ^^^ help: there is an associated function with a similar name: `powf`
For more information about this error, try `rustc --explain E0599`.
When using the suggested fix, the output is:
error[[E0308]](https://doc.rust-lang.org/stable/error-index.html#E0308): mismatched types
--> src/main.rs:4:26
|
4 | let x_cubed = x.powf(p);
| ---- ^ expected `f32`, found `i32`
| |
| arguments to this function are incorrect
|
note: associated function defined here
help: you can convert an `i32` to an `f32`, producing the floating point representation of the integer, rounded if necessary
|
4 | let x_cubed = x.powf(p as f32);
| ++++++
For more information about this error, try `rustc --explain E0308`.
Ideally the output should look like:
error[[E0599]](https://doc.rust-lang.org/stable/error-index.html#E0599): no method named `pow` found for type `f32` in the current scope
--> src/main.rs:4:21
|
4 | let x_cubed = x.pow(p);
| ^^^ help: there is an associated function with a similar name: `powi`
For more information about this error, try `rustc --explain E0599`.
The ideal output provides a working fix, and is likely to be more correct to the user's intended function.
Metadata
Metadata
Assignees
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.