reorder some methods

This commit is contained in:
Trevor Spiteri 2019-08-22 14:03:32 +02:00
parent 9e333d99c2
commit 87d30cd8b1
3 changed files with 192 additions and 185 deletions

View File

@ -190,6 +190,74 @@ assert_eq!(Fix::from_bits(bits).rotate_right(3), Fix::from_bits(rot));
$Fixed => const fn rotate_right(self, n: u32)
}
if_signed! {
$Signedness;
delegate! {
"Returns [`true`][`bool`] if the number is > 0.
# Examples
```rust
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
assert!(Fix::from_num(5).is_positive());
assert!(!Fix::from_num(0).is_positive());
assert!(!Fix::from_num(-5).is_positive());
```
[`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html
";
$Fixed => const fn is_positive(self) -> bool
}
delegate! {
"Returns [`true`][`bool`] if the number is < 0.
# Examples
```rust
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
assert!(!Fix::from_num(5).is_negative());
assert!(!Fix::from_num(0).is_negative());
assert!(Fix::from_num(-5).is_negative());
```
[`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html
";
$Fixed => const fn is_negative(self) -> bool
}
}
if_unsigned! {
$Signedness;
comment! {
"Returns [`true`][`bool`] if the fixed-point number is
2<sup><i>k</i></sup> for some integer <i>k</i>.
# Examples
```rust
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
// 3/8 is 0.0110
let three_eights = Fix::from_bits(0b0110);
// 1/2 is 0.1000
let half = Fix::from_bits(0b1000);
assert!(!three_eights.is_power_of_two());
assert!(half.is_power_of_two());
```
[`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html
";
#[inline]
pub const fn is_power_of_two(self) -> bool {
(self.to_bits().wrapping_sub(1) & self.to_bits() == 0)
& (self.to_bits() != 0)
}
}
}
if_signed! {
$Signedness;
comment! {
@ -218,6 +286,35 @@ assert_eq!(minus_five.abs(), five);
}
}
if_unsigned! {
$Signedness;
delegate! {
"Returns the smallest power of two ≥ `self`.
# Panics
When debug assertions are enabled, panics if the next power of two is
too large to represent. When debug assertions are not enabled, zero
can be returned, but it is not considered a breaking change if in the
future it panics.
# Examples
```rust
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
// 3/8 is 0.0110
let three_eights = Fix::from_bits(0b0110);
// 1/2 is 0.1000
let half = Fix::from_bits(0b1000);
assert_eq!(three_eights.next_power_of_two(), half);
assert_eq!(half.next_power_of_two(), half);
```
";
$Fixed => fn next_power_of_two(self)
}
}
comment! {
"Checked negation. Returns the negated value, or [`None`] on overflow.
@ -450,6 +547,34 @@ assert_eq!(Fix::min_value().checked_abs(), None);
}
}
if_unsigned! {
$Signedness;
comment! {
"Returns the smallest power of two ≥ `self`, or
[`None`] if the next power of two is too large to represent.
# Examples
```rust
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
// 3/8 is 0.0110
let three_eights = Fix::from_bits(0b0110);
// 1/2 is 0.1000
let half = Fix::from_bits(0b1000);
assert_eq!(three_eights.checked_next_power_of_two(), Some(half));
assert!(Fix::max_value().checked_next_power_of_two().is_none());
```
[`None`]: https://doc.rust-lang.org/nightly/std/option/enum.Option.html#variant.None
";
#[inline]
pub fn checked_next_power_of_two(self) -> Option<$Fixed<Frac>> {
self.to_bits().checked_next_power_of_two().map(Self::from_bits)
}
}
}
comment! {
"Saturating negation. Returns the negated value, saturating on overflow.
@ -1107,125 +1232,6 @@ assert_eq!(Fix::min_value().overflowing_abs(), (Fix::min_value(), true));
}
}
}
if_unsigned! {
$Signedness;
comment! {
"Returns [`true`][`bool`] if the fixed-point number is
2<sup><i>k</i></sup> for some integer <i>k</i>.
# Examples
```rust
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
// 3/8 is 0.0110
let three_eights = Fix::from_bits(0b0110);
// 1/2 is 0.1000
let half = Fix::from_bits(0b1000);
assert!(!three_eights.is_power_of_two());
assert!(half.is_power_of_two());
```
[`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html
";
#[inline]
pub const fn is_power_of_two(self) -> bool {
(self.to_bits().wrapping_sub(1) & self.to_bits() == 0)
& (self.to_bits() != 0)
}
}
delegate! {
"Returns the smallest power of two ≥ `self`.
# Panics
When debug assertions are enabled, panics if the next power of two is
too large to represent. When debug assertions are not enabled, zero
can be returned, but it is not considered a breaking change if in the
future it panics.
# Examples
```rust
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
// 3/8 is 0.0110
let three_eights = Fix::from_bits(0b0110);
// 1/2 is 0.1000
let half = Fix::from_bits(0b1000);
assert_eq!(three_eights.next_power_of_two(), half);
assert_eq!(half.next_power_of_two(), half);
```
";
$Fixed => fn next_power_of_two(self)
}
comment! {
"Returns the smallest power of two ≥ `self`, or
[`None`] if the next power of two is too large to represent.
# Examples
```rust
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
// 3/8 is 0.0110
let three_eights = Fix::from_bits(0b0110);
// 1/2 is 0.1000
let half = Fix::from_bits(0b1000);
assert_eq!(three_eights.checked_next_power_of_two(), Some(half));
assert!(Fix::max_value().checked_next_power_of_two().is_none());
```
[`None`]: https://doc.rust-lang.org/nightly/std/option/enum.Option.html#variant.None
";
#[inline]
pub fn checked_next_power_of_two(self) -> Option<$Fixed<Frac>> {
self.to_bits().checked_next_power_of_two().map(Self::from_bits)
}
}
}
if_signed! {
$Signedness;
delegate! {
"Returns [`true`][`bool`] if the number is > 0.
# Examples
```rust
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
assert!(Fix::from_num(5).is_positive());
assert!(!Fix::from_num(0).is_positive());
assert!(!Fix::from_num(-5).is_positive());
```
[`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html
";
$Fixed => const fn is_positive(self) -> bool
}
delegate! {
"Returns [`true`][`bool`] if the number is < 0.
# Examples
```rust
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
assert!(!Fix::from_num(5).is_negative());
assert!(!Fix::from_num(0).is_negative());
assert!(Fix::from_num(-5).is_negative());
```
[`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html
";
$Fixed => const fn is_negative(self) -> bool
}
}
}
};
}

