Skip to content

Commit 6c3bdbe

Browse files
committed
auto merge of #13735 : aturon/rust/float-consts-take-2, r=brson
Follow-up on issue #13297 and PR #13710. Instead of following the (confusing) C/C++ approach of using `MIN_VALUE` for the smallest *positive* number, we introduce `MIN_POS_VALUE` (and in the Float trait, `min_pos_value`) to represent this number. This patch also removes a few remaining redundantly-defined constants that were missed last time around.
2 parents 0be4c33 + b8da4d7 commit 6c3bdbe

File tree

3 files changed

+40
-19
lines changed

3 files changed

+40
-19
lines changed

src/libstd/num/f32.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,11 @@ pub static DIGITS: uint = 6u;
7171

7272
pub static EPSILON: f32 = 1.19209290e-07_f32;
7373

74-
/// Minimum normalized f32 value
75-
pub static MIN_VALUE: f32 = 1.17549435e-38_f32;
76-
/// Maximum f32 value
74+
/// Smallest finite f32 value
75+
pub static MIN_VALUE: f32 = -3.40282347e+38_f32;
76+
/// Smallest positive, normalized f32 value
77+
pub static MIN_POS_VALUE: f32 = 1.17549435e-38_f32;
78+
/// Largest finite f32 value
7779
pub static MAX_VALUE: f32 = 3.40282347e+38_f32;
7880

