From d5bb6056f16bae301b498bd44ad286685bb5e618 Mon Sep 17 00:00:00 2001 From: bmoxb <maxoblack@yahoo.com> Date: Thu, 9 Mar 2023 18:55:28 +0000 Subject: [PATCH 1/4] Document the resulting values produced when using `From<bool>` on floats --- library/core/src/convert/num.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/library/core/src/convert/num.rs b/library/core/src/convert/num.rs index 4da7c323492..8c407d07c51 100644 --- a/library/core/src/convert/num.rs +++ b/library/core/src/convert/num.rs @@ -172,7 +172,8 @@ impl_from! { f32, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0" #[stable(feature = "float_from_bool", since = "1.68.0")] #[rustc_const_unstable(feature = "const_num_from_num", issue = "87852")] impl const From<bool> for f32 { - /// Converts `bool` to `f32` losslessly. + /// Converts `bool` to `f32` losslessly. The resulting value is `0.0` for + /// `false` and `1.0` for `true` values. #[inline] fn from(small: bool) -> Self { small as u8 as Self From 73016bb8d4a5f20dd07524854d29ff9177ccbe54 Mon Sep 17 00:00:00 2001 From: bmoxb <maxoblack@yahoo.com> Date: Thu, 9 Mar 2023 20:36:26 +0000 Subject: [PATCH 2/4] Indicate that `0.0` refers to positive `0.0` --- library/core/src/convert/num.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/core/src/convert/num.rs b/library/core/src/convert/num.rs index 8c407d07c51..205fe3d382e 100644 --- a/library/core/src/convert/num.rs +++ b/library/core/src/convert/num.rs @@ -172,8 +172,8 @@ impl_from! { f32, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0" #[stable(feature = "float_from_bool", since = "1.68.0")] #[rustc_const_unstable(feature = "const_num_from_num", issue = "87852")] impl const From<bool> for f32 { - /// Converts `bool` to `f32` losslessly. The resulting value is `0.0` for - /// `false` and `1.0` for `true` values. + /// Converts `bool` to `f32` losslessly. The resulting value is positive + /// `0.0` for `false` and `1.0` for `true` values. #[inline] fn from(small: bool) -> Self { small as u8 as Self From 8d2bdb89c624d58feb0f077de6648787f8e6afbc Mon Sep 17 00:00:00 2001 From: bmoxb <maxoblack@yahoo.com> Date: Thu, 9 Mar 2023 20:37:04 +0000 Subject: [PATCH 3/4] Add missing comment for f64 --- library/core/src/convert/num.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/library/core/src/convert/num.rs b/library/core/src/convert/num.rs index 205fe3d382e..baee008b7ed 100644 --- a/library/core/src/convert/num.rs +++ b/library/core/src/convert/num.rs @@ -182,7 +182,8 @@ impl const From<bool> for f32 { #[stable(feature = "float_from_bool", since = "1.68.0")] #[rustc_const_unstable(feature = "const_num_from_num", issue = "87852")] impl const From<bool> for f64 { - /// Converts `bool` to `f64` losslessly. + /// Converts `bool` to `f64` losslessly. The resulting value is positive + /// `0.0` for `false` and `1.0` for `true` values. #[inline] fn from(small: bool) -> Self { small as u8 as Self From b439189236a8956eaf9cd37bd601f3df074716f2 Mon Sep 17 00:00:00 2001 From: bmoxb <maxoblack@yahoo.com> Date: Thu, 9 Mar 2023 20:44:11 +0000 Subject: [PATCH 4/4] Add examples section which demonstrates the behaviour (specifically the sign positive aspect) --- library/core/src/convert/num.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/library/core/src/convert/num.rs b/library/core/src/convert/num.rs index baee008b7ed..a74a56bc5b2 100644 --- a/library/core/src/convert/num.rs +++ b/library/core/src/convert/num.rs @@ -174,6 +174,16 @@ impl_from! { f32, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0" impl const From<bool> for f32 { /// Converts `bool` to `f32` losslessly. The resulting value is positive /// `0.0` for `false` and `1.0` for `true` values. + /// + /// # Examples + /// ``` + /// let x: f32 = false.into(); + /// assert_eq!(x, 0.0); + /// assert!(x.is_sign_positive()); + /// + /// let y: f32 = true.into(); + /// assert_eq!(y, 1.0); + /// ``` #[inline] fn from(small: bool) -> Self { small as u8 as Self @@ -184,6 +194,16 @@ impl const From<bool> for f32 { impl const From<bool> for f64 { /// Converts `bool` to `f64` losslessly. The resulting value is positive /// `0.0` for `false` and `1.0` for `true` values. + /// + /// # Examples + /// ``` + /// let x: f64 = false.into(); + /// assert_eq!(x, 0.0); + /// assert!(x.is_sign_positive()); + /// + /// let y: f64 = true.into(); + /// assert_eq!(y, 1.0); + /// ``` #[inline] fn from(small: bool) -> Self { small as u8 as Self