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

no method named "fract" found for type "f64" in the current scope when in no_std #133579

Open
hukasu opened this issue Nov 28, 2024 · 12 comments
Open
Labels
C-discussion Category: Discussion or questions that doesn't represent real issues. T-libs Relevant to the library team, which will review and decide on the PR/issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@hukasu
Copy link

hukasu commented Nov 28, 2024

I tried this code:

#![no_std]

fn get_fract(f: f64) -> f64 {
    f.fract()
}

I expected to see this happen: Compiles successfully

Instead, this happened: Fails to compile with E0599 "no method named fract found for type f64 in the current scope"

Meta

rustc --version --verbose:

rustc 1.83.0 (90b35a623 2024-11-26)
binary: rustc
commit-hash: 90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf
commit-date: 2024-11-26
host: x86_64-unknown-linux-gnu
release: 1.83.0
LLVM version: 19.1.1
@hukasu hukasu added the C-bug Category: This is a bug. label Nov 28, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Nov 28, 2024
@hukasu
Copy link
Author

hukasu commented Nov 29, 2024

error[E0599]: no method named `fract` found for reference `&f64` in the current scope
  --> src/ext/float.rs:12:23
   |
12 |         self.borrow().fract().classify() == FpCategory::Zero
   |                       ^^^^^ method not found in `&f64`

error[E0599]: no method named `powf` found for type `f64` in the current scope
   --> src/program/binops/mod.rs:184:49
    |
184 |             Ok(ExpDesc::Float({ *lhs_i as f64 }.powf(*rhs_i as f64)))
    |                                                 ^^^^ method not found in `f64`

error[E0599]: no method named `powf` found for reference `&f64` in the current scope
   --> src/program/binops/mod.rs:186:83
    |
186 |         (ExpDesc::Float(lhs_f), ExpDesc::Float(rhs_f)) => Ok(ExpDesc::Float(lhs_f.powf(*rhs_f))),
    |                                                                                   ^^^^ method not found in `&f64`

error[E0599]: no method named `powf` found for type `f64` in the current scope
   --> src/program/binops/mod.rs:188:47
    |
188 |             Ok(ExpDesc::Float((*lhs_i as f64).powf(*rhs_f)))
    |                                               ^^^^ method not found in `f64`

error[E0599]: no method named `powf` found for reference `&f64` in the current scope
   --> src/program/binops/mod.rs:191:37
    |
191 |             Ok(ExpDesc::Float(lhs_f.powf(*rhs_i as f64)))
    |                                     ^^^^ method not found in `&f64`

error[E0599]: no method named `trunc` found for type `f64` in the current scope
   --> src/program/binops/mod.rs:256:47
    |
256 |             Ok(ExpDesc::Float((lhs_f / rhs_f).trunc()))
    |                                               ^^^^^ method not found in `f64`

error[E0599]: no method named `trunc` found for type `f64` in the current scope
   --> src/program/binops/mod.rs:259:55
    |
259 |             Ok(ExpDesc::Float((*lhs_i as f64 / rhs_f).trunc()))
    |                                                       ^^^^^ method not found in `f64`

error[E0599]: no method named `trunc` found for type `f64` in the current scope
   --> src/program/binops/mod.rs:262:55
    |
262 |             Ok(ExpDesc::Float((lhs_f / *rhs_i as f64).trunc()))
    |                                                       ^^^^^ method not found in `f64`

error[E0599]: no method named `powf` found for type `f64` in the current scope
   --> src/lib.rs:305:54
    |
305 | ...                   Value::Float((*l as f64).powf(*r as f64))
    |                                                ^^^^ method not found in `f64`

error[E0599]: no method named `powf` found for reference `&f64` in the current scope
   --> src/lib.rs:307:78
    |
307 |                         (Value::Float(l), Value::Float(r)) => Value::Float(l.powf(*r)),
    |                                                                              ^^^^ method not found in `&f64`

error[E0599]: no method named `powf` found for type `f64` in the current scope
   --> src/lib.rs:308:90
    |
308 |                         (Value::Integer(l), Value::Float(r)) => Value::Float((*l as f64).powf(*r)),
    |                                                                                          ^^^^ method not found in `f64`

error[E0599]: no method named `powf` found for reference `&f64` in the current scope
   --> src/lib.rs:309:80
    |
