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

Numeric fallbacks don't work when inherent methods are involved #24124

Open
XMPPwocky opened this Issue Apr 6, 2015 · 4 comments

Comments

Projects
None yet
7 participants
@XMPPwocky
Copy link
Contributor

XMPPwocky commented Apr 6, 2015

I would expect 2.0.sqrt() to return sqrt(2) as a f64. Instead,

<anon>:9:13: 9:19 error: type `_` does not implement any method in scope named `sqrt`
<anon>:9         2.0.sqrt()

<anon>:9:19: 9:19 help: methods from traits can only be called if the trait is in scope; the following traits are implemented but not in scope, perhaps add a `use` for one of them:
<anon>:9:19: 9:19 help: candidate #1: use `std::num::Float`
<anon>:9:19: 9:19 help: candidate #2: use `core::num::Float`

... and the Float trait is deprecated!

This also occurs with integer literals.

I ran into this in the wild while doing an exponential decay:

let k = c * 2.0.powf(-1.0 * scale * dt);

A workaround is to annotate the literal's type explicitly.

@azar77

This comment has been minimized.

Copy link

azar77 commented Apr 7, 2015

I encountered a similar problem:

fn sqrt2() -> f64 {
    let x = 2.0;
    x.sqrt()
}

fn two_plus_three() -> u64 {
    let x = 2;
    x.checked_add(3).unwrap()
}

In both cases, the compiler fails to infer the type of x even though it has enough information to do so (from the return value of the functions).

@IvanUkhov

This comment has been minimized.

Copy link
Contributor

IvanUkhov commented Jul 21, 2016

Moreover, the language reference says that

If the program context under-constrains the type, it defaults to f64.

So, if the reference was respected, it would default to f64 and call the right method.

@CasualX

This comment has been minimized.

Copy link

CasualX commented Jul 20, 2017

I just ran into this writing some example codes (where it's more likely to have a lot of 'unconstrained' hard coded constants), note the very unhelpful error message:

fn main() {
    let _ = 0.5.sqrt();
}
error: no method named `sqrt` found for type `{float}` in the current scope
 --> src/main.rs:2:21
  |
2 |         let _ = 0.5.sqrt();
  |                     ^^^^
  |
  = help: items from traits can only be used if the trait is in scope; the following trait is implemented but not in scope, perhaps add a `use` for it:
  = help: candidate #1: `use rand::FloatMath;`

error: aborting due to previous error

error: Could not compile `playground`.

To learn more, run the command again with --verbose.

https://play.rust-lang.org/?gist=24e7667d5a1ea0c7b8702e9ac4a075b4&version=stable

@zackw

This comment has been minimized.

Copy link
Contributor

zackw commented Aug 5, 2017

This was also reported as a pain point in the "Ideas for making Rust easier for beginners" internals thread: https://internals.rust-lang.org/t/ideas-for-making-rust-easier-for-beginners/4761/37

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.