Skip to content

Commit 266812e

Browse files
committed
fix std::f32 and std::f64 constants
Some of the constant values in std::f32 were incorrectly copied from std::f64. More broadly, both modules defined their constants redundantly in two places, which is what led to the bug. Moreover, the specs for some of the constants were incorrent, even when the values were correct. Closes #13297. Closes #11537.
1 parent b5dd3f0 commit 266812e

File tree

3 files changed

+94
-75
lines changed

3 files changed

+94
-75
lines changed

src/libstd/num/f32.rs

+54-45
Original file line numberDiff line numberDiff line change
@@ -64,48 +64,56 @@ mod cmath {
6464
}
6565
}
6666

67-
// FIXME(#11621): These constants should be deprecated once CTFE is implemented
68-
// in favour of calling their respective functions in `Bounded` and `Float`.
69-
7067
pub static RADIX: uint = 2u;
7168

72-
pub static MANTISSA_DIGITS: uint = 53u;
73-
pub static DIGITS: uint = 15u;
69+
pub static MANTISSA_DIGITS: uint = 24u;
70+
pub static DIGITS: uint = 6u;
7471

75-
pub static EPSILON: f64 = 2.220446e-16_f64;
72+
pub static EPSILON: f32 = 1.19209290e-07_f32;
7673

77-
// FIXME (#1433): this is wrong, replace with hexadecimal (%a) statics
78-
// below.
79-
pub static MIN_VALUE: f64 = 2.225074e-308_f64;
80-
pub static MAX_VALUE: f64 = 1.797693e+308_f64;
74+
/// Minimum normalized f32 value
75+
pub static MIN_VALUE: f32 = 1.17549435e-38_f32;
76+
/// Maximum f32 value
77+
pub static MAX_VALUE: f32 = 3.40282347e+38_f32;
8178

82-
pub static MIN_EXP: uint = -1021u;
83-
pub static MAX_EXP: uint = 1024u;
79+
pub static MIN_EXP: int = -125;
80+
pub static MAX_EXP: int = 128;
8481

85-
pub static MIN_10_EXP: int = -307;
86-
pub static MAX_10_EXP: int = 308;
82+
pub static MIN_10_EXP: int = -37;
83+
pub static MAX_10_EXP: int = 38;
8784

8885
pub static NAN: f32 = 0.0_f32/0.0_f32;
8986
pub static INFINITY: f32 = 1.0_f32/0.0_f32;
9087
pub static NEG_INFINITY: f32 = -1.0_f32/0.0_f32;
9188

