rust-lang / rust-lang/rust

`<{f16,f32,f64,f128} as Rem>::rem` documented definition is misleading w.r.t. intermediate rounding

Open
#133,758 12 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-docs C-bug T-libs
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Our documentation for impl Rem for {f16, f32, f64, f128} says:

The remainder has the same sign as the dividend and is computed as: x - (x / y).trunc() * y.

But that's not true. E.g.:

fn main() {
    let (x, y) = (11f64, 1.1f64);
    assert_eq!(x - (x / y).trunc() * y, x % y);
    //~^ PANIC
    // assertion `left == right` failed
    // left: 0.0
    // right: 1.0999999999999992
}

This mismatch creates a hazard when trying to correctly encode algorithms that rely on this semantic.

This tripped me up, e.g., when authoring:

cc #133485 #107904

cc @cuviper @Noratrieb @tczajka @Neutron3529 @BartMassey

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked Rem implementation documentation and the displayed formula, then reproduce the Rust example with f64 values to observe the intermediate-rounding mismatch. Revise the documentation so it does not describe % as exactly that real-number formula, and verify that the updated wording accurately matches the demonstrated behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.