Skip to content

Codegen and const eval evaluates -1.0 % -1.0 to 0 with different signs #109567

Closed
@cbeuw

Description

@cbeuw
pub fn f() -> f64 {
    std::hint::black_box(-1.0) % std::hint::black_box(-1.0)
}

pub fn g() -> f64 {
    -1.0 % -1.0
}

pub fn main() {
    println!("with black box: {}", f());
    println!("without black box: {}", g());
    assert_eq!(f().signum(), g().signum());
}
% rustc repro.rs && ./repro
with black box: -0
without black box: 0
thread 'main' panicked at 'assertion failed: `(left == right)`
  left: `-1.0`,
 right: `1.0`', repro.rs:12:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Not sure whether -1.0 % -1.0 should be 0 or -0 according to IEEE 754, but codegen (with black box) and const eval (without blackbox) should produce the same result

Meta

rustc --version --verbose:

rustc 1.70.0-nightly (1459b3128 2023-03-23)
binary: rustc
commit-hash: 1459b3128e288a85fcc4dd1fee7ada2cdcf28794
commit-date: 2023-03-23
host: aarch64-apple-darwin
release: 1.70.0-nightly
LLVM version: 15.0.7

Metadata

Metadata

Assignees

Labels

A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)A-floating-pointArea: Floating point numbers and arithmeticC-bugCategory: This is a bug.T-compilerRelevant to the compiler 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