| 1 | #![cfg(feature = "f16")] |
| 2 | |
| 3 | use lexical_util::f16::f16; |
| 4 | use lexical_util::num::Float; |
| 5 | |
| 6 | #[test] |
| 7 | fn as_f32_test() { |
| 8 | assert_eq!(f16::from_bits(1).as_f32_const(), 0.000000059604645); |
| 9 | assert_eq!(f16::ZERO.as_f32_const(), 0.0f32); |
| 10 | assert_eq!(f16::ZERO.to_bits(), 0); |
| 11 | assert_eq!(f16::ONE.as_f32_const(), 1.0f32); |
| 12 | assert_eq!(f16::ONE.to_bits(), (15 << 10)); |
| 13 | assert_eq!(f16::TWO.as_f32_const(), 2.0f32); |
| 14 | assert_eq!(f16::TWO.to_bits(), (16 << 10)); |
| 15 | assert_eq!(f16::from_bits(14 << 10).as_f32_const(), 0.5f32); |
| 16 | assert!(f16::NAN.as_f32_const().is_nan()); |
| 17 | assert!(f16::INFINITY.as_f32_const().is_inf()); |
| 18 | assert!(f16::NEG_INFINITY.as_f32_const().is_inf()); |
| 19 | } |
| 20 | |
| 21 | #[test] |
| 22 | fn from_f32_test() { |
| 23 | assert_eq!(f16::from_f32_const(2.980232e-08).to_bits(), 0); |
| 24 | assert_eq!(f16::from_f32_const(2.9802322e-08).to_bits(), 0); |
| 25 | assert_eq!(f16::from_f32_const(2.9802326e-08).to_bits(), 1); |
| 26 | assert_eq!(f16::from_f32_const(5.960464e-08).to_bits(), 1); |
| 27 | assert_eq!(f16::from_f32_const(5.9604645e-08).to_bits(), 1); |
| 28 | assert_eq!(f16::from_f32_const(5.960465e-08).to_bits(), 1); |
| 29 | assert!(f16::from_f32_const(f32::NAN).is_nan()); |
| 30 | assert!(f16::from_f32_const(f32::INFINITY).is_inf()); |
| 31 | assert!(f16::from_f32_const(f32::NEG_INFINITY).is_inf()); |
| 32 | } |
| 33 | |
| 34 | #[test] |
| 35 | fn math_tests() { |
| 36 | assert_eq!(f16::ONE + f16::ONE, f16::TWO); |
| 37 | assert_eq!(f16::ONE * f16::ONE, f16::ONE); |
| 38 | assert_eq!(f16::ONE / f16::ONE, f16::ONE); |
| 39 | assert_eq!(f16::ONE - f16::ONE, f16::ZERO); |
| 40 | assert_eq!(f16::ONE % f16::ONE, f16::ZERO); |
| 41 | } |