Skip to content

Commit 57cf40c

Browse files
committed
Hoist zero and one out into TestableFloat
1 parent 80bcd1a commit 57cf40c

File tree

1 file changed

+72
-88
lines changed
  • library/coretests/tests/floats

1 file changed

+72
-88
lines changed

library/coretests/tests/floats/mod.rs

Lines changed: 72 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ trait TestableFloat {
66
type Int;
77
/// Set the default tolerance for float comparison based on the type.
88
const APPROX: Self;
9+
const ZERO: Self;
10+
const ONE: Self;
911
const MIN_POSITIVE_NORMAL: Self;
1012
const MAX_SUBNORMAL: Self;
1113
/// Smallest number
@@ -23,6 +25,8 @@ trait TestableFloat {
2325
impl TestableFloat for f16 {
2426
type Int = u16;
2527
const APPROX: Self = 1e-3;
28+
const ZERO: Self = 0.0;
29+
const ONE: Self = 1.0;
2630
const MIN_POSITIVE_NORMAL: Self = Self::MIN_POSITIVE;
2731
const MAX_SUBNORMAL: Self = Self::MIN_POSITIVE.next_down();
2832
const TINY: Self = Self::from_bits(0x1);
@@ -35,6 +39,8 @@ impl TestableFloat for f16 {
3539
impl TestableFloat for f32 {
3640
type Int = u32;
3741
const APPROX: Self = 1e-6;
42+
const ZERO: Self = 0.0;
43+
const ONE: Self = 1.0;
3844
const MIN_POSITIVE_NORMAL: Self = Self::MIN_POSITIVE;
3945
const MAX_SUBNORMAL: Self = Self::MIN_POSITIVE.next_down();
4046
const TINY: Self = Self::from_bits(0x1);
@@ -47,6 +53,8 @@ impl TestableFloat for f32 {
4753
impl TestableFloat for f64 {
4854
type Int = u64;
4955
const APPROX: Self = 1e-6;
56+
const ZERO: Self = 0.0;
57+
const ONE: Self = 1.0;
5058
const MIN_POSITIVE_NORMAL: Self = Self::MIN_POSITIVE;
5159
const MAX_SUBNORMAL: Self = Self::MIN_POSITIVE.next_down();
5260
const TINY: Self = Self::from_bits(0x1);
@@ -59,6 +67,8 @@ impl TestableFloat for f64 {
5967
impl TestableFloat for f128 {
6068
type Int = u128;
6169
const APPROX: Self = 1e-9;
70+
const ZERO: Self = 0.0;
71+
const ONE: Self = 1.0;
6272
const MIN_POSITIVE_NORMAL: Self = Self::MIN_POSITIVE;
6373
const MAX_SUBNORMAL: Self = Self::MIN_POSITIVE.next_down();
6474
const TINY: Self = Self::from_bits(0x1);
@@ -378,15 +388,14 @@ float_test! {
378388
f128: #[cfg(any(miri, target_has_reliable_f128))],
379389
},
380390
test<Float> {
381-
let zero: Float = 0.0;
382-
assert_biteq!(0.0, zero);
383-
assert!(!zero.is_infinite());
384-
assert!(zero.is_finite());
385-
assert!(zero.is_sign_positive());
386-
assert!(!zero.is_sign_negative());
387-
assert!(!zero.is_nan());
388-
assert!(!zero.is_normal());
389-
assert!(matches!(zero.classify(), Fp::Zero));
391+
assert_biteq!(0.0, Float::ZERO);
392+
assert!(!Float::ZERO.is_infinite());
393+
assert!(Float::ZERO.is_finite());
394+
assert!(Float::ZERO.is_sign_positive());
395+
assert!(!Float::ZERO.is_sign_negative());
396+
assert!(!Float::ZERO.is_nan());
397+
assert!(!Float::ZERO.is_normal());
398+
assert!(matches!(Float::ZERO.classify(), Fp::Zero));
390399
}
391400
}
392401

@@ -417,15 +426,14 @@ float_test! {
417426
f128: #[cfg(any(miri, target_has_reliable_f128))],
418427
},
419428
test<Float> {
420-
let one: Float = 1.0;
421-
assert_biteq!(1.0, one);
422-
assert!(!one.is_infinite());
423-
assert!(one.is_finite());
424-
assert!(one.is_sign_positive());
425-
assert!(!one.is_sign_negative());
426-
assert!(!one.is_nan());
427-
assert!(one.is_normal());
428-
assert!(matches!(one.classify(), Fp::Normal));
429+
assert_biteq!(1.0, Float::ONE);
430+
assert!(!Float::ONE.is_infinite());
431+
assert!(Float::ONE.is_finite());
432+
assert!(Float::ONE.is_sign_positive());
433+
assert!(!Float::ONE.is_sign_negative());
434+
assert!(!Float::ONE.is_nan());
435+
assert!(Float::ONE.is_normal());
436+
assert!(matches!(Float::ONE.classify(), Fp::Normal));
429437
}
430438
}
431439

@@ -439,11 +447,10 @@ float_test! {
439447
let nan: Float = Float::NAN;
440448
let inf: Float = Float::INFINITY;
441449
let neg_inf: Float = Float::NEG_INFINITY;
442-
let zero: Float = 0.0;
443450
let pos: Float = 5.3;
444451
let neg: Float = -10.732;
445452
assert!(nan.is_nan());
446-
assert!(!zero.is_nan());
453+
assert!(!Float::ZERO.is_nan());
447454
assert!(!pos.is_nan());
448455
assert!(!neg.is_nan());
449456
assert!(!inf.is_nan());
@@ -461,13 +468,12 @@ float_test! {
461468
let nan: Float = Float::NAN;
462469
let inf: Float = Float::INFINITY;
463470
let neg_inf: Float = Float::NEG_INFINITY;
464-
let zero: Float = 0.0;
465471
let pos: Float = 42.8;
466472
let neg: Float = -109.2;
467473
assert!(!nan.is_infinite());
468474
assert!(inf.is_infinite());
469475
assert!(neg_inf.is_infinite());
470-
assert!(!zero.is_infinite());
476+
assert!(!Float::ZERO.is_infinite());
471477
assert!(!pos.is_infinite());
472478
assert!(!neg.is_infinite());
473479
}
@@ -483,13 +489,12 @@ float_test! {
483489
let nan: Float = Float::NAN;
484490
let inf: Float = Float::INFINITY;
485491
let neg_inf: Float = Float::NEG_INFINITY;
486-
let zero: Float = 0.0;
487492
let pos: Float = 42.8;
488493
let neg: Float = -109.2;
489494
assert!(!nan.is_finite());
490495
assert!(!inf.is_finite());
491496
assert!(!neg_inf.is_finite());
492-
assert!(zero.is_finite());
497+
assert!(Float::ZERO.is_finite());
493498
assert!(pos.is_finite());
494499
assert!(neg.is_finite());
495500
}
@@ -505,15 +510,13 @@ float_test! {
505510
let nan: Float = Float::NAN;
506511
let inf: Float = Float::INFINITY;
507512
let neg_inf: Float = Float::NEG_INFINITY;
508-
let zero: Float = 0.0;
509513
let neg_zero: Float = -0.0;
510-
let one : Float = 1.0;
511514
assert!(!nan.is_normal());
512515
assert!(!inf.is_normal());
513516
assert!(!neg_inf.is_normal());
514-
assert!(!zero.is_normal());
517+
assert!(!Float::ZERO.is_normal());
515518
assert!(!neg_zero.is_normal());
516-
assert!(one.is_normal());
519+
assert!(Float::ONE.is_normal());
517520
assert!(Float::MIN_POSITIVE_NORMAL.is_normal());
518521
assert!(!Float::MAX_SUBNORMAL.is_normal());
519522
}
@@ -528,15 +531,13 @@ float_test! {
528531
let nan: Float = Float::NAN;
529532
let inf: Float = Float::INFINITY;
530533
let neg_inf: Float = Float::NEG_INFINITY;
531-
let zero: Float = 0.0;
532534
let neg_zero: Float = -0.0;
533-
let one: Float = 1.0;
534535
assert!(matches!(nan.classify(), Fp::Nan));
535536
assert!(matches!(inf.classify(), Fp::Infinite));
536537
assert!(matches!(neg_inf.classify(), Fp::Infinite));
537-
assert!(matches!(zero.classify(), Fp::Zero));
538+
assert!(matches!(Float::ZERO.classify(), Fp::Zero));
538539
assert!(matches!(neg_zero.classify(), Fp::Zero));
539-
assert!(matches!(one.classify(), Fp::Normal));
540+
assert!(matches!(Float::ONE.classify(), Fp::Normal));
540541
assert!(matches!(Float::MIN_POSITIVE_NORMAL.classify(), Fp::Normal));
541542
assert!(matches!(Float::MAX_SUBNORMAL.classify(), Fp::Subnormal));
542543
}
@@ -756,15 +757,13 @@ float_test! {
756757
f128: #[cfg(any(miri, target_has_reliable_f128_math))],
757758
},
758759
test<Float> {
759-
let one: Float = 1.0;
760-
let zero: Float = 0.0;
761760
assert_biteq!(Float::INFINITY.abs(), Float::INFINITY);
762-
assert_biteq!(one.abs(), one);
763-
assert_biteq!(zero.abs(), zero);
764-
assert_biteq!((-zero).abs(), zero);
765-
assert_biteq!((-one).abs(), one);
761+
assert_biteq!(Float::ONE.abs(), Float::ONE);
762+
assert_biteq!(Float::ZERO.abs(), Float::ZERO);
763+
assert_biteq!((-Float::ZERO).abs(), Float::ZERO);
764+
assert_biteq!((-Float::ONE).abs(), Float::ONE);
766765
assert_biteq!(Float::NEG_INFINITY.abs(), Float::INFINITY);
767-
assert_biteq!((one / Float::NEG_INFINITY).abs(), zero);
766+
assert_biteq!((Float::ONE / Float::NEG_INFINITY).abs(), Float::ZERO);
768767
assert!(Float::NAN.abs().is_nan());
769768
}
770769
}
@@ -1001,15 +1000,13 @@ float_test! {
10011000
f128: #[cfg(any(miri, target_has_reliable_f128_math))],
10021001
},
10031002
test<Float> {
1004-
let one: Float = 1.0;
1005-
let zero: Float = 0.0;
1006-
assert_biteq!(Float::INFINITY.signum(), one);
1007-
assert_biteq!(one.signum(), one);
1008-
assert_biteq!(zero.signum(), one);
1009-
assert_biteq!((-zero).signum(), -one);
1010-
assert_biteq!((-one).signum(), -one);
1011-
assert_biteq!(Float::NEG_INFINITY.signum(), -one);
1012-
assert_biteq!((one / Float::NEG_INFINITY).signum(), -one);
1003+
assert_biteq!(Float::INFINITY.signum(), Float::ONE);
1004+
assert_biteq!(Float::ONE.signum(), Float::ONE);
1005+
assert_biteq!(Float::ZERO.signum(), Float::ONE);
1006+
assert_biteq!((-Float::ZERO).signum(), -Float::ONE);
1007+
assert_biteq!((-Float::ONE).signum(), -Float::ONE);
1008+
assert_biteq!(Float::NEG_INFINITY.signum(), -Float::ONE);
1009+
assert_biteq!((Float::ONE / Float::NEG_INFINITY).signum(), -Float::ONE);
10131010
assert!(Float::NAN.signum().is_nan());
10141011
}
10151012
}
@@ -1021,15 +1018,13 @@ float_test! {
10211018
f128: #[cfg(any(miri, target_has_reliable_f128))],
10221019
},
10231020
test<Float> {
1024-
let one: Float = 1.0;
1025-
let zero: Float = 0.0;
10261021
assert!(Float::INFINITY.is_sign_positive());
1027-
assert!(one.is_sign_positive());
1028-
assert!(zero.is_sign_positive());
1029-
assert!(!(-zero).is_sign_positive());
1030-
assert!(!(-one).is_sign_positive());
1022+
assert!(Float::ONE.is_sign_positive());
1023+
assert!(Float::ZERO.is_sign_positive());
1024+
assert!(!(-Float::ZERO).is_sign_positive());
1025+
assert!(!(-Float::ONE).is_sign_positive());
10311026
assert!(!Float::NEG_INFINITY.is_sign_positive());
1032-
assert!(!(one / Float::NEG_INFINITY).is_sign_positive());
1027+
assert!(!(Float::ONE / Float::NEG_INFINITY).is_sign_positive());
10331028
assert!(Float::NAN.is_sign_positive());
10341029
assert!(!(-Float::NAN).is_sign_positive());
10351030
}
@@ -1042,15 +1037,13 @@ float_test! {
10421037
f128: #[cfg(any(miri, target_has_reliable_f128))],
10431038
},
10441039
test<Float> {
1045-
let one: Float = 1.0;
1046-
let zero: Float = 0.0;
10471040
assert!(!Float::INFINITY.is_sign_negative());
1048-
assert!(!one.is_sign_negative());
1049-
assert!(!zero.is_sign_negative());
1050-
assert!((-zero).is_sign_negative());
1051-
assert!((-one).is_sign_negative());
1041+
assert!(!Float::ONE.is_sign_negative());
1042+
assert!(!Float::ZERO.is_sign_negative());
1043+
assert!((-Float::ZERO).is_sign_negative());
1044+
assert!((-Float::ONE).is_sign_negative());
10521045
assert!(Float::NEG_INFINITY.is_sign_negative());
1053-
assert!((one / Float::NEG_INFINITY).is_sign_negative());
1046+
assert!((Float::ONE / Float::NEG_INFINITY).is_sign_negative());
10541047
assert!(!Float::NAN.is_sign_negative());
10551048
assert!((-Float::NAN).is_sign_negative());
10561049
}
@@ -1063,19 +1056,17 @@ float_test! {
10631056
f128: #[cfg(any(miri, target_has_reliable_f128))],
10641057
},
10651058
test<Float> {
1066-
let one: Float = 1.0;
1067-
let zero: Float = 0.0;
10681059
assert_biteq!(Float::NEG_INFINITY.next_up(), Float::MIN);
10691060
assert_biteq!(Float::MIN.next_up(), -Float::MAX_DOWN);
1070-
assert_biteq!((-one - Float::EPSILON).next_up(), -one);
1061+
assert_biteq!((-Float::ONE - Float::EPSILON).next_up(), -Float::ONE);
10711062
assert_biteq!((-Float::MIN_POSITIVE_NORMAL).next_up(), -Float::MAX_SUBNORMAL);
10721063
assert_biteq!((-Float::TINY_UP).next_up(), -Float::TINY);
1073-
assert_biteq!((-Float::TINY).next_up(), -zero);
1074-
assert_biteq!((-zero).next_up(), Float::TINY);
1075-
assert_biteq!(zero.next_up(), Float::TINY);
1064+
assert_biteq!((-Float::TINY).next_up(), -Float::ZERO);
1065+
assert_biteq!((-Float::ZERO).next_up(), Float::TINY);
1066+
assert_biteq!(Float::ZERO.next_up(), Float::TINY);
10761067
assert_biteq!(Float::TINY.next_up(), Float::TINY_UP);
10771068
assert_biteq!(Float::MAX_SUBNORMAL.next_up(), Float::MIN_POSITIVE_NORMAL);
1078-
assert_biteq!(one.next_up(), 1.0 + Float::EPSILON);
1069+
assert_biteq!(Float::ONE.next_up(), 1.0 + Float::EPSILON);
10791070
assert_biteq!(Float::MAX.next_up(), Float::INFINITY);
10801071
assert_biteq!(Float::INFINITY.next_up(), Float::INFINITY);
10811072

@@ -1096,20 +1087,18 @@ float_test! {
10961087
f128: #[cfg(any(miri, target_has_reliable_f128))],
10971088
},
10981089
test<Float> {
1099-
let one: Float = 1.0;
1100-
let zero: Float = 0.0;
11011090
assert_biteq!(Float::NEG_INFINITY.next_down(), Float::NEG_INFINITY);
11021091
assert_biteq!(Float::MIN.next_down(), Float::NEG_INFINITY);
11031092
assert_biteq!((-Float::MAX_DOWN).next_down(), Float::MIN);
1104-
assert_biteq!((-one).next_down(), -1.0 - Float::EPSILON);
1093+
assert_biteq!((-Float::ONE).next_down(), -1.0 - Float::EPSILON);
11051094
assert_biteq!((-Float::MAX_SUBNORMAL).next_down(), -Float::MIN_POSITIVE_NORMAL);
11061095
assert_biteq!((-Float::TINY).next_down(), -Float::TINY_UP);
1107-
assert_biteq!((-zero).next_down(), -Float::TINY);
1108-
assert_biteq!((zero).next_down(), -Float::TINY);
1109-
assert_biteq!(Float::TINY.next_down(), zero);
1096+
assert_biteq!((-Float::ZERO).next_down(), -Float::TINY);
1097+
assert_biteq!((Float::ZERO).next_down(), -Float::TINY);
1098+
assert_biteq!(Float::TINY.next_down(), Float::ZERO);
11101099
assert_biteq!(Float::TINY_UP.next_down(), Float::TINY);
11111100
assert_biteq!(Float::MIN_POSITIVE_NORMAL.next_down(), Float::MAX_SUBNORMAL);
1112-
assert_biteq!((1.0 + Float::EPSILON).next_down(), one);
1101+
assert_biteq!((1.0 + Float::EPSILON).next_down(), Float::ONE);
11131102
assert_biteq!(Float::MAX.next_down(), Float::MAX_DOWN);
11141103
assert_biteq!(Float::INFINITY.next_down(), Float::MAX);
11151104

@@ -1134,14 +1123,12 @@ float_test! {
11341123
f128: #[cfg(all(not(miri), target_has_reliable_f128_math))],
11351124
},
11361125
test<Float> {
1137-
let one: Float = 1.0;
1138-
let zero: Float = 0.0;
11391126
assert!(Float::NAN.sqrt().is_nan());
11401127
assert!(Float::NEG_INFINITY.sqrt().is_nan());
1141-
assert!((-one).sqrt().is_nan());
1142-
assert_biteq!((-zero).sqrt(), -zero);
1143-
assert_biteq!(zero.sqrt(), zero);
1144-
assert_biteq!(one.sqrt(), one);
1128+
assert!((-Float::ONE).sqrt().is_nan());
1129+
assert_biteq!((-Float::ZERO).sqrt(), -Float::ZERO);
1130+
assert_biteq!(Float::ZERO.sqrt(), Float::ZERO);
1131+
assert_biteq!(Float::ONE.sqrt(), Float::ONE);
11451132
assert_biteq!(Float::INFINITY.sqrt(), Float::INFINITY);
11461133
}
11471134
}
@@ -1156,8 +1143,7 @@ float_test! {
11561143
f128: #[should_panic, cfg(any(miri, target_has_reliable_f128))],
11571144
},
11581145
test<Float> {
1159-
let one : Float = 1.0;
1160-
let _ = one.clamp(3.0, 1.0);
1146+
let _ = Float::ONE.clamp(3.0, 1.0);
11611147
}
11621148
}
11631149

@@ -1171,8 +1157,7 @@ float_test! {
11711157
f128: #[should_panic, cfg(any(miri, target_has_reliable_f128))],
11721158
},
11731159
test<Float> {
1174-
let one : Float = 1.0;
1175-
let _ = one.clamp(Float::NAN, 1.0);
1160+
let _ = Float::ONE.clamp(Float::NAN, 1.0);
11761161
}
11771162
}
11781163

@@ -1186,8 +1171,7 @@ float_test! {
11861171
f128: #[should_panic, cfg(any(miri, target_has_reliable_f128))],
11871172
},
11881173
test<Float> {
1189-
let one : Float = 1.0;
1190-
let _ = one.clamp(3.0, Float::NAN);
1174+
let _ = Float::ONE.clamp(3.0, Float::NAN);
11911175
}
11921176
}
11931177

0 commit comments

Comments
 (0)