add is_zero method

This commit is contained in:
Trevor Spiteri 2021-04-23 00:06:23 +02:00
parent 2866af5915
commit cb0f7c7904
6 changed files with 69 additions and 0 deletions

View File

@ -92,6 +92,10 @@ The conversions supported cover the following cases.
### Version 1.9.0 news (unreleased)
* The following method was added to all fixed-point numbers, to the
[`Fixed`][tf-1-9] trait, and to the [`Wrapping`][w-1-9] and
[`Unwrapped`][u-1-9] wrappers:
* [`is_zero`][f-iz-1-9]
* The following traits from the [*bytemuck* crate] were implemented for all
fixed-point numbers, added as supertraits to the [`Fixed`][tf-1-9] trait,
and implemented for the [`Wrapping`][w-1-9] and [`Unwrapped`][u-1-9]
@ -114,6 +118,7 @@ The conversions supported cover the following cases.
[bm-p-1]: https://docs.rs/bytemuck/^1/bytemuck/trait.Pod.html
[bm-tw-1]: https://docs.rs/bytemuck/^1/bytemuck/trait.TransparentWrapper.html
[bm-z-1]: https://docs.rs/bytemuck/^1/bytemuck/trait.Zeroable.html
[f-iz-1-9]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.is_zero
[fof-1-9]: https://tspiteri.gitlab.io/fixed/dev/fixed/traits/trait.FixedOptionalFeatures.html
[leu128-1-9]: https://tspiteri.gitlab.io/fixed/dev/fixed/types/extra/trait.LeEqU128.html
[leu16-1-9]: https://tspiteri.gitlab.io/fixed/dev/fixed/types/extra/trait.LeEqU16.html

View File

@ -8,6 +8,10 @@ as-is, without any warranty. -->
Version 1.9.0 (unreleased)
==========================
* The following method was added to all fixed-point numbers, to the
[`Fixed`][tf-1-9] trait, and to the [`Wrapping`][w-1-9] and
[`Unwrapped`][u-1-9] wrappers:
* [`is_zero`][f-iz-1-9]
* The following traits from the [*bytemuck* crate] were implemented for all
fixed-point numbers, added as supertraits to the [`Fixed`][tf-1-9] trait,
and implemented for the [`Wrapping`][w-1-9] and [`Unwrapped`][u-1-9]
@ -31,6 +35,7 @@ Compatibility notes
[bm-p-1]: https://docs.rs/bytemuck/^1/bytemuck/trait.Pod.html
[bm-tw-1]: https://docs.rs/bytemuck/^1/bytemuck/trait.TransparentWrapper.html
[bm-z-1]: https://docs.rs/bytemuck/^1/bytemuck/trait.Zeroable.html
[f-iz-1-9]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.is_zero
[fof-1-9]: https://tspiteri.gitlab.io/fixed/dev/fixed/traits/trait.FixedOptionalFeatures.html
[leu128-1-9]: https://tspiteri.gitlab.io/fixed/dev/fixed/types/extra/trait.LeEqU128.html
[leu16-1-9]: https://tspiteri.gitlab.io/fixed/dev/fixed/types/extra/trait.LeEqU16.html

View File

@ -640,6 +640,24 @@ assert_eq!(Fix::from_bits(bits).rotate_right(3), Fix::from_bits(rot));
}
}
comment! {
"Returns [`true`] if the number is zero.
# Examples
```rust
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
assert!(Fix::ZERO.is_zero());
assert!(!Fix::from_num(5).is_zero());
```
";
#[inline]
pub const fn is_zero(self) -> bool {
self.to_bits() == 0
}
}
if_signed! {
$Signedness;
comment! {

View File

@ -1105,6 +1105,12 @@ where
#[must_use = "this returns the result of the operation, without modifying the original"]
fn rotate_right(self, n: u32) -> Self;
/// Returns [`true`] if the number is zero.
///
/// See also <code>FixedI32::[is\_zero][FixedI32::is_zero]</code> and
/// <code>FixedU32::[is\_zero][FixedU32::is_zero]</code>.
fn is_zero(self) -> bool;
/// Returns the mean of `self` and `other`.
///
/// See also <code>FixedI32::[mean][FixedI32::mean]</code> and
@ -3200,6 +3206,7 @@ macro_rules! impl_fixed {
trait_delegate! { fn reverse_bits(self) -> Self }
trait_delegate! { fn rotate_left(self, n: u32) -> Self }
trait_delegate! { fn rotate_right(self, n: u32) -> Self }
trait_delegate! { fn is_zero(self) -> bool }
trait_delegate! { fn mean(self, other: Self) -> Self }
trait_delegate! { fn recip(self) -> Self }
trait_delegate! { fn mul_add(self, mul: Self, add: Self) -> Self }

View File

@ -978,6 +978,23 @@ impl<F: Fixed> Unwrapped<F> {
Unwrapped(self.0.rotate_right(n))
}
/// Returns [`true`] if the number is zero.
///
/// See also <code>FixedI32::[is\_zero][FixedI32::is_zero]</code> and
/// <code>FixedU32::[is\_zero][FixedU32::is_zero]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Unwrapped};
/// assert!(Unwrapped(I16F16::ZERO).is_zero());
/// assert!(!Unwrapped(I16F16::from_num(4.3)).is_zero());
/// ```
#[inline]
pub fn is_zero(self) -> bool {
self.0.is_zero()
}
/// Returns the mean of `self` and `other`.
///
/// See also <code>FixedI32::[mean][FixedI32::mean]</code> and

View File

@ -926,6 +926,23 @@ impl<F: Fixed> Wrapping<F> {
Wrapping(self.0.rotate_right(n))
}
/// Returns [`true`] if the number is zero.
///
/// See also <code>FixedI32::[is\_zero][FixedI32::is_zero]</code> and
/// <code>FixedU32::[is\_zero][FixedU32::is_zero]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// assert!(Wrapping(I16F16::ZERO).is_zero());
/// assert!(!Wrapping(I16F16::from_num(4.3)).is_zero());
/// ```
#[inline]
pub fn is_zero(self) -> bool {
self.0.is_zero()
}
/// Returns the mean of `self` and `other`.
///
/// See also <code>FixedI32::[mean][FixedI32::mean]</code> and