-
Notifications
You must be signed in to change notification settings - Fork 13.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added documentation on the remainder (Rem) operator for floating points. #59529
Conversation
r? @Kimundi (rust_highfive has picked a reviewer for you, use r? to override) |
I'm not sure this is enough to clarify the operation. LLVM
We could demonstrate that in Rust code like: |
@cuviper I think you're right. I will adjust it tomorrow and write a doc test as an example? Or do you think that would be overkill? |
As long as it's kept simple, an example sounds good to me. |
…int values is computed internally.
@cuviper What do you think about this? |
/// ``` | ||
/// let x: f32 = 4.0; | ||
/// let y: f32 = 2.5; | ||
/// let remainder = x - (x / y).trunc() * y; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd comment the expected value (1.5) for demonstration purposes. Might be nicer to choose inputs with a greater quotient too, so this doesn't look like just subtraction.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright! So what about 15.2 and 2.3? (Just some random numbers). Then I would also add a comment that the result is: 1.4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Random is fine, but maybe stick to base-2 numbers that float can represent exactly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cuviper I updated the example! What about this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works for me!
…floats and added a comment with the solution.
/// ``` | ||
/// let x: f32 = 4.0; | ||
/// let y: f32 = 2.5; | ||
/// let remainder = x - (x / y).trunc() * y; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works for me!
r? @cuviper |
@bors r+ |
📌 Commit a1c7905 has been approved by |
Added documentation on the remainder (Rem) operator for floating points. # Description As has been explained in rust-lang#57738 the remainder operator on floating points is not clear. This PR requests adds some information on how the `Rem` / remainder operator on floating points works. Note also that this description is for both `Rem<f32> for f32` and `Rem<f64> for f64` implementations. Ps. I wasn't really sure on how to formulate things. So please suggest changes if you have better idea's! closes rust-lang#57738
Rollup of 4 pull requests Successful merges: - #59166 (resolve: collect trait aliases along with traits) - #59341 (Fix custom relative libdir) - #59446 (Fix stack overflow when generating debuginfo for 'recursive' type) - #59529 (Added documentation on the remainder (Rem) operator for floating points.) Failed merges: r? @ghost
Description
As has been explained in #57738 the remainder operator on floating points is not clear.
This PR requests adds some information on how the
Rem
/ remainder operator on floating points works.Note also that this description is for both
Rem<f32> for f32
andRem<f64> for f64
implementations.Ps. I wasn't really sure on how to formulate things. So please suggest changes if you have better idea's!
closes #57738