View File

@ -105,6 +105,56 @@ assert_eq!((-two_and_quarter).frac(), three_quarters);
}
}
comment! {
"Rounds to the next integer towards 0.
",
if_unsigned_else_empty_str! {
$Signedness,
"Note that for unsigned numbers, this is equivalent to [`floor`].
",
},
"# Examples
```rust
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
assert_eq!(Fix::from_num(2.1).round_to_zero(), Fix::from_num(2));
assert_eq!(Fix::from_num(2.9).round_to_zero(), Fix::from_num(2));
",
if_signed_else_empty_str! {
$Signedness,
"assert_eq!(Fix::from_num(-2.1).round_to_zero(), Fix::from_num(-2));
assert_eq!(Fix::from_num(-2.9).round_to_zero(), Fix::from_num(-2));
",
},
"```
",
if_unsigned_else_empty_str! {
$Signedness,
"
[`floor`]: #method.floor
"
};
#[inline]
pub fn round_to_zero(self) -> $Fixed<Frac> {
if_signed! {
$Signedness;
if self.is_negative() {
let int = self.int();
let increment = Self::from_bits(Self::INT_LSB);
if Self::INT_NBITS == 1 {
// increment is -1, so subtract it
return int - increment;
}
return int + increment;
}
}
self.int()
}
}
comment! {
"Rounds to the next integer towards +∞.
@ -182,56 +232,6 @@ assert_eq!(Fix::from_num(2.5).floor(), Fix::from_num(2));
}
}
comment! {
"Rounds to the next integer towards 0.
",
if_unsigned_else_empty_str! {
$Signedness,
"Note that for unsigned numbers, this is equivalent to [`floor`].
",
},
"# Examples
```rust
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
assert_eq!(Fix::from_num(2.1).round_to_zero(), Fix::from_num(2));
assert_eq!(Fix::from_num(2.9).round_to_zero(), Fix::from_num(2));
",
if_signed_else_empty_str! {
$Signedness,
"assert_eq!(Fix::from_num(-2.1).round_to_zero(), Fix::from_num(-2));
assert_eq!(Fix::from_num(-2.9).round_to_zero(), Fix::from_num(-2));
",
},
"```
",
if_unsigned_else_empty_str! {
$Signedness,
"
[`floor`]: #method.floor
"
};
#[inline]
pub fn round_to_zero(self) -> $Fixed<Frac> {
if_signed! {
$Signedness;
if self.is_negative() {
let int = self.int();
let increment = Self::from_bits(Self::INT_LSB);
if Self::INT_NBITS == 1 {
// increment is -1, so subtract it
return int - increment;
}
return int + increment;
}
}
self.int()
}
}
comment! {
"Rounds to the nearest integer, with ties rounded away
from zero.

View File

@ -204,7 +204,7 @@ pub trait Fixed
where
Self: Copy + Default + Hash + Ord,
Self: Debug + Display + Binary + Octal + LowerHex + UpperHex + FromStr,
Self: FromFixed + ToFixed + FixedOptionalFeatures,
Self: FromFixed + ToFixed,
Self: Add<Output = Self> + AddAssign + Sub<Output = Self> + SubAssign,
Self: Mul<Output = Self> + MulAssign + Div<Output = Self> + DivAssign,
Self: Mul<<Self as Fixed>::Bits, Output = Self> + MulAssign<<Self as Fixed>::Bits>,
@ -218,6 +218,7 @@ where
Self: PartialOrd<u8> + PartialOrd<u16> + PartialOrd<u32>,
Self: PartialOrd<u64> + PartialOrd<u128> + PartialOrd<usize>,
Self: PartialOrd<f32> + PartialOrd<f64>,
Self: FixedOptionalFeatures,
Self: Sealed,
{
/// The primitive integer underlying type.
@ -412,15 +413,15 @@ where
/// Returns the fractional part.
fn frac(self) -> Self;
/// Rounds to the next integer towards 0.
fn round_to_zero(self) -> Self;
/// Rounds to the next integer towards +∞.
fn ceil(self) -> Self;
/// Rounds to the next integer towards −∞.
fn floor(self) -> Self;
/// Rounds to the next integer towards 0.
fn round_to_zero(self) -> Self;
/// Rounds to the nearest integer, with ties rounded away from zero.
fn round(self) -> Self;
@ -930,6 +931,18 @@ where
/// This trait provides methods common to all signed fixed-point numbers.
pub trait FixedSigned: Fixed + Neg<Output = Self> {
/// Returns [`true`][`bool`] if the number is > 0.
///
/// [`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html
/// [tuple]: https://doc.rust-lang.org/nightly/std/primitive.tuple.html
fn is_positive(self) -> bool;
/// Returns [`true`][`bool`] if the number is < 0.
///
/// [`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html
/// [tuple]: https://doc.rust-lang.org/nightly/std/primitive.tuple.html
fn is_negative(self) -> bool;
/// Returns the absolute value.
fn abs(self) -> Self;
@ -976,18 +989,6 @@ pub trait FixedSigned: Fixed + Neg<Output = Self> {
/// [`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html
/// [tuple]: https://doc.rust-lang.org/nightly/std/primitive.tuple.html
fn overflowing_abs(self) -> (Self, bool);
/// Returns [`true`][`bool`] if the number is > 0.
///
/// [`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html
/// [tuple]: https://doc.rust-lang.org/nightly/std/primitive.tuple.html
fn is_positive(self) -> bool;
/// Returns [`true`][`bool`] if the number is < 0.
///
/// [`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html
/// [tuple]: https://doc.rust-lang.org/nightly/std/primitive.tuple.html
fn is_negative(self) -> bool;
}
/// This trait provides methods common to all unsigned fixed-point numbers.