From 3794dec03d4a45623594b5ee42584d45559768ae Mon Sep 17 00:00:00 2001 From: Trevor Spiteri Date: Fri, 2 Apr 2021 21:02:31 +0200 Subject: [PATCH] add ZERO, DELTA, ONE constants --- README.md | 11 +++++++++++ RELEASES.md | 11 +++++++++++ src/macros_const.rs | 15 +++++++++++++++ src/macros_no_frac.rs | 30 ++++++++++++++++++++++++++++++ src/traits.rs | 8 ++++++++ src/unwrapped.rs | 20 ++++++++++++++++++++ src/wrapping.rs | 20 ++++++++++++++++++++ 7 files changed, 115 insertions(+) diff --git a/README.md b/README.md index 0e85e77..3eb0078 100644 --- a/README.md +++ b/README.md @@ -81,6 +81,12 @@ The conversions supported cover the following cases. ### Version 1.8.0 news (unreleased) + * The following constants were added to all fixed-point numbers, to + the [`Fixed`][tf-1-8] trait, and to the [`Wrapping`][w-1-8] and + [`Unwrapped`][u-1-8] wrappers: + * [`ZERO`][f-z-1-8], [`DELTA`][f-d-1-8] + * The [`ONE`][f-o-1-8] constant was added to all fixed-point numbers + that can represent the value 1. * The following methods were added to all fixed-point numbers and to the [`Fixed`][tf-1-8] trait: * [`unwrapped_rem`][f-ur-1-8], @@ -88,10 +94,15 @@ The conversions supported cover the following cases. * [`unwrapped_rem_int`][f-uri-1-8] * Many methods were marked with the `must_use` attribute. +[f-d-1-8]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#associatedconstant.DELTA +[f-o-1-8]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#associatedconstant.ONE [f-ur-1-8]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.unwrapped_rem [f-ure-1-8]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.unwrapped_rem_euclid [f-uri-1-8]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.unwrapped_rem_int +[f-z-1-8]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#associatedconstant.ZERO [tf-1-8]: https://tspiteri.gitlab.io/fixed/dev/fixed/traits/trait.Fixed.html +[u-1-8]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.Unwrapped.html +[w-1-8]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.Wrapping.html ### Version 1.7.0 news (2021-03-25) diff --git a/RELEASES.md b/RELEASES.md index 61f8ecf..7fa9ee9 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -8,6 +8,12 @@ as-is, without any warranty. --> Version 1.8.0 (unreleased) ========================== + * The following constants were added to all fixed-point numbers, to + the [`Fixed`][tf-1-8] trait, and to the [`Wrapping`][w-1-8] and + [`Unwrapped`][u-1-8] wrappers: + * [`ZERO`][f-z-1-8], [`DELTA`][f-d-1-8] + * The [`ONE`][f-o-1-8] constant was added to all fixed-point numbers + that can represent the value 1. * The following methods were added to all fixed-point numbers and to the [`Fixed`][tf-1-8] trait: * [`unwrapped_rem`][f-ur-1-8], @@ -15,10 +21,15 @@ Version 1.8.0 (unreleased) * [`unwrapped_rem_int`][f-uri-1-8] * Many methods were marked with the `must_use` attribute. +[f-d-1-8]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#associatedconstant.DELTA +[f-o-1-8]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#associatedconstant.ONE [f-ur-1-8]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.unwrapped_rem [f-ure-1-8]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.unwrapped_rem_euclid [f-uri-1-8]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.unwrapped_rem_int +[f-z-1-8]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#associatedconstant.ZERO [tf-1-8]: https://tspiteri.gitlab.io/fixed/dev/fixed/traits/trait.Fixed.html +[u-1-8]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.Unwrapped.html +[w-1-8]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.Wrapping.html Version 1.7.0 (2021-03-25) ========================== diff --git a/src/macros_const.rs b/src/macros_const.rs index 9faf89b..ae2ea59 100644 --- a/src/macros_const.rs +++ b/src/macros_const.rs @@ -193,6 +193,21 @@ let _ = Fix::LOG2_E; where Frac: IsLessOrEqual<$LeEqU_C1, Output = True>, { + comment! { + "One. + +# Examples + +```rust +use fixed::{types::extra::U4, ", $s_fixed, "}; +type Fix = ", $s_fixed, "; +assert_eq!(Fix::ONE, Fix::from_num(1)); +``` +"; + pub const ONE: $Fixed = + $Fixed::from_bits($Fixed::::DELTA.to_bits() << Frac::U32); + } + /// τ/4 = 1.57079… pub const FRAC_TAU_4: $Fixed = shift!(FRAC_TAU_4, 127, $Fixed); diff --git a/src/macros_no_frac.rs b/src/macros_no_frac.rs index c5d2b70..409ee2a 100644 --- a/src/macros_no_frac.rs +++ b/src/macros_no_frac.rs @@ -51,6 +51,36 @@ assert_eq!(Fix::MAX, Fix::from_bits(", $s_inner, "::MAX)); pub const MAX: $Fixed = Self::from_bits(<$Inner>::MAX); } + comment! { + "Zero. + +# Examples + +```rust +use fixed::{types::extra::U4, ", $s_fixed, "}; +type Fix = ", $s_fixed, "; +assert_eq!(Fix::ZERO, Fix::from_bits(0)); +``` +"; + pub const ZERO: $Fixed = Self::from_bits(0); + } + + comment! { + "The smallest positive value that can be represented. + +# Examples + +```rust +use fixed::{types::extra::U4, ", $s_fixed, "}; +type Fix = ", $s_fixed, "; +assert_eq!(Fix::DELTA, Fix::from_bits(1)); +// binary 0.0001 is decimal 0.0625 +assert_eq!(Fix::DELTA, 0.0625); +``` +"; + pub const DELTA: $Fixed = Self::from_bits(1); + } + comment! { if_signed_unsigned!($Signedness, "[`true`]", "[`false`]"), "[`bool`] because the [`", $s_fixed, "`] type is ", diff --git a/src/traits.rs b/src/traits.rs index c4212ed..1f5a367 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -307,6 +307,12 @@ where /// The largest value that can be represented. const MAX: Self; + /// Zero. + const ZERO: Self; + + /// The smallest positive value that can be represented. + const DELTA: Self; + /// [`true`] if the type is signed. const IS_SIGNED: bool; @@ -2235,6 +2241,8 @@ macro_rules! impl_fixed { type Frac = Frac; const MIN: Self = Self::MIN; const MAX: Self = Self::MAX; + const ZERO: Self = Self::ZERO; + const DELTA: Self = Self::DELTA; const IS_SIGNED: bool = Self::IS_SIGNED; const INT_NBITS: u32 = Self::INT_NBITS; const FRAC_NBITS: u32 = Self::FRAC_NBITS; diff --git a/src/unwrapped.rs b/src/unwrapped.rs index eb0831f..beae1fe 100644 --- a/src/unwrapped.rs +++ b/src/unwrapped.rs @@ -72,6 +72,26 @@ impl Unwrapped { /// ``` pub const MAX: Unwrapped = Unwrapped(F::MAX); + /// Zero. + /// + /// # Examples + /// + /// ```rust + /// use fixed::{types::I16F16, Unwrapped}; + /// assert_eq!(Unwrapped::::ZERO, Unwrapped(I16F16::ZERO)); + /// ``` + pub const ZERO: Unwrapped = Unwrapped(F::ZERO); + + /// The smallest positive value that can be represented. + /// + /// # Examples + /// + /// ```rust + /// use fixed::{types::I16F16, Unwrapped}; + /// assert_eq!(Unwrapped::::DELTA, Unwrapped(I16F16::DELTA)); + /// ``` + pub const DELTA: Unwrapped = Unwrapped(F::DELTA); + /// [`true`] if the type is signed. /// /// # Examples diff --git a/src/wrapping.rs b/src/wrapping.rs index bd95b0b..e63afed 100644 --- a/src/wrapping.rs +++ b/src/wrapping.rs @@ -69,6 +69,26 @@ impl Wrapping { /// ``` pub const MAX: Wrapping = Wrapping(F::MAX); + /// Zero. + /// + /// # Examples + /// + /// ```rust + /// use fixed::{types::I16F16, Wrapping}; + /// assert_eq!(Wrapping::::ZERO, Wrapping(I16F16::ZERO)); + /// ``` + pub const ZERO: Wrapping = Wrapping(F::ZERO); + + /// The smallest positive value that can be represented. + /// + /// # Examples + /// + /// ```rust + /// use fixed::{types::I16F16, Wrapping}; + /// assert_eq!(Wrapping::::DELTA, Wrapping(I16F16::DELTA)); + /// ``` + pub const DELTA: Wrapping = Wrapping(F::DELTA); + /// [`true`] if the type is signed. /// /// # Examples