From c477dfe306ba87a8c03f735b6d537271d9562477 Mon Sep 17 00:00:00 2001 From: Mathilda Date: Wed, 26 Jun 2024 15:37:56 -0400 Subject: [PATCH 1/3] Fix bug in example for trait method `Float::integer_decode` --- src/float.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/float.rs b/src/float.rs index d0d21a5..dd4946f 100644 --- a/src/float.rs +++ b/src/float.rs @@ -1872,10 +1872,10 @@ pub trait Float: Num + Copy + NumCast + PartialOrd + Neg { /// let (mantissa, exponent, sign) = Float::integer_decode(num); /// let sign_f = sign as f32; /// let mantissa_f = mantissa as f32; - /// let exponent_f = num.powf(exponent as f32); + /// let exponent_f = exponent as f32; /// /// // 1 * 8388608 * 2^(-22) == 2 - /// let abs_difference = (sign_f * mantissa_f * exponent_f - num).abs(); + /// let abs_difference = (sign_f * mantissa_f * 2_f32.powf(exponent_f) - num).abs(); /// /// assert!(abs_difference < 1e-10); /// ``` From d3935bf68893f34afd6b389a5e6671496370f4b1 Mon Sep 17 00:00:00 2001 From: Mathilda Date: Wed, 26 Jun 2024 15:40:47 -0400 Subject: [PATCH 2/3] Change value of `num` in example for trait method `Float::integer_decode` --- src/float.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/float.rs b/src/float.rs index dd4946f..a0383c0 100644 --- a/src/float.rs +++ b/src/float.rs @@ -1866,15 +1866,15 @@ pub trait Float: Num + Copy + NumCast + PartialOrd + Neg { /// ``` /// use num_traits::Float; /// - /// let num = 2.0f32; + /// let num = 42_f32; /// - /// // (8388608, -22, 1) + /// // (11010048, -18, 1) /// let (mantissa, exponent, sign) = Float::integer_decode(num); /// let sign_f = sign as f32; /// let mantissa_f = mantissa as f32; /// let exponent_f = exponent as f32; /// - /// // 1 * 8388608 * 2^(-22) == 2 + /// // 1 * 11010048 * 2^(-18) == 42 /// let abs_difference = (sign_f * mantissa_f * 2_f32.powf(exponent_f) - num).abs(); /// /// assert!(abs_difference < 1e-10); From e8c766f3abf628ada7e8c1d88d530c5465d10339 Mon Sep 17 00:00:00 2001 From: Tilda <63630918+mtilda@users.noreply.github.com> Date: Thu, 27 Jun 2024 11:09:13 -0400 Subject: [PATCH 3/3] Refactor `2_f32.powf(exponent)` as `exponent_f.exp2()` Co-authored-by: Josh Stone --- src/float.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/float.rs b/src/float.rs index a0383c0..4124e92 100644 --- a/src/float.rs +++ b/src/float.rs @@ -1875,7 +1875,7 @@ pub trait Float: Num + Copy + NumCast + PartialOrd + Neg { /// let exponent_f = exponent as f32; /// /// // 1 * 11010048 * 2^(-18) == 42 - /// let abs_difference = (sign_f * mantissa_f * 2_f32.powf(exponent_f) - num).abs(); + /// let abs_difference = (sign_f * mantissa_f * exponent_f.exp2() - num).abs(); /// /// assert!(abs_difference < 1e-10); /// ```