Skip to content

docs: clarify that float Rem formula uses exact arithmetic - #153981

Open
mango766 wants to merge 1 commit into
rust-lang:mainfrom
mango766:fix-rem-float-doc-misleading-formula
Open

mango766 wants to merge 1 commit into
rust-lang:mainfrom
mango766:fix-rem-float-doc-misleading-formula

Conversation

@mango766

@mango766 mango766 commented Mar 17, 2026 •

Copy link
Copy Markdown

Summary

Fixes #133758.

The documentation for <{f16,f32,f64,f128} as Rem>::rem previously described the remainder as being "computed as: x - (x / y).trunc() * y", which reads as a literal Rust expression. In reality, the operation is mathematically defined as exact x - trunc(x/y) * y (computed in infinite precision, then rounded to the result type), equivalent to C's fmod. Evaluating the expression literally in Rust produces different results for many inputs due to intermediate floating-point rounding.

For example, as noted in the issue:

let (x, y) = (11f64, 1.1f64);
// These are NOT equal:
assert_eq!(x - (x / y).trunc() * y, x % y);  // panics: left=0.0, right≈1.1

This PR:

  • Rewrites the formula using mathematical notation (trunc(x / y)) rather than Rust method syntax, making it clear this is not meant as literal code
  • Explicitly states the computation uses infinite precision arithmetic
  • Adds a note explaining why the literal Rust expression differs
  • Simplifies the example to avoid suggesting the expressions are equivalent

This aligns with the consensus in #133758 and the IEEE 754 / C fmod semantics documented in RFC 3514.

…semantics

The documentation for `<float as Rem>::rem` previously presented the
formula `x - (x / y).trunc() * y` as if it were a Rust expression, but
this operation is actually computed using infinite precision (exact)
arithmetic, then rounded to the result type (equivalent to C's `fmod`).

Evaluating the expression literally in Rust can produce different results
due to intermediate rounding at each floating-point operation.

This clarifies the docs to explicitly state the formula is mathematical
and notes the discrepancy with the literal Rust expression. The example
is also simplified to avoid suggesting the two are equivalent.

Fixes rust-lang#133758

Co-Authored-By: Claude (claude-opus-4-6) <noreply@anthropic.com>
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Mar 17, 2026
@rustbot

rustbot commented Mar 17, 2026

Copy link
Copy Markdown
Collaborator

r? @joboet

rustbot has assigned @joboet.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: @scottmcm, libs
  • @scottmcm, libs expanded to 8 candidates
  • Random selection from Mark-Simulacrum, joboet, scottmcm

@rustbot

rustbot commented Mar 17, 2026

Copy link
Copy Markdown
Collaborator

⚠️ Warning ⚠️

  • There are issue links (such as #123) in the commit messages of the following commits.
    Please move them to the PR description, to avoid spamming the issues with references to the commit, and so this bot can automatically canonicalize them to avoid issues with subtree.

@joboet

joboet commented Mar 19, 2026

Copy link
Copy Markdown
Member

r? @traviscross

@rustbot rustbot assigned traviscross and unassigned joboet Mar 19, 2026

@tgross35 tgross35 left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r? me

Please handwrite all PR descriptions and commit messages, there is no need to restate what is literally in the diff.

View changes since this review

Comment on lines 635 to +637
/// let x: f32 = 50.50;
/// let y: f32 = 8.125;
/// let remainder = x - (x / y).trunc() * y;
///
/// // The answer to both operations is 1.75
/// assert_eq!(x % y, remainder);
/// assert_eq!(x % y, 1.75);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add assert!((x % 0.0).is_nan()) to demo the zero case? Infinity would be good as well.

/// The remainder has the same sign as the dividend and is computed
/// as the exact value `x - trunc(x / y) * y` (using infinite
/// precision arithmetic, then rounded to the result type). This is
/// the IEEE 754 remainder as implemented by C's `fmod`.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would simplify the rounding to:

... as the exact value x - trunc(x / y) * y. Note that the exact value differs from the Rust expression x - (x / y).trunc() * y for some inputs.

Comment on lines +625 to +626
/// precision arithmetic, then rounded to the result type). This is
/// the IEEE 754 remainder as implemented by C's `fmod`.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

754's remainder is C's remainder, no?

@rustbot rustbot assigned tgross35 and unassigned traviscross Jun 21, 2026

@tgross35 tgross35 left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, clicked the wrong button

View changes since this review

@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 21, 2026
@rustbot

rustbot commented Jun 21, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@rustbot rustbot added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Jun 21, 2026
@tgross35

Copy link
Copy Markdown
Member

@mango766 ping for the small updates

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

5 participants