fix remaining checked {div,rem}_euclid_int methods
Closes https://gitlab.com/tspiteri/fixed/issues/13
This commit is contained in:
parent
97128d2b9f
commit
3ee5811556
|
@ -142,7 +142,13 @@ assert_eq!(Fix::from_num(-5).signum(), -1);
|
||||||
|
|
||||||
# Panics
|
# Panics
|
||||||
|
|
||||||
Panics if the divisor is zero or if the division results in overflow.
|
Panics if the divisor is zero.
|
||||||
|
|
||||||
|
When debug assertions are enabled, this method also panics if the
|
||||||
|
division overflows. When debug assertions are not enabled, the wrapped
|
||||||
|
value can be returned, but it is not considered a breaking change if
|
||||||
|
in the future it panics; if wrapping is required use
|
||||||
|
[`wrapping_div_euclid`] instead.
|
||||||
|
|
||||||
# Examples
|
# Examples
|
||||||
|
|
||||||
|
@ -157,6 +163,8 @@ assert_eq!(Fix::from_num(7.5).div_euclid(Fix::from_num(2)), Fix::from_num(3));
|
||||||
",
|
",
|
||||||
},
|
},
|
||||||
"```
|
"```
|
||||||
|
|
||||||
|
[`wrapping_div_euclid`]: #method.wrapping_div_euclid
|
||||||
";
|
";
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn div_euclid(self, rhs: $Fixed<Frac>) -> $Fixed<Frac> {
|
pub fn div_euclid(self, rhs: $Fixed<Frac>) -> $Fixed<Frac> {
|
||||||
|
@ -175,6 +183,86 @@ assert_eq!(Fix::from_num(7.5).div_euclid(Fix::from_num(2)), Fix::from_num(3));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
comment! {
|
||||||
|
"Euclidean division by an integer.
|
||||||
|
|
||||||
|
# Panics
|
||||||
|
|
||||||
|
Panics if the divisor is zero.
|
||||||
|
|
||||||
|
When debug assertions are enabled, this method also panics if the
|
||||||
|
division overflows. When debug assertions are not enabled, the wrapped
|
||||||
|
value can be returned, but it is not considered a breaking change if
|
||||||
|
in the future it panics; if wrapping is required use
|
||||||
|
[`wrapping_div_euclid_int`] instead.
|
||||||
|
|
||||||
|
|
||||||
|
# Examples
|
||||||
|
|
||||||
|
```rust
|
||||||
|
use fixed::{types::extra::U4, ", $s_fixed, "};
|
||||||
|
type Fix = ", $s_fixed, "<U4>;
|
||||||
|
assert_eq!(Fix::from_num(7.5).div_euclid_int(2), Fix::from_num(3));
|
||||||
|
",
|
||||||
|
if_signed_else_empty_str! {
|
||||||
|
$Signedness,
|
||||||
|
"assert_eq!(Fix::from_num(-7.5).div_euclid_int(2), Fix::from_num(-4));
|
||||||
|
",
|
||||||
|
},
|
||||||
|
"```
|
||||||
|
|
||||||
|
[`wrapping_div_euclid_int`]: #method.wrapping_div_euclid_int
|
||||||
|
";
|
||||||
|
#[inline]
|
||||||
|
pub fn div_euclid_int(self, rhs: $Inner) -> $Fixed<Frac> {
|
||||||
|
let q = (self / rhs).round_to_zero();
|
||||||
|
if_signed! {
|
||||||
|
$Signedness;
|
||||||
|
if (self % rhs).is_negative() {
|
||||||
|
return if rhs.is_positive() {
|
||||||
|
q - Self::from_num(1)
|
||||||
|
} else {
|
||||||
|
q + Self::from_num(1)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
q
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
comment! {
|
||||||
|
"Remainder for Euclidean division by an integer.
|
||||||
|
|
||||||
|
# Panics
|
||||||
|
|
||||||
|
Panics if the divisor is zero.
|
||||||
|
|
||||||
|
# Examples
|
||||||
|
|
||||||
|
```rust
|
||||||
|
use fixed::{types::extra::U4, ", $s_fixed, "};
|
||||||
|
type Fix = ", $s_fixed, "<U4>;
|
||||||
|
assert_eq!(Fix::from_num(7.5).rem_euclid_int(2), Fix::from_num(1.5));
|
||||||
|
",
|
||||||
|
if_signed_else_empty_str! {
|
||||||
|
$Signedness,
|
||||||
|
"assert_eq!(Fix::from_num(-7.5).rem_euclid_int(2), Fix::from_num(0.5));
|
||||||
|
",
|
||||||
|
},
|
||||||
|
"```
|
||||||
|
";
|
||||||
|
#[inline]
|
||||||
|
pub fn rem_euclid_int(self, rhs: $Inner) -> $Fixed<Frac> {
|
||||||
|
// Any overflow in coverting rhs to $Fixed<Frac> means that |rhs| > |self|,
|
||||||
|
// and consequently the remainder is self.
|
||||||
|
let fixed_rhs = match Self::checked_from_num(rhs) {
|
||||||
|
Some(s) => s,
|
||||||
|
None => return self,
|
||||||
|
};
|
||||||
|
self.rem_euclid(fixed_rhs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
comment! {
|
comment! {
|
||||||
"Checked multiplication. Returns the product, or [`None`] on overflow.
|
"Checked multiplication. Returns the product, or [`None`] on overflow.
|
||||||
|
|
||||||
|
@ -253,11 +341,10 @@ assert_eq!(Fix::max_value().checked_div_euclid(Fix::from_num(0.25)), None);
|
||||||
if_signed! {
|
if_signed! {
|
||||||
$Signedness;
|
$Signedness;
|
||||||
if (self % rhs).is_negative() {
|
if (self % rhs).is_negative() {
|
||||||
let one = Self::checked_from_num(1)?;
|
|
||||||
return if rhs.is_positive() {
|
return if rhs.is_positive() {
|
||||||
q.checked_sub(one)
|
q.checked_add(Self::checked_from_num(-1)?)
|
||||||
} else {
|
} else {
|
||||||
q.checked_add(one)
|
q.checked_add(Self::checked_from_num(1)?)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -298,6 +385,83 @@ assert_eq!(Fix::from_num(3.75).checked_rem_int(0), None);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
comment! {
|
||||||
|
"Checked Euclidean division by an integer. Returns the
|
||||||
|
quotient, or [`None`] if the divisor is zero",
|
||||||
|
if_signed_else_empty_str! {
|
||||||
|
$Signedness,
|
||||||
|
" or if the division results in overflow",
|
||||||
|
},
|
||||||
|
".
|
||||||
|
|
||||||
|
# Examples
|
||||||
|
|
||||||
|
```rust
|
||||||
|
use fixed::{types::extra::U4, ", $s_fixed, "};
|
||||||
|
type Fix = ", $s_fixed, "<U4>;
|
||||||
|
assert_eq!(Fix::from_num(7.5).checked_div_euclid_int(2), Some(Fix::from_num(3)));
|
||||||
|
assert_eq!(Fix::from_num(7.5).checked_div_euclid_int(0), None);
|
||||||
|
",
|
||||||
|
if_signed_else_empty_str! {
|
||||||
|
$Signedness,
|
||||||
|
"assert_eq!(Fix::min_value().checked_div_euclid_int(-1), None);
|
||||||
|
",
|
||||||
|
},
|
||||||
|
"```
|
||||||
|
|
||||||
|
[`None`]: https://doc.rust-lang.org/nightly/core/option/enum.Option.html#variant.None
|
||||||
|
";
|
||||||
|
#[inline]
|
||||||
|
pub fn checked_div_euclid_int(self, rhs: $Inner) -> Option<$Fixed<Frac>> {
|
||||||
|
let q = self.checked_div_int(rhs)?.round_to_zero();
|
||||||
|
if_signed! {
|
||||||
|
$Signedness;
|
||||||
|
if (self % rhs).is_negative() {
|
||||||
|
return if rhs.is_positive() {
|
||||||
|
q.checked_add(Self::checked_from_num(-1)?)
|
||||||
|
} else {
|
||||||
|
q.checked_add(Self::checked_from_num(1)?)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Some(q)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
comment! {
|
||||||
|
"Checked remainder for Euclidean division by an integer.
|
||||||
|
Returns the remainder, or [`None`] if the divisor is zero.
|
||||||
|
|
||||||
|
# Examples
|
||||||
|
|
||||||
|
```rust
|
||||||
|
use fixed::{types::extra::U4, ", $s_fixed, "};
|
||||||
|
type Fix = ", $s_fixed, "<U4>;
|
||||||
|
assert_eq!(Fix::from_num(7.5).checked_rem_euclid_int(2), Some(Fix::from_num(1.5)));
|
||||||
|
assert_eq!(Fix::from_num(7.5).checked_rem_euclid_int(0), None);
|
||||||
|
",
|
||||||
|
if_signed_else_empty_str! {
|
||||||
|
$Signedness,
|
||||||
|
"assert_eq!(Fix::from_num(-7.5).checked_rem_euclid_int(2), Some(Fix::from_num(0.5)));
|
||||||
|
",
|
||||||
|
},
|
||||||
|
"```
|
||||||
|
|
||||||
|
[`None`]: https://doc.rust-lang.org/nightly/core/option/enum.Option.html#variant.None
|
||||||
|
";
|
||||||
|
#[inline]
|
||||||
|
pub fn checked_rem_euclid_int(self, rhs: $Inner) -> Option<$Fixed<Frac>> {
|
||||||
|
// Any overflow in coverting rhs to $Fixed<Frac> means that |rhs| > |self|,
|
||||||
|
// and consequently the remainder is self.
|
||||||
|
let fixed_rhs = match Self::checked_from_num(rhs) {
|
||||||
|
Some(s) => s,
|
||||||
|
None => return Some(self),
|
||||||
|
};
|
||||||
|
self.checked_rem_euclid(fixed_rhs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
comment! {
|
comment! {
|
||||||
"Saturating multiplication. Returns the product, saturating on overflow.
|
"Saturating multiplication. Returns the product, saturating on overflow.
|
||||||
|
|
||||||
|
@ -467,6 +631,45 @@ assert_eq!(Fix::max_value().wrapping_div_euclid(Fix::from_num(0.25)), wrapped);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
comment! {
|
||||||
|
"Wrapping Euclidean division by an integer. Returns the quotient",
|
||||||
|
if_signed_unsigned! {
|
||||||
|
$Signedness,
|
||||||
|
", wrapping on overflow.
|
||||||
|
|
||||||
|
Overflow can only occur when dividing the minimum value by −1.",
|
||||||
|
".
|
||||||
|
|
||||||
|
Can never overflow for unsigned values.",
|
||||||
|
},
|
||||||
|
"
|
||||||
|
|
||||||
|
# Panics
|
||||||
|
|
||||||
|
Panics if the divisor is zero.
|
||||||
|
|
||||||
|
# Examples
|
||||||
|
|
||||||
|
```rust
|
||||||
|
use fixed::{types::extra::U4, ", $s_fixed, "};
|
||||||
|
type Fix = ", $s_fixed, "<U4>;
|
||||||
|
assert_eq!(Fix::from_num(7.5).wrapping_div_euclid_int(2), Fix::from_num(3));
|
||||||
|
",
|
||||||
|
if_signed_else_empty_str! {
|
||||||
|
$Signedness,
|
||||||
|
"assert_eq!(Fix::from_num(-7.5).wrapping_div_euclid_int(2), Fix::from_num(-4));
|
||||||
|
let wrapped = Fix::min_value().round_to_zero();
|
||||||
|
assert_eq!(Fix::min_value().wrapping_div_euclid_int(-1), wrapped);
|
||||||
|
",
|
||||||
|
},
|
||||||
|
"```
|
||||||
|
";
|
||||||
|
#[inline]
|
||||||
|
pub fn wrapping_div_euclid_int(self, rhs: $Inner) -> $Fixed<Frac> {
|
||||||
|
self.overflowing_div_euclid_int(rhs).0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
comment! {
|
comment! {
|
||||||
"Overflowing multiplication.
|
"Overflowing multiplication.
|
||||||
|
|
||||||
|
@ -555,13 +758,80 @@ assert_eq!(Fix::max_value().overflowing_div_euclid(Fix::from_num(0.25)), (wrappe
|
||||||
if_signed! {
|
if_signed! {
|
||||||
$Signedness;
|
$Signedness;
|
||||||
if (self % rhs).is_negative() {
|
if (self % rhs).is_negative() {
|
||||||
|
let (q, overflow2) = if rhs.is_positive() {
|
||||||
|
let minus_one = match Self::checked_from_num(-1) {
|
||||||
|
None => return (q, true),
|
||||||
|
Some(s) => s,
|
||||||
|
};
|
||||||
|
q.overflowing_add(minus_one)
|
||||||
|
} else {
|
||||||
let one = match Self::checked_from_num(1) {
|
let one = match Self::checked_from_num(1) {
|
||||||
None => return (q, true),
|
None => return (q, true),
|
||||||
Some(one) => one,
|
Some(s) => s,
|
||||||
};
|
};
|
||||||
|
q.overflowing_add(one)
|
||||||
|
};
|
||||||
|
return (q, overflow | overflow2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
(q, overflow)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
comment! {
|
||||||
|
"Overflowing Euclidean division by an integer.
|
||||||
|
|
||||||
|
Returns a [tuple] of the quotient and ",
|
||||||
|
if_signed_unsigned! {
|
||||||
|
$Signedness,
|
||||||
|
"a [`bool`] indicating whether an overflow has
|
||||||
|
occurred. On overflow, the wrapped value is returned. Overflow can
|
||||||
|
only occur when dividing the minimum value by −1.",
|
||||||
|
"[`false`][`bool`], as the division can never overflow for unsigned values.",
|
||||||
|
},
|
||||||
|
"
|
||||||
|
|
||||||
|
# Panics
|
||||||
|
|
||||||
|
Panics if the divisor is zero.
|
||||||
|
|
||||||
|
# Examples
|
||||||
|
|
||||||
|
```rust
|
||||||
|
use fixed::{types::extra::U4, ", $s_fixed, "};
|
||||||
|
type Fix = ", $s_fixed, "<U4>;
|
||||||
|
assert_eq!(Fix::from_num(7.5).overflowing_div_euclid_int(2), (Fix::from_num(3), false));
|
||||||
|
",
|
||||||
|
if_signed_else_empty_str! {
|
||||||
|
$Signedness,
|
||||||
|
"assert_eq!(Fix::from_num(-7.5).overflowing_div_euclid_int(2), (Fix::from_num(-4), false));
|
||||||
|
let wrapped = Fix::min_value().round_to_zero();
|
||||||
|
assert_eq!(Fix::min_value().overflowing_div_euclid_int(-1), (wrapped, true));
|
||||||
|
",
|
||||||
|
},
|
||||||
|
"```
|
||||||
|
|
||||||
|
[`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html
|
||||||
|
[tuple]: https://doc.rust-lang.org/nightly/std/primitive.tuple.html
|
||||||
|
";
|
||||||
|
#[inline]
|
||||||
|
pub fn overflowing_div_euclid_int(self, rhs: $Inner) -> ($Fixed<Frac>, bool) {
|
||||||
|
let (mut q, overflow) = self.overflowing_div_int(rhs);
|
||||||
|
q = q.round_to_zero();
|
||||||
|
if_signed! {
|
||||||
|
$Signedness;
|
||||||
|
if (self % rhs).is_negative() {
|
||||||
let (q, overflow2) = if rhs.is_positive() {
|
let (q, overflow2) = if rhs.is_positive() {
|
||||||
q.overflowing_sub(one)
|
let minus_one = match Self::checked_from_num(-1) {
|
||||||
|
None => return (q, true),
|
||||||
|
Some(s) => s,
|
||||||
|
};
|
||||||
|
q.overflowing_add(minus_one)
|
||||||
} else {
|
} else {
|
||||||
|
let one = match Self::checked_from_num(1) {
|
||||||
|
None => return (q, true),
|
||||||
|
Some(s) => s,
|
||||||
|
};
|
||||||
q.overflowing_add(one)
|
q.overflowing_add(one)
|
||||||
};
|
};
|
||||||
return (q, overflow | overflow2);
|
return (q, overflow | overflow2);
|
||||||
|
@ -592,6 +862,28 @@ assert_eq!(Fix::max_value().overflowing_div_euclid(Fix::from_num(0.25)), (wrappe
|
||||||
pub fn overflowing_rem_int(self, rhs: $Inner) -> ($Fixed<Frac>, bool) {
|
pub fn overflowing_rem_int(self, rhs: $Inner) -> ($Fixed<Frac>, bool) {
|
||||||
(self % rhs, false)
|
(self % rhs, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Remainder for Euclidean division by an integer.
|
||||||
|
///
|
||||||
|
/// # Panics
|
||||||
|
///
|
||||||
|
/// Panics if the divisor is zero.
|
||||||
|
#[deprecated(since = "0.5.3", note = "cannot overflow, use `rem_euclid_int` instead")]
|
||||||
|
#[inline]
|
||||||
|
pub fn wrapping_rem_euclid_int(self, rhs: $Inner) -> $Fixed<Frac> {
|
||||||
|
self.rem_euclid_int(rhs)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Remainder for Euclidean division by an integer.
|
||||||
|
///
|
||||||
|
/// # Panics
|
||||||
|
///
|
||||||
|
/// Panics if the divisor is zero.
|
||||||
|
#[deprecated(since = "0.5.3", note = "cannot overflow, use `rem_euclid_int` instead")]
|
||||||
|
#[inline]
|
||||||
|
pub fn overflowing_rem_euclid_int(self, rhs: $Inner) -> ($Fixed<Frac>, bool) {
|
||||||
|
(self.rem_euclid_int(rhs), false)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -453,70 +453,6 @@ assert_eq!(Fix::from_num(7.5).rem_euclid(Fix::from_num(2)), Fix::from_num(1.5));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
comment! {
|
|
||||||
"Euclidean division by an integer.
|
|
||||||
|
|
||||||
# Panics
|
|
||||||
|
|
||||||
Panics if the divisor is zero",
|
|
||||||
if_signed_else_empty_str! {
|
|
||||||
$Signedness,
|
|
||||||
" or if the division results in overflow",
|
|
||||||
},
|
|
||||||
".
|
|
||||||
|
|
||||||
# Examples
|
|
||||||
|
|
||||||
```rust
|
|
||||||
use fixed::{types::extra::U4, ", $s_fixed, "};
|
|
||||||
type Fix = ", $s_fixed, "<U4>;
|
|
||||||
assert_eq!(Fix::from_bits(0x7F).div_euclid_int(4), Fix::from_bits(0x1F));
|
|
||||||
",
|
|
||||||
if_signed_else_empty_str! {
|
|
||||||
$Signedness,
|
|
||||||
"assert_eq!(Fix::from_bits(-0x7F).div_euclid_int(4), Fix::from_bits(-0x20));
|
|
||||||
",
|
|
||||||
},
|
|
||||||
"```
|
|
||||||
";
|
|
||||||
#[inline]
|
|
||||||
pub fn div_euclid_int(self, rhs: $Inner) -> $Fixed<Frac> {
|
|
||||||
Self::from_bits(self.to_bits().div_euclid(rhs))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
comment! {
|
|
||||||
"Remainder for Euclidean division by an integer.
|
|
||||||
|
|
||||||
# Panics
|
|
||||||
|
|
||||||
Panics if the divisor is zero",
|
|
||||||
if_signed_else_empty_str! {
|
|
||||||
$Signedness,
|
|
||||||
" or if the division results in overflow",
|
|
||||||
},
|
|
||||||
".
|
|
||||||
|
|
||||||
# Examples
|
|
||||||
|
|
||||||
```rust
|
|
||||||
use fixed::{types::extra::U4, ", $s_fixed, "};
|
|
||||||
type Fix = ", $s_fixed, "<U4>;
|
|
||||||
assert_eq!(Fix::from_bits(0x7F).rem_euclid_int(4), Fix::from_bits(0x03));
|
|
||||||
",
|
|
||||||
if_signed_else_empty_str! {
|
|
||||||
$Signedness,
|
|
||||||
"assert_eq!(Fix::from_bits(-0x7F).rem_euclid_int(4), Fix::from_bits(0x01));
|
|
||||||
",
|
|
||||||
},
|
|
||||||
"```
|
|
||||||
";
|
|
||||||
#[inline]
|
|
||||||
pub fn rem_euclid_int(self, rhs: $Inner) -> $Fixed<Frac> {
|
|
||||||
Self::from_bits(self.to_bits().rem_euclid(rhs))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if_signed! {
|
if_signed! {
|
||||||
$Signedness;
|
$Signedness;
|
||||||
comment! {
|
comment! {
|
||||||
|
@ -769,72 +705,6 @@ assert_eq!(num.checked_rem_euclid(Fix::from_num(0)), None);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
comment! {
|
|
||||||
"Checked Euclidean division by an integer. Returns the
|
|
||||||
quotient, or [`None`] if the divisor is zero",
|
|
||||||
if_signed_else_empty_str! {
|
|
||||||
$Signedness,
|
|
||||||
" or if the division results in overflow",
|
|
||||||
},
|
|
||||||
".
|
|
||||||
|
|
||||||
# Examples
|
|
||||||
|
|
||||||
```rust
|
|
||||||
use fixed::{types::extra::U4, ", $s_fixed, "};
|
|
||||||
type Fix = ", $s_fixed, "<U4>;
|
|
||||||
assert_eq!(Fix::from_num(1).checked_div_euclid_int(0), None);
|
|
||||||
assert_eq!(Fix::from_bits(0x7F).checked_div_euclid_int(4), Some(Fix::from_bits(0x1F)));
|
|
||||||
",
|
|
||||||
if_signed_else_empty_str! {
|
|
||||||
$Signedness,
|
|
||||||
"assert_eq!(Fix::from_bits(-0x7F).checked_div_euclid_int(4), Some(Fix::from_bits(-0x20)));
|
|
||||||
assert_eq!(Fix::min_value().checked_div_int(-1), None);
|
|
||||||
",
|
|
||||||
},
|
|
||||||
"```
|
|
||||||
|
|
||||||
[`None`]: https://doc.rust-lang.org/nightly/core/option/enum.Option.html#variant.None
|
|
||||||
";
|
|
||||||
#[inline]
|
|
||||||
pub fn checked_div_euclid_int(self, rhs: $Inner) -> Option<$Fixed<Frac>> {
|
|
||||||
self.to_bits().checked_div_euclid(rhs).map(Self::from_bits)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
comment! {
|
|
||||||
"Checked fixed-point remainder for Euclidean division by an integer.
|
|
||||||
Returns the remainder, or [`None`] if the divisor is zero",
|
|
||||||
if_signed_else_empty_str! {
|
|
||||||
$Signedness,
|
|
||||||
" or if the division results in overflow",
|
|
||||||
},
|
|
||||||
".
|
|
||||||
|
|
||||||
# Examples
|
|
||||||
|
|
||||||
```rust
|
|
||||||
use fixed::{types::extra::U4, ", $s_fixed, "};
|
|
||||||
type Fix = ", $s_fixed, "<U4>;
|
|
||||||
assert_eq!(Fix::from_num(1).checked_rem_euclid_int(0), None);
|
|
||||||
assert_eq!(Fix::from_bits(0x7F).checked_rem_euclid_int(4), Some(Fix::from_bits(0x03)));
|
|
||||||
",
|
|
||||||
if_signed_else_empty_str! {
|
|
||||||
$Signedness,
|
|
||||||
"assert_eq!(Fix::from_bits(-0x7F).checked_rem_euclid_int(4), Some(Fix::from_bits(0x01)));
|
|
||||||
assert_eq!(Fix::min_value().checked_rem_euclid_int(-1), None);
|
|
||||||
",
|
|
||||||
},
|
|
||||||
"```
|
|
||||||
|
|
||||||
[`None`]: https://doc.rust-lang.org/nightly/core/option/enum.Option.html#variant.None
|
|
||||||
";
|
|
||||||
#[inline]
|
|
||||||
pub fn checked_rem_euclid_int(self, rhs: $Inner) -> Option<$Fixed<Frac>> {
|
|
||||||
self.to_bits().checked_rem_euclid(rhs).map(Self::from_bits)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
comment! {
|
comment! {
|
||||||
"Checked shift left. Returns the shifted number,
|
"Checked shift left. Returns the shifted number,
|
||||||
or [`None`] if `rhs` ≥ ", $s_nbits, ".
|
or [`None`] if `rhs` ≥ ", $s_nbits, ".
|
||||||
|
@ -1227,83 +1097,6 @@ assert_eq!(Fix::from_num(3).wrapping_div_int(2), one_point_5);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
comment! {
|
|
||||||
"Wrapping Euclidean division by an integer. Returns the quotient",
|
|
||||||
if_signed_unsigned! {
|
|
||||||
$Signedness,
|
|
||||||
", wrapping on overflow.
|
|
||||||
|
|
||||||
Overflow can only occur when dividing the minimum value by −1.",
|
|
||||||
".
|
|
||||||
|
|
||||||
Can never overflow for unsigned values.",
|
|
||||||
},
|
|
||||||
"
|
|
||||||
|
|
||||||
# Panics
|
|
||||||
|
|
||||||
Panics if the divisor is zero.
|
|
||||||
|
|
||||||
# Examples
|
|
||||||
|
|
||||||
```rust
|
|
||||||
use fixed::{types::extra::U4, ", $s_fixed, "};
|
|
||||||
type Fix = ", $s_fixed, "<U4>;
|
|
||||||
assert_eq!(Fix::from_bits(0x7F).wrapping_div_euclid_int(4), Fix::from_bits(0x1F));
|
|
||||||
",
|
|
||||||
if_signed_else_empty_str! {
|
|
||||||
$Signedness,
|
|
||||||
"assert_eq!(Fix::from_bits(-0x7F).wrapping_div_euclid_int(4), Fix::from_bits(-0x20));
|
|
||||||
assert_eq!(Fix::min_value().wrapping_div_euclid_int(-1), Fix::min_value());
|
|
||||||
",
|
|
||||||
},
|
|
||||||
"```
|
|
||||||
";
|
|
||||||
#[inline]
|
|
||||||
pub fn wrapping_div_euclid_int(self, rhs: $Inner) -> $Fixed<Frac> {
|
|
||||||
Self::from_bits(self.to_bits().wrapping_div_euclid(rhs))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
comment! {
|
|
||||||
"Wrapping fixed-point remainder for Euclidean division
|
|
||||||
by an integer. Returns the remainder",
|
|
||||||
if_signed_unsigned! {
|
|
||||||
$Signedness,
|
|
||||||
", wrapping on overflow.
|
|
||||||
|
|
||||||
Overflow can only occur when dividing the minimum value by −1.",
|
|
||||||
".
|
|
||||||
|
|
||||||
Can never overflow for unsigned values.",
|
|
||||||
},
|
|
||||||
"
|
|
||||||
|
|
||||||
# Panics
|
|
||||||
|
|
||||||
Panics if the divisor is zero.
|
|
||||||
|
|
||||||
# Examples
|
|
||||||
|
|
||||||
```rust
|
|
||||||
use fixed::{types::extra::U4, ", $s_fixed, "};
|
|
||||||
type Fix = ", $s_fixed, "<U4>;
|
|
||||||
assert_eq!(Fix::from_bits(0x7F).wrapping_rem_euclid_int(4), Fix::from_bits(0x03));
|
|
||||||
",
|
|
||||||
if_signed_else_empty_str! {
|
|
||||||
$Signedness,
|
|
||||||
"assert_eq!(Fix::from_bits(-0x7F).wrapping_rem_euclid_int(4), Fix::from_bits(0x01));
|
|
||||||
assert_eq!(Fix::min_value().wrapping_rem_euclid_int(-1), 0);
|
|
||||||
",
|
|
||||||
},
|
|
||||||
"```
|
|
||||||
";
|
|
||||||
#[inline]
|
|
||||||
pub fn wrapping_rem_euclid_int(self, rhs: $Inner) -> $Fixed<Frac> {
|
|
||||||
Self::from_bits(self.to_bits().wrapping_rem_euclid(rhs))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
comment! {
|
comment! {
|
||||||
"Wrapping shift left. Wraps `rhs` if `rhs` ≥ ", $s_nbits, ",
|
"Wrapping shift left. Wraps `rhs` if `rhs` ≥ ", $s_nbits, ",
|
||||||
then shifts and returns the number.
|
then shifts and returns the number.
|
||||||
|
@ -1539,90 +1332,6 @@ assert_eq!(Fix::from_num(3).overflowing_div_int(2), (one_point_5, false));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
comment! {
|
|
||||||
"Overflowing Euclidean division by an integer.
|
|
||||||
|
|
||||||
Returns a [tuple] of the quotient and ",
|
|
||||||
if_signed_unsigned! {
|
|
||||||
$Signedness,
|
|
||||||
"a [`bool`] indicating whether an overflow has
|
|
||||||
occurred. On overflow, the wrapped value is returned. Overflow can
|
|
||||||
only occur when dividing the minimum value by −1.",
|
|
||||||
"[`false`][`bool`], as the division can never overflow for unsigned values.",
|
|
||||||
},
|
|
||||||
"
|
|
||||||
|
|
||||||
# Panics
|
|
||||||
|
|
||||||
Panics if the divisor is zero.
|
|
||||||
|
|
||||||
# Examples
|
|
||||||
|
|
||||||
```rust
|
|
||||||
use fixed::{types::extra::U4, ", $s_fixed, "};
|
|
||||||
type Fix = ", $s_fixed, "<U4>;
|
|
||||||
assert_eq!(Fix::from_bits(0x7F).overflowing_div_euclid_int(4), (Fix::from_bits(0x1F), false));
|
|
||||||
",
|
|
||||||
if_signed_else_empty_str! {
|
|
||||||
$Signedness,
|
|
||||||
"assert_eq!(Fix::from_bits(-0x7F).overflowing_div_euclid_int(4), (Fix::from_bits(-0x20), false));
|
|
||||||
assert_eq!(Fix::min_value().overflowing_div_euclid_int(-1), (Fix::min_value(), true));
|
|
||||||
",
|
|
||||||
},
|
|
||||||
"```
|
|
||||||
|
|
||||||
[`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html
|
|
||||||
[tuple]: https://doc.rust-lang.org/nightly/std/primitive.tuple.html
|
|
||||||
";
|
|
||||||
#[inline]
|
|
||||||
pub fn overflowing_div_euclid_int(self, rhs: $Inner) -> ($Fixed<Frac>, bool) {
|
|
||||||
let (ans, o) = self.to_bits().overflowing_div_euclid(rhs);
|
|
||||||
(Self::from_bits(ans), o)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
comment! {
|
|
||||||
"Overflowing fixed-point remainder for Euclidean division by an integer.
|
|
||||||
|
|
||||||
Returns a [tuple] of the remainder and ",
|
|
||||||
if_signed_unsigned! {
|
|
||||||
$Signedness,
|
|
||||||
"a [`bool`] indicating whether an overflow has
|
|
||||||
occurred. On overflow, the wrapped value is returned. Overflow can
|
|
||||||
only occur when dividing the minimum value by −1.",
|
|
||||||
"[`false`][`bool`], as the division can never overflow for unsigned values.",
|
|
||||||
},
|
|
||||||
"
|
|
||||||
|
|
||||||
# Panics
|
|
||||||
|
|
||||||
Panics if the divisor is zero.
|
|
||||||
|
|
||||||
# Examples
|
|
||||||
|
|
||||||
```rust
|
|
||||||
use fixed::{types::extra::U4, ", $s_fixed, "};
|
|
||||||
type Fix = ", $s_fixed, "<U4>;
|
|
||||||
assert_eq!(Fix::from_bits(0x7F).overflowing_rem_euclid_int(4), (Fix::from_bits(0x03), false));
|
|
||||||
",
|
|
||||||
if_signed_else_empty_str! {
|
|
||||||
$Signedness,
|
|
||||||
"assert_eq!(Fix::from_bits(-0x7F).overflowing_rem_euclid_int(4), (Fix::from_bits(0x01), false));
|
|
||||||
assert_eq!(Fix::min_value().overflowing_rem_euclid_int(-1), (Fix::from_num(0), true));
|
|
||||||
",
|
|
||||||
},
|
|
||||||
"```
|
|
||||||
|
|
||||||
[`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html
|
|
||||||
[tuple]: https://doc.rust-lang.org/nightly/std/primitive.tuple.html
|
|
||||||
";
|
|
||||||
#[inline]
|
|
||||||
pub fn overflowing_rem_euclid_int(self, rhs: $Inner) -> ($Fixed<Frac>, bool) {
|
|
||||||
let (ans, o) = self.to_bits().overflowing_rem_euclid(rhs);
|
|
||||||
(Self::from_bits(ans), o)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
comment! {
|
comment! {
|
||||||
"Overflowing shift left.
|
"Overflowing shift left.
|
||||||
|
|
||||||
|
|
|
@ -692,7 +692,7 @@ where
|
||||||
|
|
||||||
/// Checked fixed-point remainder for Euclidean division by an
|
/// Checked fixed-point remainder for Euclidean division by an
|
||||||
/// integer. Returns the remainder, or [`None`] if the divisor is
|
/// integer. Returns the remainder, or [`None`] if the divisor is
|
||||||
/// zero or if the division results in overflow.
|
/// zero.
|
||||||
///
|
///
|
||||||
/// [`None`]: https://doc.rust-lang.org/nightly/core/option/enum.Option.html#variant.None
|
/// [`None`]: https://doc.rust-lang.org/nightly/core/option/enum.Option.html#variant.None
|
||||||
fn checked_rem_euclid_int(self, rhs: Self::Bits) -> Option<Self>;
|
fn checked_rem_euclid_int(self, rhs: Self::Bits) -> Option<Self>;
|
||||||
|
@ -786,16 +786,6 @@ where
|
||||||
/// Panics if the divisor is zero.
|
/// Panics if the divisor is zero.
|
||||||
fn wrapping_div_euclid_int(self, rhs: Self::Bits) -> Self;
|
fn wrapping_div_euclid_int(self, rhs: Self::Bits) -> Self;
|
||||||
|
|
||||||
/// Wrapping fixed-point remainder for Euclidean division by an
|
|
||||||
/// integer. Returns the remainder, wrapping on overflow.
|
|
||||||
///
|
|
||||||
/// Overflow can only occur when dividing the minimum value by −1.
|
|
||||||
///
|
|
||||||
/// # Panics
|
|
||||||
///
|
|
||||||
/// Panics if the divisor is zero.
|
|
||||||
fn wrapping_rem_euclid_int(self, rhs: Self::Bits) -> Self;
|
|
||||||
|
|
||||||
/// Wrapping shift left. Wraps `rhs` if `rhs` ≥ the number of
|
/// Wrapping shift left. Wraps `rhs` if `rhs` ≥ the number of
|
||||||
/// bits, then shifts and returns the number.
|
/// bits, then shifts and returns the number.
|
||||||
fn wrapping_shl(self, rhs: u32) -> Self;
|
fn wrapping_shl(self, rhs: u32) -> Self;
|
||||||
|
@ -910,21 +900,6 @@ where
|
||||||
/// [tuple]: https://doc.rust-lang.org/nightly/std/primitive.tuple.html
|
/// [tuple]: https://doc.rust-lang.org/nightly/std/primitive.tuple.html
|
||||||
fn overflowing_div_euclid_int(self, rhs: Self::Bits) -> (Self, bool);
|
fn overflowing_div_euclid_int(self, rhs: Self::Bits) -> (Self, bool);
|
||||||
|
|
||||||
/// Overflowing fixed-point remainder for Euclidean division by an integer.
|
|
||||||
///
|
|
||||||
/// Returns a [tuple] of the remainder and a [`bool`], indicating
|
|
||||||
/// whether an overflow has occurred. On overflow, the wrapped
|
|
||||||
/// value is returned. Overflow can only occur when dividing the
|
|
||||||
/// minimum value by −1.
|
|
||||||
///
|
|
||||||
/// # Panics
|
|
||||||
///
|
|
||||||
/// Panics if the divisor is zero.
|
|
||||||
///
|
|
||||||
/// [`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html
|
|
||||||
/// [tuple]: https://doc.rust-lang.org/nightly/std/primitive.tuple.html
|
|
||||||
fn overflowing_rem_euclid_int(self, rhs: Self::Bits) -> (Self, bool);
|
|
||||||
|
|
||||||
/// Overflowing shift left.
|
/// Overflowing shift left.
|
||||||
///
|
///
|
||||||
/// Returns a [tuple] of the shifted value and a [`bool`],
|
/// Returns a [tuple] of the shifted value and a [`bool`],
|
||||||
|
@ -950,7 +925,10 @@ where
|
||||||
/// # Panics
|
/// # Panics
|
||||||
///
|
///
|
||||||
/// Panics if the divisor is zero.
|
/// Panics if the divisor is zero.
|
||||||
#[deprecated(since = "0.5.3", note = "cannot overflow, use `%` or `Rem::rem` instead")]
|
#[deprecated(
|
||||||
|
since = "0.5.3",
|
||||||
|
note = "cannot overflow, use `%` or `Rem::rem` instead"
|
||||||
|
)]
|
||||||
fn wrapping_rem_int(self, rhs: Self::Bits) -> Self {
|
fn wrapping_rem_int(self, rhs: Self::Bits) -> Self {
|
||||||
self % rhs
|
self % rhs
|
||||||
}
|
}
|
||||||
|
@ -960,10 +938,39 @@ where
|
||||||
/// # Panics
|
/// # Panics
|
||||||
///
|
///
|
||||||
/// Panics if the divisor is zero.
|
/// Panics if the divisor is zero.
|
||||||
#[deprecated(since = "0.5.3", note = "cannot overflow, use `%` or `Rem::rem` instead")]
|
#[deprecated(
|
||||||
|
since = "0.5.3",
|
||||||
|
note = "cannot overflow, use `%` or `Rem::rem` instead"
|
||||||
|
)]
|
||||||
fn overflowing_rem_int(self, rhs: Self::Bits) -> (Self, bool) {
|
fn overflowing_rem_int(self, rhs: Self::Bits) -> (Self, bool) {
|
||||||
(self % rhs, false)
|
(self % rhs, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Remainder for Euclidean division by an integer.
|
||||||
|
///
|
||||||
|
/// # Panics
|
||||||
|
///
|
||||||
|
/// Panics if the divisor is zero.
|
||||||
|
#[deprecated(
|
||||||
|
since = "0.5.3",
|
||||||
|
note = "cannot overflow, use `rem_euclid_int` instead"
|
||||||
|
)]
|
||||||
|
fn wrapping_rem_euclid_int(self, rhs: Self::Bits) -> Self {
|
||||||
|
self.rem_euclid_int(rhs)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Remainder for Euclidean division by an integer.
|
||||||
|
///
|
||||||
|
/// # Panics
|
||||||
|
///
|
||||||
|
/// Panics if the divisor is zero.
|
||||||
|
#[deprecated(
|
||||||
|
since = "0.5.3",
|
||||||
|
note = "cannot overflow, use `rem_euclid_int` instead"
|
||||||
|
)]
|
||||||
|
fn overflowing_rem_euclid_int(self, rhs: Self::Bits) -> (Self, bool) {
|
||||||
|
(self.rem_euclid_int(rhs), false)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This trait provides methods common to all signed fixed-point numbers.
|
/// This trait provides methods common to all signed fixed-point numbers.
|
||||||
|
@ -1610,7 +1617,6 @@ macro_rules! impl_fixed {
|
||||||
trait_delegate! { fn wrapping_mul_int(self, rhs: Self::Bits) -> Self }
|
trait_delegate! { fn wrapping_mul_int(self, rhs: Self::Bits) -> Self }
|
||||||
trait_delegate! { fn wrapping_div_int(self, rhs: Self::Bits) -> Self }
|
trait_delegate! { fn wrapping_div_int(self, rhs: Self::Bits) -> Self }
|
||||||
trait_delegate! { fn wrapping_div_euclid_int(self, rhs: Self::Bits) -> Self }
|
trait_delegate! { fn wrapping_div_euclid_int(self, rhs: Self::Bits) -> Self }
|
||||||
trait_delegate! { fn wrapping_rem_euclid_int(self, rhs: Self::Bits) -> Self }
|
|
||||||
trait_delegate! { fn wrapping_shl(self, rhs: u32) -> Self }
|
trait_delegate! { fn wrapping_shl(self, rhs: u32) -> Self }
|
||||||
trait_delegate! { fn wrapping_shr(self, rhs: u32) -> Self }
|
trait_delegate! { fn wrapping_shr(self, rhs: u32) -> Self }
|
||||||
trait_delegate! { fn overflowing_neg(self) -> (Self, bool) }
|
trait_delegate! { fn overflowing_neg(self) -> (Self, bool) }
|
||||||
|
@ -1622,7 +1628,6 @@ macro_rules! impl_fixed {
|
||||||
trait_delegate! { fn overflowing_mul_int(self, rhs: Self::Bits) -> (Self, bool) }
|
trait_delegate! { fn overflowing_mul_int(self, rhs: Self::Bits) -> (Self, bool) }
|
||||||
trait_delegate! { fn overflowing_div_int(self, rhs: Self::Bits) -> (Self, bool) }
|
trait_delegate! { fn overflowing_div_int(self, rhs: Self::Bits) -> (Self, bool) }
|
||||||
trait_delegate! { fn overflowing_div_euclid_int(self, rhs: Self::Bits) -> (Self, bool) }
|
trait_delegate! { fn overflowing_div_euclid_int(self, rhs: Self::Bits) -> (Self, bool) }
|
||||||
trait_delegate! { fn overflowing_rem_euclid_int(self, rhs: Self::Bits) -> (Self, bool) }
|
|
||||||
trait_delegate! { fn overflowing_shl(self, rhs: u32) -> (Self, bool) }
|
trait_delegate! { fn overflowing_shl(self, rhs: u32) -> (Self, bool) }
|
||||||
trait_delegate! { fn overflowing_shr(self, rhs: u32) -> (Self, bool) }
|
trait_delegate! { fn overflowing_shr(self, rhs: u32) -> (Self, bool) }
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,9 +18,9 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
from_str::ParseFixedError,
|
from_str::ParseFixedError,
|
||||||
traits::{Fixed, FixedSigned, FixedUnsigned, FromFixed, ToFixed},
|
traits::{Fixed, FixedSigned, FixedUnsigned, FromFixed, ToFixed},
|
||||||
|
types::extra::{LeEqU128, LeEqU16, LeEqU32, LeEqU64, LeEqU8},
|
||||||
FixedI128, FixedI16, FixedI32, FixedI64, FixedI8, FixedU128, FixedU16, FixedU32, FixedU64,
|
FixedI128, FixedI16, FixedI32, FixedI64, FixedI8, FixedU128, FixedU16, FixedU32, FixedU64,
|
||||||
FixedU8,
|
FixedU8,
|
||||||
types::extra::{LeEqU128, LeEqU16, LeEqU32, LeEqU64, LeEqU8},
|
|
||||||
};
|
};
|
||||||
use core::{
|
use core::{
|
||||||
fmt::{Display, Formatter, Result as FmtResult},
|
fmt::{Display, Formatter, Result as FmtResult},
|
||||||
|
|
Loading…
Reference in New Issue