docs: clarify that float Rem formula uses exact arithmetic#153981
Open
mango766 wants to merge 1 commit intorust-lang:mainfrom
Open
docs: clarify that float Rem formula uses exact arithmetic#153981mango766 wants to merge 1 commit intorust-lang:mainfrom
mango766 wants to merge 1 commit intorust-lang:mainfrom
Conversation
…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>
Collaborator
|
r? @joboet rustbot has assigned @joboet. Use Why was this reviewer chosen?The reviewer was selected based on:
|
Collaborator
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #133758.
The documentation for
<{f16,f32,f64,f128} as Rem>::rempreviously 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 exactx - trunc(x/y) * y(computed in infinite precision, then rounded to the result type), equivalent to C'sfmod. 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:
This PR:
trunc(x / y)) rather than Rust method syntax, making it clear this is not meant as literal codeThis aligns with the consensus in #133758 and the IEEE 754 / C
fmodsemantics documented in RFC 3514.