Skip to content

Two consecutive std::f32::sin() invocations on the same value give different results in certain scenarios #109118

Closed
@aleksa2808

Description

@aleksa2808

I noticed that, in certain scenarios, two consecutive std::f32::sin() function invocations can give two different results.

A scenario where I observed this is the following:

fn main() {
    let a: f32 = 0.7568419;

    assert_eq!(
        format!("{:x}", a.sin().to_bits()),
        format!("{:x}", a.sin().to_bits())
    );
    println!("{a}");
}

When the opt-level is set to 1, the assert fails with the following output:

thread 'main' panicked at 'assertion failed: `(left == right)`
  left: `"3f2fc6e9"`,
 right: `"3f2fc6ea"`', src\main.rs:4:5

When the opt-level is 0, both outputs are 3f2fc6ea, and when it is 2 or 3 both outputs are 3f2fc6e9.

Another surprising thing for me is that when I remove the last println (println!("{a}");), the assert passes even for opt-level=1. In that case, values are 3f2fc6ea only when the opt-level is 0, while for other values they are 3f2fc6e9.

Meta

rustc --version --verbose:

rustc 1.68.0 (2c8cc3432 2023-03-06)
binary: rustc
commit-hash: 2c8cc343237b8f7d5a3c3703e3a87f2eb2c54a74
commit-date: 2023-03-06
host: x86_64-pc-windows-msvc
release: 1.68.0
LLVM version: 15.0.6

This also occurs on the following nightly version:

rustc 1.70.0-nightly (7b4f48927 2023-03-12)
binary: rustc
commit-hash: 7b4f48927dce585f747a58083b45ab62b9d73a53
commit-date: 2023-03-12
host: x86_64-pc-windows-msvc
release: 1.70.0-nightly
LLVM version: 15.0.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsA-floating-pointArea: Floating point numbers and arithmeticC-bugCategory: This is a bug.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions