Closed
Description
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
Labels
Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.Area: Documentation for any part of the project, including the compiler, standard library, and toolsArea: Floating point numbers and arithmeticCategory: This is a bug.Relevant to the library API team, which will review and decide on the PR/issue.