9289
/// Various useful constants.
9390
pub mod consts {
94-
// FIXME (requires Issue #1433 to fix): replace with mathematical
95-
// staticants from cmath.
91+
// FIXME: replace with mathematical constants from cmath.
9692

9793
// FIXME(#11621): These constants should be deprecated once CTFE is
9894
// implemented in favour of calling their respective functions in `Float`.
9995

10096
/// Archimedes' constant
10197
pub static PI: f32 = 3.14159265358979323846264338327950288_f32;
10298

99+
/// pi * 2.0
100+
pub static PI_2: f32 = 6.28318530717958647692528676655900576_f32;
101+
103102
/// pi/2.0
104103
pub static FRAC_PI_2: f32 = 1.57079632679489661923132169163975144_f32;
105104

105+
/// pi/3.0
106+
pub static FRAC_PI_3: f32 = 1.04719755119659774615421446109316763_f32;
107+
106108
/// pi/4.0
107109
pub static FRAC_PI_4: f32 = 0.785398163397448309615660845819875721_f32;
108110

111+
/// pi/6.0
112+
pub static FRAC_PI_6: f32 = 0.52359877559829887307710723054658381_f32;
113+
114+
/// pi/8.0
115+
pub static FRAC_PI_8: f32 = 0.39269908169872415480783042290993786_f32;
116+
109117
/// 1.0/pi
110118
pub static FRAC_1_PI: f32 = 0.318309886183790671537767526745028724_f32;
111119

@@ -251,24 +259,25 @@ impl Signed for f32 {
251259
}
252260

253261
impl Bounded for f32 {
262+
// NOTE: this is the smallest non-infinite f32 value, *not* MIN_VALUE
254263
#[inline]
255-
fn min_value() -> f32 { 1.17549435e-38 }
264+
fn min_value() -> f32 { -MAX_VALUE }
256265

257266
#[inline]
258-
fn max_value() -> f32 { 3.40282347e+38 }
267+
fn max_value() -> f32 { MAX_VALUE }
259268
}
260269

261270
impl Primitive for f32 {}
262271

263272
impl Float for f32 {
264273
#[inline]
265-
fn nan() -> f32 { 0.0 / 0.0 }
274+
fn nan() -> f32 { NAN }
266275

267276
#[inline]
268-
fn infinity() -> f32 { 1.0 / 0.0 }
277+
fn infinity() -> f32 { INFINITY }
269278

270279
#[inline]
271-
fn neg_infinity() -> f32 { -1.0 / 0.0 }
280+
fn neg_infinity() -> f32 { NEG_INFINITY }
272281

273282
#[inline]
274283
fn neg_zero() -> f32 { -0.0 }
@@ -313,25 +322,25 @@ impl Float for f32 {
313322
}
314323

315324
#[inline]
316-
fn mantissa_digits(_: Option<f32>) -> uint { 24 }
325+
fn mantissa_digits(_: Option<f32>) -> uint { MANTISSA_DIGITS }
317326

318327
#[inline]
319-
fn digits(_: Option<f32>) -> uint { 6 }
328+
fn digits(_: Option<f32>) -> uint { DIGITS }
320329

321330
#[inline]
322-
fn epsilon() -> f32 { 1.19209290e-07 }
331+
fn epsilon() -> f32 { EPSILON }
323332

324333
#[inline]
325-
fn min_exp(_: Option<f32>) -> int { -125 }
334+
fn min_exp(_: Option<f32>) -> int { MIN_EXP }
326335

327336
#[inline]
328-
fn max_exp(_: Option<f32>) -> int { 128 }
337+
fn max_exp(_: Option<f32>) -> int { MAX_EXP }
329338

330339
#[inline]
331-
fn min_10_exp(_: Option<f32>) -> int { -37 }
340+
fn min_10_exp(_: Option<f32>) -> int { MIN_10_EXP }
332341

333342
#[inline]
334-
fn max_10_exp(_: Option<f32>) -> int { 38 }
343+
fn max_10_exp(_: Option<f32>) -> int { MAX_10_EXP }
335344

336345
/// Constructs a floating point number by multiplying `x` by 2 raised to the
337346
/// power of `exp`
@@ -442,11 +451,11 @@ impl Float for f32 {
442451

443452
/// sqrt(2.0)
444453
#[inline]
445-
fn sqrt2() -> f32 { 1.41421356237309504880168872420969808 }
454+
fn sqrt2() -> f32 { consts::SQRT2 }
446455

447456
/// 1.0 / sqrt(2.0)
448457
#[inline]
449-
fn frac_1_sqrt2() -> f32 { 0.707106781186547524400844362104849039 }
458+
fn frac_1_sqrt2() -> f32 { consts::FRAC_1_SQRT2 }
450459

451460
#[inline]
452461
fn sqrt(self) -> f32 {
@@ -468,43 +477,43 @@ impl Float for f32 {
468477

469478
/// Archimedes' constant
470479
#[inline]
471-
fn pi() -> f32 { 3.14159265358979323846264338327950288 }
480+
fn pi() -> f32 { consts::PI }
472481

473482
/// 2.0 * pi
474483
#[inline]
475-
fn two_pi() -> f32 { 6.28318530717958647692528676655900576 }
484+
fn two_pi() -> f32 { consts::PI_2 }
476485

477486
/// pi / 2.0
478487
#[inline]
479-
fn frac_pi_2() -> f32 { 1.57079632679489661923132169163975144 }
488+
fn frac_pi_2() -> f32 { consts::FRAC_PI_2 }
480489

481490
/// pi / 3.0
482491
#[inline]
483-
fn frac_pi_3() -> f32 { 1.04719755119659774615421446109316763 }
492+
fn frac_pi_3() -> f32 { consts::FRAC_PI_3 }
484493

485494
/// pi / 4.0
486495
#[inline]
487-
fn frac_pi_4() -> f32 { 0.785398163397448309615660845819875721 }
496+
fn frac_pi_4() -> f32 { consts::FRAC_PI_4 }
488497

489498
/// pi / 6.0
490499
#[inline]
491-
fn frac_pi_6() -> f32 { 0.52359877559829887307710723054658381 }
500+
fn frac_pi_6() -> f32 { consts::FRAC_PI_6 }
492501

493502
/// pi / 8.0
494503
#[inline]
495-
fn frac_pi_8() -> f32 { 0.39269908169872415480783042290993786 }
504+
fn frac_pi_8() -> f32 { consts::FRAC_PI_8 }
496505

497506
/// 1 .0/ pi
498507
#[inline]
499-
fn frac_1_pi() -> f32 { 0.318309886183790671537767526745028724 }
508+
fn frac_1_pi() -> f32 { consts::FRAC_1_PI }
500509

501510
/// 2.0 / pi
502511
#[inline]
503-
fn frac_2_pi() -> f32 { 0.636619772367581343075535053490057448 }
512+
fn frac_2_pi() -> f32 { consts::FRAC_2_PI }
504513

505514
/// 2.0 / sqrt(pi)
506515
#[inline]
507-
fn frac_2_sqrtpi() -> f32 { 1.12837916709551257389615890312154517 }
516+
fn frac_2_sqrtpi() -> f32 { consts::FRAC_2_SQRTPI }
508517

509518
#[inline]
510519
fn sin(self) -> f32 {
@@ -549,23 +558,23 @@ impl Float for f32 {
549558

550559
/// Euler's number
551560
#[inline]
552-
fn e() -> f32 { 2.71828182845904523536028747135266250 }
561+
fn e() -> f32 { consts::E }
553562

554563
/// log2(e)
555564
#[inline]
556-
fn log2_e() -> f32 { 1.44269504088896340735992468100189214 }
565+
fn log2_e() -> f32 { consts::LOG2_E }
557566

558567
/// log10(e)
559568
#[inline]
560-
fn log10_e() -> f32 { 0.434294481903251827651128918916605082 }
569+
fn log10_e() -> f32 { consts::LOG10_E }
561570

562571
/// ln(2.0)
563572
#[inline]
564-
fn ln_2() -> f32 { 0.693147180559945309417232121458176568 }
573+
fn ln_2() -> f32 { consts::LN_2 }
565574

566575
/// ln(10.0)
567576
#[inline]
568-
fn ln_10() -> f32 { 2.30258509299404568401799145468436421 }
577+
fn ln_10() -> f32 { consts::LN_10 }
569578

570579
/// Returns the exponential of the number
571580
#[inline]

0 commit comments

Comments
 (0)