@@ -27,6 +27,9 @@ trait TestableFloat: Sized {
27
27
const NAN_MASK1 : Self :: Int ;
28
28
/// Second pattern over the mantissa
29
29
const NAN_MASK2 : Self :: Int ;
30
+ const EPS_ADD : Self ;
31
+ const EPS_MUL : Self ;
32
+ const EPS_DIV : Self ;
30
33
}
31
34
32
35
impl TestableFloat for f16 {
@@ -44,6 +47,9 @@ impl TestableFloat for f16 {
44
47
const MAX_DOWN : Self = Self :: from_bits ( 0x7bfe ) ;
45
48
const NAN_MASK1 : Self :: Int = 0x02aa ;
46
49
const NAN_MASK2 : Self :: Int = 0x0155 ;
50
+ const EPS_ADD : Self = if cfg ! ( miri) { 1e1 } else { 0.0 } ;
51
+ const EPS_MUL : Self = if cfg ! ( miri) { 1e3 } else { 0.0 } ;
52
+ const EPS_DIV : Self = if cfg ! ( miri) { 1e0 } else { 0.0 } ;
47
53
}
48
54
49
55
impl TestableFloat for f32 {
@@ -63,6 +69,9 @@ impl TestableFloat for f32 {
63
69
const MAX_DOWN : Self = Self :: from_bits ( 0x7f7f_fffe ) ;
64
70
const NAN_MASK1 : Self :: Int = 0x002a_aaaa ;
65
71
const NAN_MASK2 : Self :: Int = 0x0055_5555 ;
72
+ const EPS_ADD : Self = if cfg ! ( miri) { 1e-3 } else { 0.0 } ;
73
+ const EPS_MUL : Self = if cfg ! ( miri) { 1e-1 } else { 0.0 } ;
74
+ const EPS_DIV : Self = if cfg ! ( miri) { 1e-4 } else { 0.0 } ;
66
75
}
67
76
68
77
impl TestableFloat for f64 {
@@ -78,6 +87,9 @@ impl TestableFloat for f64 {
78
87
const MAX_DOWN : Self = Self :: from_bits ( 0x7fef_ffff_ffff_fffe ) ;
79
88
const NAN_MASK1 : Self :: Int = 0x000a_aaaa_aaaa_aaaa ;
80
89
const NAN_MASK2 : Self :: Int = 0x0005_5555_5555_5555 ;
90
+ const EPS_ADD : Self = if cfg ! ( miri) { 1e-6 } else { 0.0 } ;
91
+ const EPS_MUL : Self = if cfg ! ( miri) { 1e-6 } else { 0.0 } ;
92
+ const EPS_DIV : Self = if cfg ! ( miri) { 1e-6 } else { 0.0 } ;
81
93
}
82
94
83
95
impl TestableFloat for f128 {
@@ -93,6 +105,9 @@ impl TestableFloat for f128 {
93
105
const MAX_DOWN : Self = Self :: from_bits ( 0x7ffefffffffffffffffffffffffffffe ) ;
94
106
const NAN_MASK1 : Self :: Int = 0x0000aaaaaaaaaaaaaaaaaaaaaaaaaaaa ;
95
107
const NAN_MASK2 : Self :: Int = 0x00005555555555555555555555555555 ;
108
+ const EPS_ADD : Self = if cfg ! ( miri) { 1e-6 } else { 0.0 } ;
109
+ const EPS_MUL : Self = if cfg ! ( miri) { 1e-6 } else { 0.0 } ;
110
+ const EPS_DIV : Self = if cfg ! ( miri) { 1e-6 } else { 0.0 } ;
96
111
}
97
112
98
113
/// Determine the tolerance for values of the argument type.
@@ -1440,3 +1455,27 @@ float_test! {
1440
1455
assert_biteq!( neg_inf. to_radians( ) , neg_inf) ;
1441
1456
}
1442
1457
}
1458
+
1459
+ float_test ! {
1460
+ name: to_algebraic,
1461
+ attrs: {
1462
+ f16: #[ cfg( target_has_reliable_f16) ] ,
1463
+ f128: #[ cfg( target_has_reliable_f128) ] ,
1464
+ } ,
1465
+ test<Float > {
1466
+ let a: Float = 123.0 ;
1467
+ let b: Float = 456.0 ;
1468
+
1469
+ // Check that individual operations match their primitive counterparts.
1470
+ //
1471
+ // This is a check of current implementations and does NOT imply any form of
1472
+ // guarantee about future behavior. The compiler reserves the right to make
1473
+ // these operations inexact matches in the future.
1474
+
1475
+ assert_approx_eq!( a. algebraic_add( b) , a + b, Float :: EPS_ADD ) ;
1476
+ assert_approx_eq!( a. algebraic_sub( b) , a - b, Float :: EPS_ADD ) ;
1477
+ assert_approx_eq!( a. algebraic_mul( b) , a * b, Float :: EPS_MUL ) ;
1478
+ assert_approx_eq!( a. algebraic_div( b) , a / b, Float :: EPS_DIV ) ;
1479
+ assert_approx_eq!( a. algebraic_rem( b) , a % b, Float :: EPS_DIV ) ;
1480
+ }
1481
+ }
0 commit comments