Skip to content
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

Merged
merged 3 commits into from
Apr 2, 2019

Conversation

DevQps
Copy link
Contributor

@DevQps DevQps commented Mar 29, 2019

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 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 #57738

@rust-highfive
Copy link
Collaborator

r? @Kimundi

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 29, 2019
@Centril
Copy link
Contributor

Centril commented Mar 29, 2019

r? @steveklabnik

@cuviper
Copy link
Member

cuviper commented Mar 29, 2019

I'm not sure this is enough to clarify the operation. LLVM frem cites libm fmod:

The floating-point remainder of the division operation x/y calculated by this function is exactly the value x - n*y, where n is x/y with its fractional part truncated.

We could demonstrate that in Rust code like: x % y == x - (x / y).trunc() * y.

@DevQps
Copy link
Contributor Author

DevQps commented Mar 29, 2019

@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?

@cuviper
Copy link
Member

cuviper commented Mar 30, 2019

As long as it's kept simple, an example sounds good to me.

@DevQps
Copy link
Contributor Author

DevQps commented Mar 30, 2019

@cuviper What do you think about this?

/// ```
/// let x: f32 = 4.0;
/// let y: f32 = 2.5;
/// let remainder = x - (x / y).trunc() * y;
Copy link
Member

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.

Copy link
Contributor Author

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

Copy link
Member

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.

Copy link
Contributor Author

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?

Copy link
Member

Choose a reason for hiding this comment

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

Works for me!

@jonas-schievink jonas-schievink added the A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools label Apr 1, 2019
…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;
Copy link
Member

Choose a reason for hiding this comment

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

Works for me!

@Centril
Copy link
Contributor

Centril commented Apr 2, 2019

r? @cuviper

@rust-highfive rust-highfive assigned cuviper and unassigned steveklabnik Apr 2, 2019
@cuviper
Copy link
Member

cuviper commented Apr 2, 2019

@bors r+

@bors
Copy link
Contributor

bors commented Apr 2, 2019

📌 Commit a1c7905 has been approved by cuviper

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 2, 2019
Centril added a commit to Centril/rust that referenced this pull request Apr 2, 2019
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
bors added a commit that referenced this pull request Apr 2, 2019
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
@bors bors merged commit a1c7905 into rust-lang:master Apr 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

What does the % operator on floats do?
8 participants