7981
pub static MIN_EXP: int = -125;
@@ -90,8 +92,9 @@ pub static NEG_INFINITY: f32 = -1.0_f32/0.0_f32;
9092
pub mod consts {
9193
// FIXME: replace with mathematical constants from cmath.
9294

93-
// FIXME(#11621): These constants should be deprecated once CTFE is
94-
// implemented in favour of calling their respective functions in `Float`.
95+
// FIXME(#5527): These constants should be deprecated once associated
96+
// constants are implemented in favour of referencing the respective members
97+
// of `Float`.
9598

9699
/// Archimedes' constant
97100
pub static PI: f32 = 3.14159265358979323846264338327950288_f32;
@@ -342,6 +345,9 @@ impl Float for f32 {
342345
#[inline]
343346
fn max_10_exp(_: Option<f32>) -> int { MAX_10_EXP }
344347

348+
#[inline]
349+
fn min_pos_value(_: Option<f32>) -> f32 { MIN_POS_VALUE }
350+
345351
/// Constructs a floating point number by multiplying `x` by 2 raised to the
346352
/// power of `exp`
347353
#[inline]

src/libstd/num/f64.rs

+21-14
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,9 @@ mod cmath {
7373
}
7474
}
7575

76-
// FIXME(#11621): These constants should be deprecated once CTFE is implemented
77-
// in favour of calling their respective functions in `Bounded` and `Float`.
76+
// FIXME(#5527): These constants should be deprecated once associated
77+
// constants are implemented in favour of referencing the respective
78+
// members of `Bounded` and `Float`.
7879

7980
pub static RADIX: uint = 2u;
8081

@@ -83,9 +84,11 @@ pub static DIGITS: uint = 15u;
8384

8485
pub static EPSILON: f64 = 2.2204460492503131e-16_f64;
8586

86-
/// Minimum normalized f64 value
87-
pub static MIN_VALUE: f64 = 2.2250738585072014e-308_f64;
88-
/// Maximum f64 value
87+
/// Smallest finite f64 value
88+
pub static MIN_VALUE: f64 = -1.7976931348623157e+308_f64;
89+
/// Smallest positive, normalized f64 value
90+
pub static MIN_POS_VALUE: f64 = 2.2250738585072014e-308_f64;
91+
/// Largest finite f64 value
8992
pub static MAX_VALUE: f64 = 1.7976931348623157e+308_f64;
9093

9194
pub static MIN_EXP: int = -1021;
@@ -104,8 +107,9 @@ pub static NEG_INFINITY: f64 = -1.0_f64/0.0_f64;
104107
pub mod consts {
105108
// FIXME: replace with mathematical constants from cmath.
106109

107-
// FIXME(#11621): These constants should be deprecated once CTFE is
108-
// implemented in favour of calling their respective functions in `Float`.
110+
// FIXME(#5527): These constants should be deprecated once associated
111+
// constants are implemented in favour of referencing the respective members
112+
// of `Float`.
109113

110114
/// Archimedes' constant
111115
pub static PI: f64 = 3.14159265358979323846264338327950288_f64;
@@ -330,25 +334,28 @@ impl Float for f64 {
330334
}
331335

332336
#[inline]
333-
fn mantissa_digits(_: Option<f64>) -> uint { 53 }
337+
fn mantissa_digits(_: Option<f64>) -> uint { MANTISSA_DIGITS }
334338

335339
#[inline]
336-
fn digits(_: Option<f64>) -> uint { 15 }
340+
fn digits(_: Option<f64>) -> uint { DIGITS }
337341

338342
#[inline]
339-
fn epsilon() -> f64 { 2.2204460492503131e-16 }
343+
fn epsilon() -> f64 { EPSILON }
340344

341345
#[inline]
342-
fn min_exp(_: Option<f64>) -> int { -1021 }
346+
fn min_exp(_: Option<f64>) -> int { MIN_EXP }
343347

344348
#[inline]
345-
fn max_exp(_: Option<f64>) -> int { 1024 }
349+
fn max_exp(_: Option<f64>) -> int { MAX_EXP }
346350

347351
#[inline]
348-
fn min_10_exp(_: Option<f64>) -> int { -307 }
352+
fn min_10_exp(_: Option<f64>) -> int { MIN_10_EXP }
349353

350354
#[inline]
351-
fn max_10_exp(_: Option<f64>) -> int { 308 }
355+
fn max_10_exp(_: Option<f64>) -> int { MAX_10_EXP }
356+
357+
#[inline]
358+
fn min_pos_value(_: Option<f64>) -> f64 { MIN_POS_VALUE }
352359

353360
/// Constructs a floating point number by multiplying `x` by 2 raised to the
354361
/// power of `exp`

src/libstd/num/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,9 @@ pub fn pow<T: One + Mul<T, T>>(mut base: T, mut exp: uint) -> T {
190190
/// Numbers which have upper and lower bounds
191191
pub trait Bounded {
192192
// FIXME (#5527): These should be associated constants
193+
/// returns the smallest finite number this type can represent
193194
fn min_value() -> Self;
195+
/// returns the largest finite number this type can represent
194196
fn max_value() -> Self;
195197
}
196198

@@ -356,6 +358,8 @@ pub trait Float: Signed + Primitive {
356358
/// Returns the category that this number falls into.
357359
fn classify(self) -> FPCategory;
358360

361+
// FIXME (#5527): These should be associated constants
362+
359363
/// Returns the number of binary digits of mantissa that this type supports.
360364
fn mantissa_digits(unused_self: Option<Self>) -> uint;
361365
/// Returns the number of base-10 digits of precision that this type supports.
@@ -370,6 +374,8 @@ pub trait Float: Signed + Primitive {
370374
fn min_10_exp(unused_self: Option<Self>) -> int;
371375
/// Returns the maximum base-10 exponent that this type can represent.
372376
fn max_10_exp(unused_self: Option<Self>) -> int;
377+
/// Returns the smallest normalized positive number that this type can represent.
378+
fn min_pos_value(unused_self: Option<Self>) -> Self;
373379

374380
/// Constructs a floating point number created by multiplying `x` by 2
375381
/// raised to the power of `exp`.
@@ -434,6 +440,8 @@ pub trait Float: Signed + Primitive {
434440
/// legs of length `x` and `y`.
435441
fn hypot(self, other: Self) -> Self;
436442

443+
// FIXME (#5527): These should be associated constants
444+
437445
/// Archimedes' constant.
438446
fn pi() -> Self;
439447
/// 2.0 * pi.

0 commit comments

Comments
 (0)