Skip to content

Commit af6f0f2

Browse files
author
Fabian Kössel
committed
Document round-off error in .mod_euc()-method, see issue #50179
1 parent 773ce53 commit af6f0f2

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/libstd/f32.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,10 @@ impl f32 {
254254

255255
/// Calculates the Euclidean modulo (self mod rhs), which is never negative.
256256
///
257-
/// In particular, the result `n` satisfies `0 <= n < rhs.abs()`.
257+
/// In particular, the return value `r` satisfies `0.0 <= r < rhs.abs()` in
258+
/// most cases. However, due to a floating point round-off error it can
259+
/// result in `r == rhs.abs()`, violating the mathematical definition, if
260+
/// `self` is much smaller than `rhs.abs()` in magnitude and `self < 0.0`.
258261
///
259262
/// # Examples
260263
///
@@ -266,6 +269,8 @@ impl f32 {
266269
/// assert_eq!((-a).mod_euc(b), 1.0);
267270
/// assert_eq!(a.mod_euc(-b), 3.0);
268271
/// assert_eq!((-a).mod_euc(-b), 1.0);
272+
/// // limitation due to round-off error
273+
/// assert!((-std::f32::EPSILON).mod_euc(3.0) != 0.0);
269274
/// ```
270275
#[inline]
271276
#[unstable(feature = "euclidean_division", issue = "49048")]

src/libstd/f64.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,10 @@ impl f64 {
230230

231231
/// Calculates the Euclidean modulo (self mod rhs), which is never negative.
232232
///
233-
/// In particular, the result `n` satisfies `0 <= n < rhs.abs()`.
233+
/// In particular, the return value `r` satisfies `0.0 <= r < rhs.abs()` in
234+
/// most cases. However, due to a floating point round-off error it can
235+
/// result in `r == rhs.abs()`, violating the mathematical definition, if
236+
/// `self` is much smaller than `rhs.abs()` in magnitude and `self < 0.0`.
234237
///
235238
/// # Examples
236239
///
@@ -242,6 +245,8 @@ impl f64 {
242245
/// assert_eq!((-a).mod_euc(b), 1.0);
243246
/// assert_eq!(a.mod_euc(-b), 3.0);
244247
/// assert_eq!((-a).mod_euc(-b), 1.0);
248+
/// // limitation due to round-off error
249+
/// assert!((-std::f64::EPSILON).mod_euc(3.0) != 0.0);
245250
/// ```
246251
#[inline]
247252
#[unstable(feature = "euclidean_division", issue = "49048")]

0 commit comments

Comments
 (0)