Skip to content

Commit 4b8bef3

Browse files
deprecate f{32,64}::DIGITS
These constants are misleading: the number of significant digits varies for each value that these floating point numbers may encode but some programmers are taking them directly as an upper bound. This is wrong and is leading to programmers creating applications that directly mislead other users about their meaning, having a negative ecosystem-wide impact on mathematical accuracy. To contain the damage, deprecate them without replacement. It is hoped this will force programmers to reevaluate their use.
1 parent 7342213 commit 4b8bef3

File tree

2 files changed

+39
-12
lines changed

2 files changed

+39
-12
lines changed

library/core/src/num/f32.rs

+19-6
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,28 @@ pub const RADIX: u32 = f32::RADIX;
5454
)]
5555
pub const MANTISSA_DIGITS: u32 = f32::MANTISSA_DIGITS;
5656

57-
/// Approximate number of significant digits in base 10.
58-
/// Use [`f32::DIGITS`] instead.
57+
/// A minimum number of significant digits in base 10.
58+
/// This value is likely incorrect for usage,
59+
/// as it is not the upper limit of significant digits `f32` can contain.
5960
///
6061
/// # Examples
6162
///
6263
/// ```rust
6364
/// // deprecated way
6465
/// # #[allow(deprecated, deprecated_in_future)]
6566
/// let d = std::f32::DIGITS;
66-
///
67-
/// // intended way
67+
/// // also deprecated
68+
/// # #[allow(deprecated, deprecated_in_future)]
6869
/// let d = f32::DIGITS;
70+
///
71+
/// // the intended way may vary by application
6972
/// ```
7073
#[stable(feature = "rust1", since = "1.0.0")]
71-
#[rustc_deprecated(since = "TBD", reason = "replaced by the `DIGITS` associated constant on `f32`")]
74+
#[rustc_deprecated(
75+
since = "TBD",
76+
reason = "this value is incorrect or misleading as it actually encompasses a range"
77+
)]
78+
#[allow(deprecated, deprecated_in_future)]
7279
pub const DIGITS: u32 = f32::DIGITS;
7380

7481
/// [Machine epsilon] value for `f32`.
@@ -381,8 +388,14 @@ impl f32 {
381388
#[stable(feature = "assoc_int_consts", since = "1.43.0")]
382389
pub const MANTISSA_DIGITS: u32 = 24;
383390

384-
/// Approximate number of significant digits in base 10.
391+
/// A minimum number of significant digits in base 10.
392+
/// This value is likely incorrect for usage,
393+
/// as it is not the upper limit of significant digits `f32` can contain.
385394
#[stable(feature = "assoc_int_consts", since = "1.43.0")]
395+
#[rustc_deprecated(
396+
since = "TBD",
397+
reason = "this value is incorrect or misleading as it actually encompasses a range"
398+
)]
386399
pub const DIGITS: u32 = 6;
387400

388401
/// [Machine epsilon] value for `f32`.

library/core/src/num/f64.rs

+20-6
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,28 @@ pub const RADIX: u32 = f64::RADIX;
5454
)]
5555
pub const MANTISSA_DIGITS: u32 = f64::MANTISSA_DIGITS;
5656

57-
/// Approximate number of significant digits in base 10.
58-
/// Use [`f64::DIGITS`] instead.
57+
/// A minimum number of significant digits in base 10.
58+
/// This value is likely incorrect for usage,
59+
/// as it is not the upper limit of significant digits `f64` can contain.
5960
///
6061
/// # Examples
6162
///
6263
/// ```rust
6364
/// // deprecated way
6465
/// # #[allow(deprecated, deprecated_in_future)]
6566
/// let d = std::f64::DIGITS;
66-
///
67-
/// // intended way
67+
/// // also deprecated
68+
/// # #[allow(deprecated, deprecated_in_future)]
6869
/// let d = f64::DIGITS;
70+
///
71+
/// // the intended way may vary by application
6972
/// ```
7073
#[stable(feature = "rust1", since = "1.0.0")]
71-
#[rustc_deprecated(since = "TBD", reason = "replaced by the `DIGITS` associated constant on `f64`")]
74+
#[rustc_deprecated(
75+
since = "TBD",
76+
reason = "this value is incorrect or misleading as it actually encompasses a range"
77+
)]
78+
#[allow(deprecated, deprecated_in_future)]
7279
pub const DIGITS: u32 = f64::DIGITS;
7380

7481
/// [Machine epsilon] value for `f64`.
@@ -380,8 +387,15 @@ impl f64 {
380387
/// Number of significant digits in base 2.
381388
#[stable(feature = "assoc_int_consts", since = "1.43.0")]
382389
pub const MANTISSA_DIGITS: u32 = 53;
383-
/// Approximate number of significant digits in base 10.
390+
391+
/// A minimum number of significant digits in base 10.
392+
/// This value is likely incorrect for usage,
393+
/// as it is not the upper limit of significant digits `f64` can contain.
384394
#[stable(feature = "assoc_int_consts", since = "1.43.0")]
395+
#[rustc_deprecated(
396+
since = "TBD",
397+
reason = "this value is incorrect or misleading as it actually encompasses a range"
398+
)]
385399
pub const DIGITS: u32 = 15;
386400

387401
/// [Machine epsilon] value for `f64`.

0 commit comments

Comments
 (0)