Skip to content

Commit ea369cb

Browse files
author
Christian
committed
Added an example that shows how the remainder function on floating point values is computed internally.
1 parent 8fe1087 commit ea369cb

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/libcore/ops/arith.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,17 @@ macro_rules! rem_impl_float {
540540

541541
/// The remainder from the division of two floats.
542542
///
543-
/// The remainder has the same sign as the dividend. For example: `-5.0 % 2.0 = -1.0`.
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 = 4.0;
549+
/// let y: f32 = 2.5;
550+
/// let remainder = x - (x / y).trunc() * y;
551+
///
552+
/// assert_eq!(x % y, remainder);
553+
/// ```
544554
#[stable(feature = "rust1", since = "1.0.0")]
545555
impl Rem for $t {
546556
type Output = $t;

0 commit comments

Comments
 (0)