constify unwrapped_div_int

This commit is contained in:
Trevor Spiteri 2021-04-15 17:42:32 +02:00
parent 89d5a9e8db
commit 49422dc9f1
3 changed files with 18 additions and 10 deletions

View File

@ -113,6 +113,7 @@ The conversions supported cover the following cases.
* [`rem_euclid`][f-re-1-8], [`checked_rem_euclid`][f-cre-1-8]
* [`checked_div_int`][f-cdi-1-8],
[`wrapping_div_int`][f-wdi-1-8],
[`unwrapped_div_int`][f-udi-1-8],
[`overflowing_div_int`][f-odi-1-8]
* The following methods were added to all fixed-point numbers:
* [`const_not`][f-cn-1-8]
@ -133,6 +134,7 @@ The conversions supported cover the following cases.
[f-re-1-8]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.rem_euclid
[f-sdei-1-8]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.saturating_div_euclid_int
[f-srei-1-8]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.saturating_rem_euclid_int
[f-udi-1-8]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.unwrapped_div_int
[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

View File

@ -26,6 +26,7 @@ Version 1.8.0 (unreleased)
* [`rem_euclid`][f-re-1-8], [`checked_rem_euclid`][f-cre-1-8]
* [`checked_div_int`][f-cdi-1-8],
[`wrapping_div_int`][f-wdi-1-8],
[`unwrapped_div_int`][f-udi-1-8],
[`overflowing_div_int`][f-odi-1-8]
* The following methods were added to all fixed-point numbers:
* [`const_not`][f-cn-1-8]
@ -46,6 +47,7 @@ Version 1.8.0 (unreleased)
[f-re-1-8]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.rem_euclid
[f-sdei-1-8]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.saturating_div_euclid_int
[f-srei-1-8]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.saturating_rem_euclid_int
[f-udi-1-8]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.unwrapped_div_int
[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

View File

@ -2264,29 +2264,33 @@ type Fix = ", $s_fixed, "<U4>;
// 1.5 is binary 1.1
let one_point_5 = Fix::from_bits(0b11 << (4 - 1));
assert_eq!(Fix::from_num(3).unwrapped_div_int(2), one_point_5);
```
The following panics because the divisor is zero.
```should_panic
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
let _divisor_is_zero = Fix::from_num(3).unwrapped_div_int(0);
```
",
if_signed_else_empty_str! {
$Signedness,
"```
"
The following panics because of overflow.
```should_panic
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
let _overflow = Fix::MIN.unwrapped_div_int(-1);
```
",
},
"```
";
};
#[inline]
#[track_caller]
#[must_use = "this returns the result of the operation, without modifying the original"]
pub fn unwrapped_div_int(self, rhs: $Inner) -> $Fixed<Frac> {
match self.overflowing_div_int(rhs) {
(_, true) => panic!("overflow"),
(ans, false) => ans,
}
pub const fn unwrapped_div_int(self, rhs: $Inner) -> $Fixed<Frac> {
Self::from_bits(self.to_bits() / rhs)
}
}