Skip to content

Commit 21e2e98

Browse files
authored
Rollup merge of #59529 - DevQps:improve-rem-docs, r=cuviper
Added documentation on the remainder (Rem) operator for floating points. # 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
2 parents 57a4f17 + a1c7905 commit 21e2e98

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/libcore/ops/arith.rs

+15
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,21 @@ rem_impl_integer! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
537537

538538
macro_rules! rem_impl_float {
539539
($($t:ty)*) => ($(
540+
541+
/// The remainder from the division of two floats.
542+
///
543+
/// The remainder has the same sign as the dividend and is computed as:
544+
/// `x - (x / y).trunc() * y`.
545+
///
546+
/// # Examples
547+
/// ```
548+
/// let x: f32 = 50.50;
549+
/// let y: f32 = 8.125;
550+
/// let remainder = x - (x / y).trunc() * y;
551+
///
552+
/// // The answer to both operations is 1.75
553+
/// assert_eq!(x % y, remainder);
554+
/// ```
540555
#[stable(feature = "rust1", since = "1.0.0")]
541556
impl Rem for $t {
542557
type Output = $t;

0 commit comments

Comments
 (0)