@@ -6,6 +6,8 @@ trait TestableFloat {
6
6
type Int ;
7
7
/// Set the default tolerance for float comparison based on the type.
8
8
const APPROX : Self ;
9
+ const ZERO : Self ;
10
+ const ONE : Self ;
9
11
const MIN_POSITIVE_NORMAL : Self ;
10
12
const MAX_SUBNORMAL : Self ;
11
13
/// Smallest number
@@ -23,6 +25,8 @@ trait TestableFloat {
23
25
impl TestableFloat for f16 {
24
26
type Int = u16 ;
25
27
const APPROX : Self = 1e-3 ;
28
+ const ZERO : Self = 0.0 ;
29
+ const ONE : Self = 1.0 ;
26
30
const MIN_POSITIVE_NORMAL : Self = Self :: MIN_POSITIVE ;
27
31
const MAX_SUBNORMAL : Self = Self :: MIN_POSITIVE . next_down ( ) ;
28
32
const TINY : Self = Self :: from_bits ( 0x1 ) ;
@@ -35,6 +39,8 @@ impl TestableFloat for f16 {
35
39
impl TestableFloat for f32 {
36
40
type Int = u32 ;
37
41
const APPROX : Self = 1e-6 ;
42
+ const ZERO : Self = 0.0 ;
43
+ const ONE : Self = 1.0 ;
38
44
const MIN_POSITIVE_NORMAL : Self = Self :: MIN_POSITIVE ;
39
45
const MAX_SUBNORMAL : Self = Self :: MIN_POSITIVE . next_down ( ) ;
40
46
const TINY : Self = Self :: from_bits ( 0x1 ) ;
@@ -47,6 +53,8 @@ impl TestableFloat for f32 {
47
53
impl TestableFloat for f64 {
48
54
type Int = u64 ;
49
55
const APPROX : Self = 1e-6 ;
56
+ const ZERO : Self = 0.0 ;
57
+ const ONE : Self = 1.0 ;
50
58
const MIN_POSITIVE_NORMAL : Self = Self :: MIN_POSITIVE ;
51
59
const MAX_SUBNORMAL : Self = Self :: MIN_POSITIVE . next_down ( ) ;
52
60
const TINY : Self = Self :: from_bits ( 0x1 ) ;
@@ -59,6 +67,8 @@ impl TestableFloat for f64 {
59
67
impl TestableFloat for f128 {
60
68
type Int = u128 ;
61
69
const APPROX : Self = 1e-9 ;
70
+ const ZERO : Self = 0.0 ;
71
+ const ONE : Self = 1.0 ;
62
72
const MIN_POSITIVE_NORMAL : Self = Self :: MIN_POSITIVE ;
63
73
const MAX_SUBNORMAL : Self = Self :: MIN_POSITIVE . next_down ( ) ;
64
74
const TINY : Self = Self :: from_bits ( 0x1 ) ;
@@ -378,15 +388,14 @@ float_test! {
378
388
f128: #[ cfg( any( miri, target_has_reliable_f128) ) ] ,
379
389
} ,
380
390
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 ) ) ;
390
399
}
391
400
}
392
401
@@ -417,15 +426,14 @@ float_test! {
417
426
f128: #[ cfg( any( miri, target_has_reliable_f128) ) ] ,
418
427
} ,
419
428
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 ) ) ;
429
437
}
430
438
}
431
439
@@ -439,11 +447,10 @@ float_test! {
439
447
let nan: Float = Float :: NAN ;
440
448
let inf: Float = Float :: INFINITY ;
441
449
let neg_inf: Float = Float :: NEG_INFINITY ;
442
- let zero: Float = 0.0 ;
443
450
let pos: Float = 5.3 ;
444
451
let neg: Float = -10.732 ;
445
452
assert!( nan. is_nan( ) ) ;
446
- assert!( !zero . is_nan( ) ) ;
453
+ assert!( !Float :: ZERO . is_nan( ) ) ;
447
454
assert!( !pos. is_nan( ) ) ;
448
455
assert!( !neg. is_nan( ) ) ;
449
456
assert!( !inf. is_nan( ) ) ;
@@ -461,13 +468,12 @@ float_test! {
461
468
let nan: Float = Float :: NAN ;
462
469
let inf: Float = Float :: INFINITY ;
463
470
let neg_inf: Float = Float :: NEG_INFINITY ;
464
- let zero: Float = 0.0 ;
465
471
let pos: Float = 42.8 ;
466
472
let neg: Float = -109.2 ;
467
473
assert!( !nan. is_infinite( ) ) ;
468
474
assert!( inf. is_infinite( ) ) ;
469
475
assert!( neg_inf. is_infinite( ) ) ;
470
- assert!( !zero . is_infinite( ) ) ;
476
+ assert!( !Float :: ZERO . is_infinite( ) ) ;
471
477
assert!( !pos. is_infinite( ) ) ;
472
478
assert!( !neg. is_infinite( ) ) ;
473
479
}
@@ -483,13 +489,12 @@ float_test! {
483
489
let nan: Float = Float :: NAN ;
484
490
let inf: Float = Float :: INFINITY ;
485
491
let neg_inf: Float = Float :: NEG_INFINITY ;
486
- let zero: Float = 0.0 ;
487
492
let pos: Float = 42.8 ;
488
493
let neg: Float = -109.2 ;
489
494
assert!( !nan. is_finite( ) ) ;
490
495
assert!( !inf. is_finite( ) ) ;
491
496
assert!( !neg_inf. is_finite( ) ) ;
492
- assert!( zero . is_finite( ) ) ;
497
+ assert!( Float :: ZERO . is_finite( ) ) ;
493
498
assert!( pos. is_finite( ) ) ;
494
499
assert!( neg. is_finite( ) ) ;
495
500
}
@@ -505,15 +510,13 @@ float_test! {
505
510
let nan: Float = Float :: NAN ;
506
511
let inf: Float = Float :: INFINITY ;
507
512
let neg_inf: Float = Float :: NEG_INFINITY ;
508
- let zero: Float = 0.0 ;
509
513
let neg_zero: Float = -0.0 ;
510
- let one : Float = 1.0 ;
511
514
assert!( !nan. is_normal( ) ) ;
512
515
assert!( !inf. is_normal( ) ) ;
513
516
assert!( !neg_inf. is_normal( ) ) ;
514
- assert!( !zero . is_normal( ) ) ;
517
+ assert!( !Float :: ZERO . is_normal( ) ) ;
515
518
assert!( !neg_zero. is_normal( ) ) ;
516
- assert!( one . is_normal( ) ) ;
519
+ assert!( Float :: ONE . is_normal( ) ) ;
517
520
assert!( Float :: MIN_POSITIVE_NORMAL . is_normal( ) ) ;
518
521
assert!( !Float :: MAX_SUBNORMAL . is_normal( ) ) ;
519
522
}
@@ -528,15 +531,13 @@ float_test! {
528
531
let nan: Float = Float :: NAN ;
529
532
let inf: Float = Float :: INFINITY ;
530
533
let neg_inf: Float = Float :: NEG_INFINITY ;
531
- let zero: Float = 0.0 ;
532
534
let neg_zero: Float = -0.0 ;
533
- let one: Float = 1.0 ;
534
535
assert!( matches!( nan. classify( ) , Fp :: Nan ) ) ;
535
536
assert!( matches!( inf. classify( ) , Fp :: Infinite ) ) ;
536
537
assert!( matches!( neg_inf. classify( ) , Fp :: Infinite ) ) ;
537
- assert!( matches!( zero . classify( ) , Fp :: Zero ) ) ;
538
+ assert!( matches!( Float :: ZERO . classify( ) , Fp :: Zero ) ) ;
538
539
assert!( matches!( neg_zero. classify( ) , Fp :: Zero ) ) ;
539
- assert!( matches!( one . classify( ) , Fp :: Normal ) ) ;
540
+ assert!( matches!( Float :: ONE . classify( ) , Fp :: Normal ) ) ;
540
541
assert!( matches!( Float :: MIN_POSITIVE_NORMAL . classify( ) , Fp :: Normal ) ) ;
541
542
assert!( matches!( Float :: MAX_SUBNORMAL . classify( ) , Fp :: Subnormal ) ) ;
542
543
}
@@ -756,15 +757,13 @@ float_test! {
756
757
f128: #[ cfg( any( miri, target_has_reliable_f128_math) ) ] ,
757
758
} ,
758
759
test<Float > {
759
- let one: Float = 1.0 ;
760
- let zero: Float = 0.0 ;
761
760
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 ) ;
766
765
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 ) ;
768
767
assert!( Float :: NAN . abs( ) . is_nan( ) ) ;
769
768
}
770
769
}
@@ -1001,15 +1000,13 @@ float_test! {
1001
1000
f128: #[ cfg( any( miri, target_has_reliable_f128_math) ) ] ,
1002
1001
} ,
1003
1002
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 ) ;
1013
1010
assert!( Float :: NAN . signum( ) . is_nan( ) ) ;
1014
1011
}
1015
1012
}
@@ -1021,15 +1018,13 @@ float_test! {
1021
1018
f128: #[ cfg( any( miri, target_has_reliable_f128) ) ] ,
1022
1019
} ,
1023
1020
test<Float > {
1024
- let one: Float = 1.0 ;
1025
- let zero: Float = 0.0 ;
1026
1021
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( ) ) ;
1031
1026
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( ) ) ;
1033
1028
assert!( Float :: NAN . is_sign_positive( ) ) ;
1034
1029
assert!( !( -Float :: NAN ) . is_sign_positive( ) ) ;
1035
1030
}
@@ -1042,15 +1037,13 @@ float_test! {
1042
1037
f128: #[ cfg( any( miri, target_has_reliable_f128) ) ] ,
1043
1038
} ,
1044
1039
test<Float > {
1045
- let one: Float = 1.0 ;
1046
- let zero: Float = 0.0 ;
1047
1040
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( ) ) ;
1052
1045
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( ) ) ;
1054
1047
assert!( !Float :: NAN . is_sign_negative( ) ) ;
1055
1048
assert!( ( -Float :: NAN ) . is_sign_negative( ) ) ;
1056
1049
}
@@ -1063,19 +1056,17 @@ float_test! {
1063
1056
f128: #[ cfg( any( miri, target_has_reliable_f128) ) ] ,
1064
1057
} ,
1065
1058
test<Float > {
1066
- let one: Float = 1.0 ;
1067
- let zero: Float = 0.0 ;
1068
1059
assert_biteq!( Float :: NEG_INFINITY . next_up( ) , Float :: MIN ) ;
1069
1060
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 ) ;
1071
1062
assert_biteq!( ( -Float :: MIN_POSITIVE_NORMAL ) . next_up( ) , -Float :: MAX_SUBNORMAL ) ;
1072
1063
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 ) ;
1076
1067
assert_biteq!( Float :: TINY . next_up( ) , Float :: TINY_UP ) ;
1077
1068
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 ) ;
1079
1070
assert_biteq!( Float :: MAX . next_up( ) , Float :: INFINITY ) ;
1080
1071
assert_biteq!( Float :: INFINITY . next_up( ) , Float :: INFINITY ) ;
1081
1072
@@ -1096,20 +1087,18 @@ float_test! {
1096
1087
f128: #[ cfg( any( miri, target_has_reliable_f128) ) ] ,
1097
1088
} ,
1098
1089
test<Float > {
1099
- let one: Float = 1.0 ;
1100
- let zero: Float = 0.0 ;
1101
1090
assert_biteq!( Float :: NEG_INFINITY . next_down( ) , Float :: NEG_INFINITY ) ;
1102
1091
assert_biteq!( Float :: MIN . next_down( ) , Float :: NEG_INFINITY ) ;
1103
1092
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 ) ;
1105
1094
assert_biteq!( ( -Float :: MAX_SUBNORMAL ) . next_down( ) , -Float :: MIN_POSITIVE_NORMAL ) ;
1106
1095
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 ) ;
1110
1099
assert_biteq!( Float :: TINY_UP . next_down( ) , Float :: TINY ) ;
1111
1100
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 ) ;
1113
1102
assert_biteq!( Float :: MAX . next_down( ) , Float :: MAX_DOWN ) ;
1114
1103
assert_biteq!( Float :: INFINITY . next_down( ) , Float :: MAX ) ;
1115
1104
@@ -1134,14 +1123,12 @@ float_test! {
1134
1123
f128: #[ cfg( all( not( miri) , target_has_reliable_f128_math) ) ] ,
1135
1124
} ,
1136
1125
test<Float > {
1137
- let one: Float = 1.0 ;
1138
- let zero: Float = 0.0 ;
1139
1126
assert!( Float :: NAN . sqrt( ) . is_nan( ) ) ;
1140
1127
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 ) ;
1145
1132
assert_biteq!( Float :: INFINITY . sqrt( ) , Float :: INFINITY ) ;
1146
1133
}
1147
1134
}
@@ -1156,8 +1143,7 @@ float_test! {
1156
1143
f128: #[ should_panic, cfg( any( miri, target_has_reliable_f128) ) ] ,
1157
1144
} ,
1158
1145
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 ) ;
1161
1147
}
1162
1148
}
1163
1149
@@ -1171,8 +1157,7 @@ float_test! {
1171
1157
f128: #[ should_panic, cfg( any( miri, target_has_reliable_f128) ) ] ,
1172
1158
} ,
1173
1159
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 ) ;
1176
1161
}
1177
1162
}
1178
1163
@@ -1186,8 +1171,7 @@ float_test! {
1186
1171
f128: #[ should_panic, cfg( any( miri, target_has_reliable_f128) ) ] ,
1187
1172
} ,
1188
1173
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 ) ;
1191
1175
}
1192
1176
}
1193
1177
0 commit comments