From f4967afa05c7e6ba53c016be554a207108d387ed Mon Sep 17 00:00:00 2001 From: Mathilda Grace Date: Wed, 26 Jun 2024 20:17:05 -0400 Subject: [PATCH 1/7] Add unit tests for trait method `float::Float::integer_decode` --- src/float.rs | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/src/float.rs b/src/float.rs index d0d21a50..bf4c05ed 100644 --- a/src/float.rs +++ b/src/float.rs @@ -2397,6 +2397,99 @@ mod tests { check::(1e-12); } + /// Test the behavior of `Float::integer_decode` with the `given` input and `expected` output values. + #[cfg(any(feature = "std", feature = "libm"))] + fn test_integer_decode(given: T, expected: (u64, i16, i8)) + where + T: crate::float::Float + core::fmt::LowerExp + core::fmt::Debug, + { + use crate::float::Float; + + let found = Float::integer_decode(given); + + assert!( + expected == found, + "unexpected output of `Float::integer_decode({0:e})` +\texpected: ({1:#x} {2} {3}) +\t found: {4:#x} {5} {6}", + given, + expected.0, + expected.1, + expected.2, + found.0, + found.1, + found.2 + ); + + // Destructure the `found` output and cast values as float. + let mantissa_f = T::from(found.0).unwrap(); + let exponent_f = T::from(found.1).unwrap(); + let sign_f = T::from(found.2).unwrap(); + + // Recover the `given` input using equation: sign * mantissa * 2^exponent. + let recovered = sign_f * mantissa_f * exponent_f.exp2(); + let deviation = recovered - given; + let tolerance = T::from(1e-6).unwrap(); + + assert_eq!(T::one(), tolerance.signum(), "tolerance must be positive"); + assert!( + recovered == given || deviation.abs() < tolerance, + "absolute deviation must not exceed tolerance` +\t given: {:+e} +\trecovered: {:+e} +\tdeviation: {:+e} +\ttolerance: <{:e}", + given, + recovered, + deviation, + tolerance + ); + } + + #[test] + #[cfg(any(feature = "std", feature = "libm"))] + fn integer_decode_f32() { + for sign in [1, -1] { + let sign_f = sign as f32; + test_integer_decode(sign_f * 0.0__f32, (0x000000, -150, sign)); + test_integer_decode(sign_f * f32::MIN_POSITIVE, (0x800000, -149, sign)); + test_integer_decode(sign_f * 0.25_f32, (0x800000, -25, sign)); + test_integer_decode(sign_f * 0.5__f32, (0x800000, -24, sign)); + test_integer_decode(sign_f * 1____f32, (0x800000, -23, sign)); + test_integer_decode(sign_f * 1.5__f32, (0xc00000, -23, sign)); + test_integer_decode(sign_f * 2____f32, (0x800000, -22, sign)); + test_integer_decode(sign_f * 2.5__f32, (0xa00000, -22, sign)); + test_integer_decode(sign_f * 3____f32, (0xc00000, -22, sign)); + test_integer_decode(sign_f * 4____f32, (0x800000, -21, sign)); + test_integer_decode(sign_f * 5____f32, (0xa00000, -21, sign)); + test_integer_decode(sign_f * 42___f32, (0xa80000, -18, sign)); + test_integer_decode(sign_f * f32::MAX, (0xffffff, 104, sign)); + test_integer_decode(sign_f * f32::INFINITY, (0x800000, 105, sign)); + } + } + + #[test] + #[cfg(any(feature = "std", feature = "libm"))] + fn integer_decode_f64() { + for sign in [1, -1] { + let sign_f = sign as f64; + test_integer_decode(sign_f * 0.0__f64, (0x00000000000000, -1075, sign)); + test_integer_decode(sign_f * f64::MIN_POSITIVE, (0x10000000000000, -1074, sign)); + test_integer_decode(sign_f * 0.25_f64, (0x10000000000000, -54, sign)); + test_integer_decode(sign_f * 0.5__f64, (0x10000000000000, -53, sign)); + test_integer_decode(sign_f * 1____f64, (0x10000000000000, -52, sign)); + test_integer_decode(sign_f * 1.5__f64, (0x18000000000000, -52, sign)); + test_integer_decode(sign_f * 2____f64, (0x10000000000000, -51, sign)); + test_integer_decode(sign_f * 2.5__f64, (0x14000000000000, -51, sign)); + test_integer_decode(sign_f * 3____f64, (0x18000000000000, -51, sign)); + test_integer_decode(sign_f * 4____f64, (0x10000000000000, -50, sign)); + test_integer_decode(sign_f * 5____f64, (0x14000000000000, -50, sign)); + test_integer_decode(sign_f * 42___f64, (0x15000000000000, -47, sign)); + test_integer_decode(sign_f * f64::MAX, (0x1fffffffffffff, 971, sign)); + test_integer_decode(sign_f * f64::INFINITY, (0x10000000000000, 972, sign)); + } + } + #[test] #[cfg(any(feature = "std", feature = "libm"))] fn copysign() { From 25eb45fa0c028ee7a42a39540acdcf74c267520f Mon Sep 17 00:00:00 2001 From: Mathilda Grace Date: Thu, 27 Jun 2024 15:18:31 -0400 Subject: [PATCH 2/7] Add FIXME related to NaN inputs to `float::Float::integer_decode` --- src/float.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/float.rs b/src/float.rs index bf4c05ed..7c35cbfc 100644 --- a/src/float.rs +++ b/src/float.rs @@ -2466,6 +2466,8 @@ mod tests { test_integer_decode(sign_f * f32::MAX, (0xffffff, 104, sign)); test_integer_decode(sign_f * f32::INFINITY, (0x800000, 105, sign)); } + // FIXME: Unclear if we should be able to recover NaN inputs + // test_integer_decode(f32::NAN, (0xc00000, 105, 1)); } #[test] @@ -2488,6 +2490,8 @@ mod tests { test_integer_decode(sign_f * f64::MAX, (0x1fffffffffffff, 971, sign)); test_integer_decode(sign_f * f64::INFINITY, (0x10000000000000, 972, sign)); } + // FIXME: Unclear if we should be able to recover NaN inputs + // test_integer_decode(f64::NAN, (0x18000000000000, 972, 1)); } #[test] From 9232b284677f813a316416d10fe38a487fe0c9ce Mon Sep 17 00:00:00 2001 From: Mathilda Grace Date: Thu, 27 Jun 2024 20:34:30 -0400 Subject: [PATCH 3/7] Fix assertion message in `test_integer_decode` --- src/float.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/float.rs b/src/float.rs index 7c35cbfc..5ff1e4ae 100644 --- a/src/float.rs +++ b/src/float.rs @@ -2411,7 +2411,7 @@ mod tests { expected == found, "unexpected output of `Float::integer_decode({0:e})` \texpected: ({1:#x} {2} {3}) -\t found: {4:#x} {5} {6}", +\t found: ({4:#x} {5} {6})", given, expected.0, expected.1, From a6e2912fa817eb4f532fb21d881ab876ce22e4d2 Mon Sep 17 00:00:00 2001 From: Mathilda Grace Date: Fri, 28 Jun 2024 13:55:52 -0400 Subject: [PATCH 4/7] Update doc strings to clarify which numbers are recoverable --- src/float.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/float.rs b/src/float.rs index a1e424f5..825cf1c8 100644 --- a/src/float.rs +++ b/src/float.rs @@ -775,7 +775,9 @@ pub trait FloatCore: Num + NumCast + Neg + PartialOrd + Copy { fn to_radians(self) -> Self; /// Returns the mantissa, base 2 exponent, and sign as integers, respectively. + /// /// The original number can be recovered by `sign * mantissa * 2 ^ exponent`. + /// This formula only works for zero, normal, and infinite numbers (per `classify()`) /// /// # Examples /// @@ -1861,7 +1863,9 @@ pub trait Float: Num + Copy + NumCast + PartialOrd + Neg { fn atanh(self) -> Self; /// Returns the mantissa, base 2 exponent, and sign as integers, respectively. + /// /// The original number can be recovered by `sign * mantissa * 2 ^ exponent`. + /// This formula only works for zero, normal, and infinite numbers (per `classify()`) /// /// ``` /// use num_traits::Float; From 300a7f91c78529e2423195f32b373209edd4256d Mon Sep 17 00:00:00 2001 From: Mathilda Grace Date: Fri, 28 Jun 2024 13:56:23 -0400 Subject: [PATCH 5/7] Remove FIXME related to NaN inputs --- src/float.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/float.rs b/src/float.rs index 825cf1c8..9527e4ae 100644 --- a/src/float.rs +++ b/src/float.rs @@ -2470,8 +2470,6 @@ mod tests { test_integer_decode(sign_f * f32::MAX, (0xffffff, 104, sign)); test_integer_decode(sign_f * f32::INFINITY, (0x800000, 105, sign)); } - // FIXME: Unclear if we should be able to recover NaN inputs - // test_integer_decode(f32::NAN, (0xc00000, 105, 1)); } #[test] @@ -2494,8 +2492,6 @@ mod tests { test_integer_decode(sign_f * f64::MAX, (0x1fffffffffffff, 971, sign)); test_integer_decode(sign_f * f64::INFINITY, (0x10000000000000, 972, sign)); } - // FIXME: Unclear if we should be able to recover NaN inputs - // test_integer_decode(f64::NAN, (0x18000000000000, 972, 1)); } #[test] From 6309a5399396db5db7f27486e223fc0aac7f42ee Mon Sep 17 00:00:00 2001 From: Mathilda Grace Date: Fri, 28 Jun 2024 15:20:17 -0400 Subject: [PATCH 6/7] Add test cases for subnormal inputs to `Float::integer_decode` --- src/float.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/float.rs b/src/float.rs index 9527e4ae..a9be2527 100644 --- a/src/float.rs +++ b/src/float.rs @@ -2456,6 +2456,7 @@ mod tests { for sign in [1, -1] { let sign_f = sign as f32; test_integer_decode(sign_f * 0.0__f32, (0x000000, -150, sign)); + test_integer_decode(sign_f * 1.0e-40_f32, (0x022d84, -150, sign)); // subnormal (between 0 and MIN_POSITIVE) test_integer_decode(sign_f * f32::MIN_POSITIVE, (0x800000, -149, sign)); test_integer_decode(sign_f * 0.25_f32, (0x800000, -25, sign)); test_integer_decode(sign_f * 0.5__f32, (0x800000, -24, sign)); @@ -2478,6 +2479,7 @@ mod tests { for sign in [1, -1] { let sign_f = sign as f64; test_integer_decode(sign_f * 0.0__f64, (0x00000000000000, -1075, sign)); + test_integer_decode(sign_f * 1.0e-308_f64, (0x0e61acf033d1a4, -1075, sign)); // subnormal (between 0 and MIN_POSITIVE) test_integer_decode(sign_f * f64::MIN_POSITIVE, (0x10000000000000, -1074, sign)); test_integer_decode(sign_f * 0.25_f64, (0x10000000000000, -54, sign)); test_integer_decode(sign_f * 0.5__f64, (0x10000000000000, -53, sign)); From c3b277a6261ce068f819e28a8a2c59000d4724d3 Mon Sep 17 00:00:00 2001 From: Mathilda Grace Date: Fri, 28 Jun 2024 16:16:39 -0400 Subject: [PATCH 7/7] Add comments to mantissa calculation --- src/float.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/float.rs b/src/float.rs index a9be2527..cd950137 100644 --- a/src/float.rs +++ b/src/float.rs @@ -2055,8 +2055,10 @@ fn integer_decode_f32(f: f32) -> (u64, i16, i8) { let sign: i8 = if bits >> 31 == 0 { 1 } else { -1 }; let mut exponent: i16 = ((bits >> 23) & 0xff) as i16; let mantissa = if exponent == 0 { + // Zeros and subnormals (bits & 0x7fffff) << 1 } else { + // Normals, infinities, and NaN (bits & 0x7fffff) | 0x800000 }; // Exponent bias + mantissa shift @@ -2069,8 +2071,10 @@ fn integer_decode_f64(f: f64) -> (u64, i16, i8) { let sign: i8 = if bits >> 63 == 0 { 1 } else { -1 }; let mut exponent: i16 = ((bits >> 52) & 0x7ff) as i16; let mantissa = if exponent == 0 { + // Zeros and subnormals (bits & 0xfffffffffffff) << 1 } else { + // Normals, infinities, and NaN (bits & 0xfffffffffffff) | 0x10000000000000 }; // Exponent bias + mantissa shift