conflux/core/math/
into_f64.rs

1use crate::core::math::FPIntof64;
2
3macro_rules! make_into_f64 {
4    ($t:ty) => {
5        impl FPIntof64 for $t {
6            #[inline]
7            fn cast_f64(&self) -> f64 {
8                *self as f64
9            }
10        }
11    };
12}
13
14make_into_f64!(isize);
15make_into_f64!(usize);
16make_into_f64!(i8);
17make_into_f64!(i16);
18make_into_f64!(i32);
19make_into_f64!(i64);
20make_into_f64!(u8);
21make_into_f64!(u16);
22make_into_f64!(u32);
23make_into_f64!(u64);
24make_into_f64!(f32);
25make_into_f64!(f64);
26
27#[cfg(test)]
28mod tests {
29    use super::*;
30    use paste::item;
31
32    macro_rules! make_test {
33        ($t:ty) => {
34            item! {
35                #[test]
36                fn [<test_into_f64_ $t>]() {
37                    let a = 8 as $t;
38                    let res = a.cast_f64();
39                    let b = 8f64;
40                    assert!(((b - res) as f64).abs() < std::f64::EPSILON);
41                }
42            }
43        };
44    }
45
46    make_test!(isize);
47    make_test!(usize);
48    make_test!(i8);
49    make_test!(u8);
50    make_test!(i16);
51    make_test!(u16);
52    make_test!(i32);
53    make_test!(u32);
54    make_test!(i64);
55    make_test!(u64);
56    make_test!(f32);
57    make_test!(f64);
58}