309 |                         (Value::Float(l), Value::Integer(r)) => Value::Float(l.powf(*r as f64)),
    |                                                                                ^^^^ method not found in `&f64`

error[E0599]: no method named `trunc` found for type `f64` in the current scope
   --> src/lib.rs:355:84
    |
355 |                         (Value::Float(l), Value::Float(r)) => Value::Float((l / r).trunc()),
    |                                                                                    ^^^^^ method not found in `f64`

error[E0599]: no method named `trunc` found for type `f64` in the current scope
   --> src/lib.rs:357:58
    |
357 | ...                   Value::Float((*l as f64 / r).trunc())
    |                                                    ^^^^^ method not found in `f64`

error[E0599]: no method named `trunc` found for type `f64` in the current scope
   --> src/lib.rs:360:58
    |
360 | ...                   Value::Float((l / *r as f64).trunc())
    |                                                    ^^^^^ method not found in `f64`

@hukasu
Copy link
Author

hukasu commented Nov 29, 2024

This is when running cargo check and cargo build on a lib, when running cargo test it compiles just fine.

@jieyouxu
Copy link
Member

jieyouxu commented Nov 29, 2024

Triage: I think f64::fract is in std but not core. Tagging T-libs/T-libs-api. Someone more familiar on the libs side of things will know better.

@jieyouxu jieyouxu added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Nov 29, 2024
@FractalFir
Copy link
Contributor

FractalFir commented Nov 29, 2024

EDIT: Irrelevant, I tought I found something possibly related, but I was wrong.

@hukasu
Copy link
Author

hukasu commented Nov 29, 2024

i saw a change in behavior between 1.82 and 1.83 though

@zachs18
Copy link
Contributor

zachs18 commented Nov 30, 2024

The example in the OP also fails on Rust 1.82.0: godbolt link. Can you provide an example of the behavior change you saw between 1.82.0 and 1.83.0?

@hukasu
Copy link
Author

hukasu commented Dec 1, 2024

weird, i reverted back to 1.82 and i does error. did i never run cargo check on that code while on 1.82 to notice? and when compiling the tests, since the tests are not no_std they do not fail.

what are the alternatives for those methods in no_std?

@hukasu
Copy link
Author

hukasu commented Dec 1, 2024

running a example that has #![no_std] still compiles and runs normally

@hukasu
Copy link
Author

hukasu commented Dec 1, 2024

Command No arguments --examples --example hello
cargo check
cargo build
cargo run Can't because it is a lib Can't run all examples at once
cargo test

the examples don't have tests, but they compile normally, the hello example has #![no_std]

@zachs18
Copy link
Contributor

zachs18 commented Dec 2, 2024

Given this project source (created using cargo new --lib f64-thing and modified from there), I get "E0599: no method named fract found for type f64 in the current scope" on all relevant invocations. If you have a different observation for your project, can you please provide your project's source code?

project source
f64-thing/
├── Cargo.lock
├── Cargo.toml
├── examples
│   └── hello.rs
└── src
    └── lib.rs
// lib.rs
#![no_std]

pub fn fract(f: f64) -> f64 {
    f.fract()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn it_works() {
        let result = fract(2.5);
        assert_eq!(result, 0.5);
    }
}
// examples/hello.rs
fn main() {}
Command No args --examples --example hello
cargo check E0599 E0599 E0599
cargo build E0599 E0599 E0599
cargo run N/A N/A E0599
cargo test E0599 E0599 E0599

what are the alternatives for those methods in no_std?

For some functions, you can use the pure-rust libm crate, but it does not appear to have fract specifically (though it does have trunc, so you can do x - x.trunc()).

@saethlin
Copy link
Member

saethlin commented Dec 2, 2024

Isn't this thread just rediscovering #50145?

@jieyouxu jieyouxu added C-discussion Category: Discussion or questions that doesn't represent real issues. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. C-bug Category: This is a bug. labels Dec 2, 2024
@jieyouxu
Copy link
Member

jieyouxu commented Dec 2, 2024

Triage: AFAICT this is intentional, marking as C-discussion for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-discussion Category: Discussion or questions that doesn't represent real issues. T-libs Relevant to the library team, which will review and decide on the PR/issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants