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
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/libcore/ops/arith.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,21 @@ rem_impl_integer! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }

macro_rules! rem_impl_float {
($($t:ty)*) => ($(

/// The remainder from the division of two floats.
///
/// The remainder has the same sign as the dividend and is computed as:
/// `x - (x / y).trunc() * y`.
///
/// # Examples
/// ```
/// let x: f32 = 50.50;
/// let y: f32 = 8.125;
/// 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!

///
/// // The answer to both operations is 1.75
/// assert_eq!(x % y, remainder);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
impl Rem for $t {
type Output = $t;
Expand Down