replace {min,max}_value() by {MIN,MAX}

This commit is contained in:
Trevor Spiteri 2020-04-11 18:31:31 +02:00
parent 91bd73bca4
commit e1febebb31
12 changed files with 241 additions and 199 deletions

View File

@ -76,10 +76,15 @@ The conversions supported cover the following cases.
### Version 0.5.5 news (unreleased)
* The associated constants [`MIN`] and [`MAX`] were added to all
fixed-point types, to the [`Fixed`] trait, and to the [`Wrapping`]
wrapper.
* The methods [`int_log2`] and [`checked_int_log2`] were added to
all fixed-point types and to the [`Fixed`] trait.
* The method [`int_log2`][wril] was added to the [`Wrapping`]
wrapper.
* The following methods were deprecated:
* [`min_value`], [`max_value`]
### Version 0.5.4 news (2020-02-21)
@ -115,6 +120,8 @@ The conversions supported cover the following cases.
* [`Wrapping`] now supports serialization. (Thanks: Shane Pearman)
[`Fixed`]: https://docs.rs/fixed/0.5.4/fixed/traits/trait.Fixed.html
[`MAX`]: https://docs.rs/fixed/0.5.4/fixed/struct.FixedI32.html#associatedconstant.MAX
[`MIN`]: https://docs.rs/fixed/0.5.4/fixed/struct.FixedI32.html#associatedconstant.MIN
[`RemAssign`]: https://doc.rust-lang.org/nightly/core/ops/trait.RemAssign.html
[`Rem`]: https://doc.rust-lang.org/nightly/core/ops/trait.Rem.html
[`Wrapping`]: https://docs.rs/fixed/0.5.4/fixed/struct.Wrapping.html
@ -124,6 +131,8 @@ The conversions supported cover the following cases.
[`checked_rem`]: https://docs.rs/fixed/0.5.4/fixed/struct.FixedI32.html#method.checked_rem
[`div_euclid`]: https://docs.rs/fixed/0.5.4/fixed/struct.FixedI32.html#method.div_euclid
[`int_log2`]: https://docs.rs/fixed/0.5.4/fixed/struct.FixedI32.html#method.int_log2
[`max_value`]: https://docs.rs/fixed/0.5.4/fixed/struct.FixedI32.html#method.max_value
[`min_value`]: https://docs.rs/fixed/0.5.4/fixed/struct.FixedI32.html#method.min_value
[`overflowing_div_euclid`]: https://docs.rs/fixed/0.5.4/fixed/struct.FixedI32.html#method.overflowing_div_euclid
[`overflowing_rem_int`]: https://docs.rs/fixed/0.5.4/fixed/struct.FixedI32.html#method.overflowing_rem_int
[`rem_euclid_int`]: https://docs.rs/fixed/0.5.4/fixed/struct.FixedI32.html#method.rem_euclid_int

View File

@ -8,9 +8,14 @@ as-is, without any warranty. -->
Version 0.5.5 (unreleased)
==========================
* The associated constants `MIN` and `MAX` were added to all
fixed-point types, to the `Fixed` trait, and to the `Wrapping`
wrapper.
* The methods `int_log2` and `checked_int_log2` were added to all
fixed-point types and to the `Fixed` trait.
* The method `int_log2` was added to the `Wrapping` wrapper.
* The following methods were deprecated:
* `min_value`, `max_value`
Version 0.5.4 (2020-02-21)
==========================

View File

@ -268,7 +268,7 @@ macro_rules! fixed_arith {
refs_assign! { impl DivAssign for $Fixed($LeEqU) { div_assign } }
// do not pass! { Rem }, as I::min_value() % I::from(-1) should return 0, not panic
// do not pass! { Rem }, as I::MIN % I::from(-1) should return 0, not panic
impl<Frac> Rem<$Fixed<Frac>> for $Fixed<Frac> {
type Output = $Fixed<Frac>;
#[inline]
@ -828,10 +828,10 @@ mod tests {
I0F32::from_num(f)
}
assert_eq!(I16F16::min_value() % -1, 0);
assert_eq!(I16F16::min_value().checked_rem_int(-1).unwrap(), 0);
assert_eq!(I16F16::min_value().rem_euclid_int(-1), 0);
assert_eq!(I16F16::min_value().checked_rem_euclid_int(-1).unwrap(), 0);
assert_eq!(I16F16::MIN % -1, 0);
assert_eq!(I16F16::MIN.checked_rem_int(-1).unwrap(), 0);
assert_eq!(I16F16::MIN.rem_euclid_int(-1), 0);
assert_eq!(I16F16::MIN.checked_rem_euclid_int(-1).unwrap(), 0);
assert_eq!(i1(-1.0) % 1, i1(0.0));
assert_eq!(i1(-1.0).rem_euclid_int(1), i1(0.0));

View File

@ -1056,9 +1056,9 @@ mod tests {
#[test]
fn to_size() {
let min_i24 = I24F8::min_value();
let max_i24 = I24F8::max_value();
let max_u24 = U24F8::max_value();
let min_i24 = I24F8::MIN;
let max_i24 = I24F8::MAX;
let max_u24 = U24F8::MAX;
assert_eq!(min_i24.overflowing_to_num::<isize>(), (!0 << 23, false));
assert_eq!(max_i24.overflowing_to_num::<isize>(), (!(!0 << 23), false));
assert_eq!(max_u24.overflowing_to_num::<isize>(), (!(!0 << 24), false));
@ -1066,9 +1066,9 @@ mod tests {
assert_eq!(max_i24.overflowing_to_num::<usize>(), (!(!0 << 23), false));
assert_eq!(max_u24.overflowing_to_num::<usize>(), (!(!0 << 24), false));
let min_i56 = I56F8::min_value();
let max_i56 = I56F8::max_value();
let max_u56 = U56F8::max_value();
let min_i56 = I56F8::MIN;
let max_i56 = I56F8::MAX;
let max_u56 = U56F8::MAX;
#[cfg(target_pointer_width = "32")]
{
assert_eq!(min_i56.overflowing_to_num::<isize>(), (0, true));
@ -1088,9 +1088,9 @@ mod tests {
assert_eq!(max_u56.overflowing_to_num::<usize>(), (!(!0 << 56), false));
}
let min_i120 = I120F8::min_value();
let max_i120 = I120F8::max_value();
let max_u120 = U120F8::max_value();
let min_i120 = I120F8::MIN;
let max_i120 = I120F8::MAX;
let max_u120 = U120F8::MAX;
assert_eq!(min_i120.overflowing_to_num::<isize>(), (0, true));
assert_eq!(max_i120.overflowing_to_num::<isize>(), (!0, true));
assert_eq!(max_u120.overflowing_to_num::<isize>(), (!0, true));
@ -1336,7 +1336,7 @@ mod tests {
fn to_infinite_f32() {
// too_large is 1.ffff_ffff_ffff... << 127,
// which will be rounded to 1.0 << 128.
let too_large = U128F0::max_value();
let too_large = U128F0::MAX;
assert_eq!(too_large.count_ones(), 128);
assert!(too_large.to_num::<f32>().is_infinite());
@ -1355,7 +1355,7 @@ mod tests {
assert!(!not_too_large.to_num::<f32>().is_infinite());
// min_128 is -1.0 << 127.
let min_i128 = I128F0::min_value();
let min_i128 = I128F0::MIN;
assert_eq!(min_i128.count_ones(), 1);
assert_eq!(min_i128.to_num::<f32>(), -(127f32.exp2()));
}

View File

@ -676,9 +676,9 @@ macro_rules! impl_from_str_traits {
Ok((val, false)) => Ok(val),
Ok((_, true)) => {
if s.starts_with('-') {
Ok(Self::min_value())
Ok(Self::MIN)
} else {
Ok(Self::max_value())
Ok(Self::MAX)
}
}
Err(e) => Err(e),

View File

@ -82,11 +82,7 @@ macro_rules! impl_sealed {
FloatKind::Infinite { neg } => neg,
FloatKind::Finite { neg, .. } => neg,
};
let saturated = if neg {
Self::min_value()
} else {
Self::max_value()
};
let saturated = if neg { Self::MIN } else { Self::MAX };
let conv = match src.kind {
FloatKind::Finite { conv, .. } => conv,
_ => return saturated,
@ -100,7 +96,7 @@ macro_rules! impl_sealed {
Widest::Unsigned(bits) => {
let bits = bits as _;
if bits < 0 {
return Self::max_value();
return Self::MAX;
}
bits
}
@ -108,7 +104,7 @@ macro_rules! impl_sealed {
},
match conv.bits {
Widest::Unsigned(bits) => bits as _,
Widest::Negative(_) => return Self::min_value(),
Widest::Negative(_) => return Self::MIN,
},
};
Self::from_bits(bits)

View File

@ -326,8 +326,8 @@ assert_eq!(Fix::from_num(7.5).rem_euclid_int(2), Fix::from_num(1.5));
```rust
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
assert_eq!(Fix::max_value().checked_mul(Fix::from_num(1)), Some(Fix::max_value()));
assert_eq!(Fix::max_value().checked_mul(Fix::from_num(2)), None);
assert_eq!(Fix::MAX.checked_mul(Fix::from_num(1)), Some(Fix::MAX));
assert_eq!(Fix::MAX.checked_mul(Fix::from_num(2)), None);
```
[`None`]: https://doc.rust-lang.org/nightly/core/option/enum.Option.html#variant.None
@ -350,8 +350,8 @@ the divisor is zero or on overflow.
```rust
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
assert_eq!(Fix::max_value().checked_div(Fix::from_num(1)), Some(Fix::max_value()));
assert_eq!(Fix::max_value().checked_div(Fix::from_num(1) / 2), None);
assert_eq!(Fix::MAX.checked_div(Fix::from_num(1)), Some(Fix::MAX));
assert_eq!(Fix::MAX.checked_div(Fix::from_num(1) / 2), None);
```
[`None`]: https://doc.rust-lang.org/nightly/core/option/enum.Option.html#variant.None
@ -379,7 +379,7 @@ use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
assert_eq!(Fix::from_num(7.5).checked_div_euclid(Fix::from_num(2)), Some(Fix::from_num(3)));
assert_eq!(Fix::from_num(7.5).checked_div_euclid(Fix::from_num(0)), None);
assert_eq!(Fix::max_value().checked_div_euclid(Fix::from_num(0.25)), None);
assert_eq!(Fix::MAX.checked_div_euclid(Fix::from_num(0.25)), None);
",
if_signed_else_empty_str! {
$Signedness,
@ -438,7 +438,7 @@ assert_eq!(Fix::from_num(3.75).checked_rem_int(0), None);
Some(fixed_rhs) => self.checked_rem(fixed_rhs),
None => Some(if_signed_unsigned!(
$Signedness,
if self == Self::min_value()
if self == Self::MIN
&& (Self::INT_NBITS > 0 && rhs == 1 << (Self::INT_NBITS - 1))
{
Self::from_bits(0)
@ -471,7 +471,7 @@ 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);
"assert_eq!(Fix::MIN.checked_div_euclid_int(-1), None);
",
},
"```
@ -563,7 +563,7 @@ assert_eq!(Fix::from_num(-7.5).checked_rem_euclid_int(20), None);
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
assert_eq!(Fix::from_num(3).saturating_mul(Fix::from_num(2)), Fix::from_num(6));
assert_eq!(Fix::max_value().saturating_mul(Fix::from_num(2)), Fix::max_value());
assert_eq!(Fix::MAX.saturating_mul(Fix::from_num(2)), Fix::MAX);
```
";
#[inline]
@ -572,9 +572,9 @@ assert_eq!(Fix::max_value().saturating_mul(Fix::from_num(2)), Fix::max_value());
(ans, false) => Self::from_bits(ans),
(_, true) => {
if (self < 0) != (rhs < 0) {
Self::min_value()
Self::MIN
} else {
Self::max_value()
Self::MAX
}
}
}
@ -595,7 +595,7 @@ use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
let one_half = Fix::from_num(1) / 2;
assert_eq!(Fix::from_num(1).saturating_div(Fix::from_num(2)), one_half);
assert_eq!(Fix::max_value().saturating_div(one_half), Fix::max_value());
assert_eq!(Fix::MAX.saturating_div(one_half), Fix::MAX);
```
";
#[inline]
@ -604,9 +604,9 @@ assert_eq!(Fix::max_value().saturating_div(one_half), Fix::max_value());
(ans, false) => Self::from_bits(ans),
(_, true) => {
if (self < 0) != (rhs < 0) {
Self::min_value()
Self::MIN
} else {
Self::max_value()
Self::MAX
}
}
}
@ -627,12 +627,12 @@ Panics if the divisor is zero.
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
assert_eq!(Fix::from_num(7.5).saturating_div_euclid(Fix::from_num(2)), Fix::from_num(3));
assert_eq!(Fix::max_value().saturating_div_euclid(Fix::from_num(0.25)), Fix::max_value());
assert_eq!(Fix::MAX.saturating_div_euclid(Fix::from_num(0.25)), Fix::MAX);
",
if_signed_else_empty_str! {
$Signedness,
"assert_eq!(Fix::from_num(-7.5).saturating_div_euclid(Fix::from_num(2)), Fix::from_num(-4));
assert_eq!(Fix::min_value().saturating_div_euclid(Fix::from_num(0.25)), Fix::min_value());
assert_eq!(Fix::MIN.saturating_div_euclid(Fix::from_num(0.25)), Fix::MIN);
",
},
"```
@ -646,9 +646,9 @@ assert_eq!(Fix::min_value().saturating_div_euclid(Fix::from_num(0.25)), Fix::min
}
self.checked_div_euclid(rhs).unwrap_or_else(|| {
if (self.to_bits() > 0) == (rhs.to_bits() > 0) {
Self::max_value()
Self::MAX
} else {
Self::min_value()
Self::MIN
}
})
}
@ -664,7 +664,7 @@ use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
assert_eq!(Fix::from_num(3).wrapping_mul(Fix::from_num(2)), Fix::from_num(6));
let wrapped = Fix::from_bits(!0 << 2);
assert_eq!(Fix::max_value().wrapping_mul(Fix::from_num(4)), wrapped);
assert_eq!(Fix::MAX.wrapping_mul(Fix::from_num(4)), wrapped);
```
";
#[inline]
@ -690,7 +690,7 @@ let one_point_5 = Fix::from_bits(0b11 << (4 - 1));
assert_eq!(Fix::from_num(3).wrapping_div(Fix::from_num(2)), one_point_5);
let quarter = Fix::from_num(1) / 4;
let wrapped = Fix::from_bits(!0 << 2);
assert_eq!(Fix::max_value().wrapping_div(quarter), wrapped);
assert_eq!(Fix::MAX.wrapping_div(quarter), wrapped);
```
";
#[inline]
@ -713,8 +713,8 @@ Panics if the divisor is zero.
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
assert_eq!(Fix::from_num(7.5).wrapping_div_euclid(Fix::from_num(2)), Fix::from_num(3));
let wrapped = Fix::max_value().wrapping_mul_int(4).round_to_zero();
assert_eq!(Fix::max_value().wrapping_div_euclid(Fix::from_num(0.25)), wrapped);
let wrapped = Fix::MAX.wrapping_mul_int(4).round_to_zero();
assert_eq!(Fix::MAX.wrapping_div_euclid(Fix::from_num(0.25)), wrapped);
```
";
#[inline]
@ -750,8 +750,8 @@ 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);
let wrapped = Fix::MIN.round_to_zero();
assert_eq!(Fix::MIN.wrapping_div_euclid_int(-1), wrapped);
",
},
"```
@ -815,7 +815,7 @@ use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
assert_eq!(Fix::from_num(3).overflowing_mul(Fix::from_num(2)), (Fix::from_num(6), false));
let wrapped = Fix::from_bits(!0 << 2);
assert_eq!(Fix::max_value().overflowing_mul(Fix::from_num(4)), (wrapped, true));
assert_eq!(Fix::MAX.overflowing_mul(Fix::from_num(4)), (wrapped, true));
```
[`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html
@ -847,7 +847,7 @@ let one_point_5 = Fix::from_bits(0b11 << (4 - 1));
assert_eq!(Fix::from_num(3).overflowing_div(Fix::from_num(2)), (one_point_5, false));
let quarter = Fix::from_num(1) / 4;
let wrapped = Fix::from_bits(!0 << 2);
assert_eq!(Fix::max_value().overflowing_div(quarter), (wrapped, true));
assert_eq!(Fix::MAX.overflowing_div(quarter), (wrapped, true));
```
[`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html
@ -877,8 +877,8 @@ use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
let check = Fix::from_num(3);
assert_eq!(Fix::from_num(7.5).overflowing_div_euclid(Fix::from_num(2)), (check, false));
let wrapped = Fix::max_value().wrapping_mul_int(4).round_to_zero();
assert_eq!(Fix::max_value().overflowing_div_euclid(Fix::from_num(0.25)), (wrapped, true));
let wrapped = Fix::MAX.wrapping_mul_int(4).round_to_zero();
assert_eq!(Fix::MAX.overflowing_div_euclid(Fix::from_num(0.25)), (wrapped, true));
```
[`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html
@ -938,8 +938,8 @@ assert_eq!(Fix::from_num(7.5).overflowing_div_euclid_int(2), (Fix::from_num(3),
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));
let wrapped = Fix::MIN.round_to_zero();
assert_eq!(Fix::MIN.overflowing_div_euclid_int(-1), (wrapped, true));
",
},
"```

View File

@ -183,6 +183,8 @@ assert_eq!(",
}
}
// TODO when rustc requirement >= 1.43.0, use {MIN,MAX}
// instead of {min,max}_value() in example
comment! {
"Creates a fixed-point number from another number if it
fits, otherwise returns [`None`].
@ -213,7 +215,7 @@ type Fix = ", $s_fixed, "<U4>;
// 1.75 is 1.11 in binary
let src = I16F16::from_bits(0b111 << (16 - 2));
assert_eq!(Fix::checked_from_num(src), Some(Fix::from_bits(0b111 << (4 - 2))));
let too_large = ", $s_fixed, "::<U2>::max_value();
let too_large = ", $s_fixed, "::<U2>::MAX;
assert!(Fix::checked_from_num(too_large).is_none());
assert_eq!(Fix::checked_from_num(3), Some(Fix::from_bits(3 << 4)));
@ -302,7 +304,7 @@ let src = Fix::from_bits(0b111 << (4 - 2));
let expected = I16F16::from_bits(0b111 << (16 - 2));
assert_eq!(src.checked_to_num::<I16F16>(), Some(expected));
type TooFewIntBits = ", $s_fixed, "<U6>;
assert!(Fix::max_value().checked_to_num::<TooFewIntBits>().is_none());
assert!(Fix::MAX.checked_to_num::<TooFewIntBits>().is_none());
// 2.5 is 10.1 in binary
let two_point_5 = Fix::from_bits(0b101 << (4 - 1));
@ -319,7 +321,7 @@ assert!(AllInt::",
if_signed_unsigned! {
$Signedness,
"from_bits(-1).checked_to_num::<u",
"max_value().checked_to_num::<i",
"MAX.checked_to_num::<i",
},
$s_nbits, ">().is_none());
@ -355,6 +357,7 @@ assert_eq!(one_point_625.checked_to_num::<f32>(), Some(1.625f32));
}
}
// TODO when rustc requirement >= 1.43.0, use MIN instead of min_value() in example
comment! {
"Creates a fixed-point number from another number,
saturating if it does not fit.
@ -390,8 +393,8 @@ type Fix = ", $s_fixed, "<U4>;
// 1.75 is 1.11 in binary
let src = I16F16::from_bits(0b111 << (16 - 2));
assert_eq!(Fix::saturating_from_num(src), Fix::from_bits(0b111 << (4 - 2)));
let too_large = ", $s_fixed, "::<U2>::max_value();
assert_eq!(Fix::saturating_from_num(too_large), Fix::max_value());
let too_large = ", $s_fixed, "::<U2>::MAX;
assert_eq!(Fix::saturating_from_num(too_large), Fix::MAX);
assert_eq!(Fix::saturating_from_num(3), Fix::from_bits(3 << 4));
let too_small = ",
@ -401,7 +404,7 @@ let too_small = ",
"-1",
},
";
assert_eq!(Fix::saturating_from_num(too_small), Fix::min_value());
assert_eq!(Fix::saturating_from_num(too_small), Fix::MIN);
// 1.75 is 1.11 in binary
let expected = Fix::from_bits(0b111 << (4 - 2));
@ -413,8 +416,8 @@ assert_eq!(Fix::saturating_from_num(",
"1.75f64), ",
},
"expected);
assert_eq!(Fix::saturating_from_num(2e38), Fix::max_value());
assert_eq!(Fix::saturating_from_num(std::f64::NEG_INFINITY), Fix::min_value());
assert_eq!(Fix::saturating_from_num(2e38), Fix::MAX);
assert_eq!(Fix::saturating_from_num(std::f64::NEG_INFINITY), Fix::MIN);
```
[NaN]: https://doc.rust-lang.org/nightly/std/primitive.f64.html#method.is_nan
@ -444,6 +447,7 @@ assert_eq!(Fix::saturating_from_num(std::f64::NEG_INFINITY), Fix::min_value());
}
}
// TODO when rustc requirement >= 1.43.0, use MAX instead of max_value() in example
comment! {
"Converts a fixed-point number to another number,
saturating the value if it does not fit.
@ -477,8 +481,8 @@ let src = Fix::from_bits(0b111 << (4 - 2));
let expected = I16F16::from_bits(0b111 << (16 - 2));
assert_eq!(src.saturating_to_num::<I16F16>(), expected);
type TooFewIntBits = ", $s_fixed, "<U6>;
let saturated = Fix::max_value().saturating_to_num::<TooFewIntBits>();
assert_eq!(saturated, TooFewIntBits::max_value());
let saturated = Fix::MAX.saturating_to_num::<TooFewIntBits>();
assert_eq!(saturated, TooFewIntBits::MAX);
// 2.5 is 10.1 in binary
let two_point_5 = Fix::from_bits(0b101 << (4 - 1));
@ -489,7 +493,7 @@ assert_eq!(",
$Signedness,
concat!("AllInt::from_bits(-1).saturating_to_num::<u", $s_nbits, ">(), 0"),
concat!(
"AllInt::max_value().saturating_to_num::<i", $s_nbits, ">(), ",
"AllInt::MAX.saturating_to_num::<i", $s_nbits, ">(), ",
"i", $s_nbits, "::max_value()",
),
},
@ -606,6 +610,7 @@ assert_eq!(Fix::wrapping_from_num(large), wrapped);
}
}
// TODO when rustc requirement >= 1.43.0, use MAX instead of max_value() in example
comment! {
"Converts a fixed-point number to another number,
wrapping the value on overflow.
@ -639,8 +644,8 @@ let src = Fix::from_bits(0b111 << (4 - 2));
let expected = I16F16::from_bits(0b111 << (16 - 2));
assert_eq!(src.wrapping_to_num::<I16F16>(), expected);
type TooFewIntBits = ", $s_fixed, "<U6>;
let wrapped = TooFewIntBits::from_bits(Fix::max_value().to_bits() << 2);
assert_eq!(Fix::max_value().wrapping_to_num::<TooFewIntBits>(), wrapped);
let wrapped = TooFewIntBits::from_bits(Fix::MAX.to_bits() << 2);
assert_eq!(Fix::MAX.wrapping_to_num::<TooFewIntBits>(), wrapped);
// 2.5 is 10.1 in binary
let two_point_5 = Fix::from_bits(0b101 << (4 - 1));
@ -653,7 +658,7 @@ assert_eq!(",
"AllInt::from_bits(-1).wrapping_to_num::<u", $s_nbits, ">(), ",
"u", $s_nbits, "::max_value()",
),
concat!("AllInt::max_value().wrapping_to_num::<i", $s_nbits, ">(), -1"),
concat!("AllInt::MAX.wrapping_to_num::<i", $s_nbits, ">(), -1"),
},
");
@ -810,14 +815,14 @@ let src = Fix::from_bits(0b111 << (4 - 2));
let expected = I16F16::from_bits(0b111 << (16 - 2));
assert_eq!(src.overflowing_to_num::<I16F16>(), (expected, false));
type TooFewIntBits = ", $s_fixed, "<U6>;
let wrapped = TooFewIntBits::from_bits(Fix::max_value().to_bits() << 2);
assert_eq!(Fix::max_value().overflowing_to_num::<TooFewIntBits>(), (wrapped, true));
let wrapped = TooFewIntBits::from_bits(Fix::MAX.to_bits() << 2);
assert_eq!(Fix::MAX.overflowing_to_num::<TooFewIntBits>(), (wrapped, true));
// 2.5 is 10.1 in binary
let two_point_5 = Fix::from_bits(0b101 << (4 - 1));
assert_eq!(two_point_5.overflowing_to_num::<i32>(), (2, false));
let does_not_fit = ", $s_fixed, "::<U0>::",
if_signed_unsigned! { $Signedness, "from_bits(-1)", "max_value()" },
if_signed_unsigned! { $Signedness, "from_bits(-1)", "MAX" },
";
let wrapped = ",
if_signed_unsigned! {
@ -963,11 +968,11 @@ Rounding is to the nearest, with ties rounded to even.
if_signed_unsigned! {
$Signedness,
"use fixed::types::I8F8;
assert_eq!(I8F8::saturating_from_str(\"9999\"), Ok(I8F8::max_value()));
assert_eq!(I8F8::saturating_from_str(\"-9999\"), Ok(I8F8::min_value()));
assert_eq!(I8F8::saturating_from_str(\"9999\"), Ok(I8F8::MAX));
assert_eq!(I8F8::saturating_from_str(\"-9999\"), Ok(I8F8::MIN));
",
"use fixed::types::U8F8;
assert_eq!(U8F8::saturating_from_str(\"9999\"), Ok(U8F8::max_value()));
assert_eq!(U8F8::saturating_from_str(\"9999\"), Ok(U8F8::MAX));
assert_eq!(U8F8::saturating_from_str(\"-1\"), Ok(U8F8::from_num(0)));
",
},
@ -992,11 +997,11 @@ Rounding is to the nearest, with ties rounded to even.
if_signed_unsigned! {
$Signedness,
"use fixed::types::I8F8;
assert_eq!(I8F8::saturating_from_str_binary(\"101100111000\"), Ok(I8F8::max_value()));
assert_eq!(I8F8::saturating_from_str_binary(\"-101100111000\"), Ok(I8F8::min_value()));
assert_eq!(I8F8::saturating_from_str_binary(\"101100111000\"), Ok(I8F8::MAX));
assert_eq!(I8F8::saturating_from_str_binary(\"-101100111000\"), Ok(I8F8::MIN));
",
"use fixed::types::U8F8;
assert_eq!(U8F8::saturating_from_str_binary(\"101100111000\"), Ok(U8F8::max_value()));
assert_eq!(U8F8::saturating_from_str_binary(\"101100111000\"), Ok(U8F8::MAX));
assert_eq!(U8F8::saturating_from_str_binary(\"-1\"), Ok(U8F8::from_num(0)));
",
},
@ -1021,11 +1026,11 @@ Rounding is to the nearest, with ties rounded to even.
if_signed_unsigned! {
$Signedness,
"use fixed::types::I8F8;
assert_eq!(I8F8::saturating_from_str_octal(\"7777\"), Ok(I8F8::max_value()));
assert_eq!(I8F8::saturating_from_str_octal(\"-7777\"), Ok(I8F8::min_value()));
assert_eq!(I8F8::saturating_from_str_octal(\"7777\"), Ok(I8F8::MAX));
assert_eq!(I8F8::saturating_from_str_octal(\"-7777\"), Ok(I8F8::MIN));
",
"use fixed::types::U8F8;
assert_eq!(U8F8::saturating_from_str_octal(\"7777\"), Ok(U8F8::max_value()));
assert_eq!(U8F8::saturating_from_str_octal(\"7777\"), Ok(U8F8::MAX));
assert_eq!(U8F8::saturating_from_str_octal(\"-1\"), Ok(U8F8::from_num(0)));
",
},
@ -1050,11 +1055,11 @@ Rounding is to the nearest, with ties rounded to even.
if_signed_unsigned! {
$Signedness,
"use fixed::types::I8F8;
assert_eq!(I8F8::saturating_from_str_hex(\"FFFF\"), Ok(I8F8::max_value()));
assert_eq!(I8F8::saturating_from_str_hex(\"-FFFF\"), Ok(I8F8::min_value()));
assert_eq!(I8F8::saturating_from_str_hex(\"FFFF\"), Ok(I8F8::MAX));
assert_eq!(I8F8::saturating_from_str_hex(\"-FFFF\"), Ok(I8F8::MIN));
",
"use fixed::types::U8F8;
assert_eq!(U8F8::saturating_from_str_hex(\"FFFF\"), Ok(U8F8::max_value()));
assert_eq!(U8F8::saturating_from_str_hex(\"FFFF\"), Ok(U8F8::MAX));
assert_eq!(U8F8::saturating_from_str_hex(\"-1\"), Ok(U8F8::from_num(0)));
",
},

View File

@ -21,38 +21,34 @@ macro_rules! fixed_no_frac {
$UInner:ty, $Signedness:tt
) => {
impl<Frac> $Fixed<Frac> {
// TODO when rustc requirement >= 1.43.0, use MIN instead of min_value() in example
comment! {
"Returns the smallest value that can be represented.
"The smallest value that can be represented.
# Examples
```rust
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
assert_eq!(Fix::min_value(), Fix::from_bits(", $s_inner, "::min_value()));
assert_eq!(Fix::MIN, Fix::from_bits(", $s_inner, "::min_value()));
```
";
#[inline]
pub const fn min_value() -> $Fixed<Frac> {
Self::from_bits(<$Inner>::min_value())
}
pub const MIN: Self = Self::from_bits(<$Inner>::min_value());
}
// TODO when rustc requirement >= 1.43.0, use MAX instead of max_value() in example
comment! {
"Returns the largest value that can be represented.
"The largest value that can be represented.
# Examples
```rust
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
assert_eq!(Fix::max_value(), Fix::from_bits(", $s_inner, "::max_value()));
assert_eq!(Fix::MAX, Fix::from_bits(", $s_inner, "::max_value()));
```
";
#[inline]
pub const fn max_value() -> $Fixed<Frac> {
Self::from_bits(<$Inner>::max_value())
}
pub const MAX: Self = Self::from_bits(<$Inner>::max_value());
}
comment! {
@ -524,7 +520,7 @@ type Fix = ", $s_fixed, "<U4>;
if_signed_unsigned! {
$Signedness,
"assert_eq!(Fix::from_num(5).checked_neg(), Some(Fix::from_num(-5)));
assert_eq!(Fix::min_value().checked_neg(), None);",
assert_eq!(Fix::MIN.checked_neg(), None);",
"assert_eq!(Fix::from_num(0).checked_neg(), Some(Fix::from_num(0)));
assert_eq!(Fix::from_num(5).checked_neg(), None);",
},
@ -548,8 +544,8 @@ assert_eq!(Fix::from_num(5).checked_neg(), None);",
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
let one = Fix::from_num(1);
assert_eq!((Fix::max_value() - one).checked_add(one), Some(Fix::max_value()));
assert_eq!(Fix::max_value().checked_add(one), None);
assert_eq!((Fix::MAX - one).checked_add(one), Some(Fix::MAX));
assert_eq!(Fix::MAX.checked_add(one), None);
```
[`None`]: https://doc.rust-lang.org/nightly/core/option/enum.Option.html#variant.None
@ -569,8 +565,8 @@ assert_eq!(Fix::max_value().checked_add(one), None);
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
let one = Fix::from_num(1);
assert_eq!((Fix::min_value() + one).checked_sub(one), Some(Fix::min_value()));
assert_eq!(Fix::min_value().checked_sub(one), None);
assert_eq!((Fix::MIN + one).checked_sub(one), Some(Fix::MIN));
assert_eq!(Fix::MIN.checked_sub(one), None);
```
[`None`]: https://doc.rust-lang.org/nightly/core/option/enum.Option.html#variant.None
@ -624,8 +620,8 @@ product, or [`None`] on overflow.
```rust
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
assert_eq!(Fix::max_value().checked_mul_int(1), Some(Fix::max_value()));
assert_eq!(Fix::max_value().checked_mul_int(2), None);
assert_eq!(Fix::MAX.checked_mul_int(1), Some(Fix::MAX));
assert_eq!(Fix::MAX.checked_mul_int(2), None);
```
[`None`]: https://doc.rust-lang.org/nightly/core/option/enum.Option.html#variant.None
@ -651,12 +647,12 @@ assert_eq!(Fix::max_value().checked_mul_int(2), None);
```rust
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
assert_eq!(Fix::max_value().checked_div_int(1), Some(Fix::max_value()));
assert_eq!(Fix::MAX.checked_div_int(1), Some(Fix::MAX));
assert_eq!(Fix::from_num(1).checked_div_int(0), None);
",
if_signed_else_empty_str! {
$Signedness,
"assert_eq!(Fix::min_value().checked_div_int(-1), None);
"assert_eq!(Fix::MIN.checked_div_int(-1), None);
",
},
"```
@ -765,7 +761,7 @@ Overflow can only occur when trying to find the absolute value of the minimum va
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
assert_eq!(Fix::from_num(-5).checked_abs(), Some(Fix::from_num(5)));
assert_eq!(Fix::min_value().checked_abs(), None);
assert_eq!(Fix::MIN.checked_abs(), None);
```
[`None`]: https://doc.rust-lang.org/nightly/core/option/enum.Option.html#variant.None
@ -793,7 +789,7 @@ 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());
assert!(Fix::MAX.checked_next_power_of_two().is_none());
```
[`None`]: https://doc.rust-lang.org/nightly/core/option/enum.Option.html#variant.None
@ -825,7 +821,7 @@ type Fix = ", $s_fixed, "<U4>;
if_signed_unsigned! {
$Signedness,
"assert_eq!(Fix::from_num(5).saturating_neg(), Fix::from_num(-5));
assert_eq!(Fix::min_value().saturating_neg(), Fix::max_value());",
assert_eq!(Fix::MIN.saturating_neg(), Fix::MAX);",
"assert_eq!(Fix::from_num(0).saturating_neg(), Fix::from_num(0));
assert_eq!(Fix::from_num(5).saturating_neg(), Fix::from_num(0));",
},
@ -838,7 +834,7 @@ assert_eq!(Fix::from_num(5).saturating_neg(), Fix::from_num(0));",
$Signedness,
{
let (val, overflow) = self.overflowing_neg();
val.if_cond_else(!overflow, Self::max_value())
val.if_cond_else(!overflow, Self::MAX)
},
Self::from_bits(0),
}
@ -854,7 +850,7 @@ assert_eq!(Fix::from_num(5).saturating_neg(), Fix::from_num(0));",
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
assert_eq!(Fix::from_num(3).saturating_add(Fix::from_num(2)), Fix::from_num(5));
assert_eq!(Fix::max_value().saturating_add(Fix::from_num(1)), Fix::max_value());
assert_eq!(Fix::MAX.saturating_add(Fix::from_num(1)), Fix::MAX);
```
";
#[inline]
@ -864,8 +860,8 @@ assert_eq!(Fix::max_value().saturating_add(Fix::from_num(1)), Fix::max_value());
!overflow,
if_signed_unsigned! {
$Signedness,
Self::min_value().if_cond_else(self.to_bits() < 0, Self::max_value()),
Self::max_value(),
Self::MIN.if_cond_else(self.to_bits() < 0, Self::MAX),
Self::MAX,
},
)
}
@ -883,7 +879,7 @@ type Fix = ", $s_fixed, "<U4>;
if_signed_unsigned! {
$Signedness,
"assert_eq!(Fix::from_num(1).saturating_sub(Fix::from_num(3)), Fix::from_num(-2));
assert_eq!(Fix::min_value().saturating_sub(Fix::from_num(1)), Fix::min_value());",
assert_eq!(Fix::MIN.saturating_sub(Fix::from_num(1)), Fix::MIN);",
"assert_eq!(Fix::from_num(5).saturating_sub(Fix::from_num(3)), Fix::from_num(2));
assert_eq!(Fix::from_num(0).saturating_sub(Fix::from_num(1)), Fix::from_num(0));",
},
@ -897,11 +893,11 @@ assert_eq!(Fix::from_num(0).saturating_sub(Fix::from_num(1)), Fix::from_num(0));
!overflow,
if_signed_unsigned! {
$Signedness,
Self::min_value().if_cond_else(
Self::MIN.if_cond_else(
self.to_bits() < rhs.to_bits(),
Self::max_value(),
Self::MAX,
),
Self::min_value(),
Self::MIN,
},
)
}
@ -916,7 +912,7 @@ assert_eq!(Fix::from_num(0).saturating_sub(Fix::from_num(1)), Fix::from_num(0));
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
assert_eq!(Fix::from_num(3).saturating_mul_int(2), Fix::from_num(6));
assert_eq!(Fix::max_value().saturating_mul_int(2), Fix::max_value());
assert_eq!(Fix::MAX.saturating_mul_int(2), Fix::MAX);
```
";
#[inline]
@ -926,11 +922,11 @@ assert_eq!(Fix::max_value().saturating_mul_int(2), Fix::max_value());
!overflow,
if_signed_unsigned! {
$Signedness,
Self::min_value().if_cond_else(
Self::MIN.if_cond_else(
(self.to_bits() < 0) != (rhs < 0),
Self::max_value(),
Self::MAX,
),
Self::max_value(),
Self::MAX,
},
)
}
@ -949,13 +945,13 @@ Overflow can only occur when trying to find the absolute value of the minimum va
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
assert_eq!(Fix::from_num(-5).saturating_abs(), Fix::from_num(5));
assert_eq!(Fix::min_value().saturating_abs(), Fix::max_value());
assert_eq!(Fix::MIN.saturating_abs(), Fix::MAX);
```
";
#[inline]
pub const fn saturating_abs(self) -> $Fixed<Frac> {
let (val, overflow) = self.overflowing_abs();
val.if_cond_else(!overflow, Self::max_value())
val.if_cond_else(!overflow, Self::MAX)
}
}
}
@ -980,7 +976,7 @@ type Fix = ", $s_fixed, "<U4>;
if_signed_unsigned! {
$Signedness,
"assert_eq!(Fix::from_num(5).wrapping_neg(), Fix::from_num(-5));
assert_eq!(Fix::min_value().wrapping_neg(), Fix::min_value());",
assert_eq!(Fix::MIN.wrapping_neg(), Fix::MIN);",
"assert_eq!(Fix::from_num(0).wrapping_neg(), Fix::from_num(0));
assert_eq!(Fix::from_num(5).wrapping_neg(), Fix::wrapping_from_num(-5));
let neg_five_bits = !Fix::from_num(5).to_bits() + 1;
@ -1006,8 +1002,8 @@ type Fix = ", $s_fixed, "<U4>;
let one = Fix::from_num(1);
let one_minus_bit = one - Fix::from_bits(1);
assert_eq!(Fix::from_num(3).wrapping_add(Fix::from_num(2)), Fix::from_num(5));
assert_eq!(Fix::max_value().wrapping_add(one), ",
if_signed_else_empty_str! { $Signedness, "Fix::min_value() + " },
assert_eq!(Fix::MAX.wrapping_add(one), ",
if_signed_else_empty_str! { $Signedness, "Fix::MIN + " },
"one_minus_bit);
```
";
@ -1031,11 +1027,11 @@ let one_minus_bit = one - Fix::from_bits(1);
if_signed_unsigned! {
$Signedness,
"assert_eq!(Fix::from_num(3).wrapping_sub(Fix::from_num(5)), Fix::from_num(-2));
assert_eq!(Fix::min_value()",
assert_eq!(Fix::MIN",
"assert_eq!(Fix::from_num(5).wrapping_sub(Fix::from_num(3)), Fix::from_num(2));
assert_eq!(Fix::from_num(0)",
},
".wrapping_sub(one), Fix::max_value() - one_minus_bit);
".wrapping_sub(one), Fix::MAX - one_minus_bit);
```
";
#[inline]
@ -1054,7 +1050,7 @@ use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
assert_eq!(Fix::from_num(3).wrapping_mul_int(2), Fix::from_num(6));
let wrapped = Fix::from_bits(!0 << 2);
assert_eq!(Fix::max_value().wrapping_mul_int(4), wrapped);
assert_eq!(Fix::MAX.wrapping_mul_int(4), wrapped);
```
";
#[inline]
@ -1091,7 +1087,7 @@ assert_eq!(Fix::from_num(3).wrapping_div_int(2), one_point_5);
",
if_signed_else_empty_str! {
$Signedness,
"assert_eq!(Fix::min_value().wrapping_div_int(-1), Fix::min_value());
"assert_eq!(Fix::MIN.wrapping_div_int(-1), Fix::MIN);
",
},
"```
@ -1153,7 +1149,7 @@ Overflow can only occur when trying to find the absolute value of the minimum va
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
assert_eq!(Fix::from_num(-5).wrapping_abs(), Fix::from_num(5));
assert_eq!(Fix::min_value().wrapping_abs(), Fix::min_value());
assert_eq!(Fix::MIN.wrapping_abs(), Fix::MIN);
```
";
#[inline]
@ -1186,7 +1182,7 @@ type Fix = ", $s_fixed, "<U4>;
if_signed_unsigned! {
$Signedness,
"assert_eq!(Fix::from_num(5).overflowing_neg(), (Fix::from_num(-5), false));
assert_eq!(Fix::min_value().overflowing_neg(), (Fix::min_value(), true));",
assert_eq!(Fix::MIN.overflowing_neg(), (Fix::MIN, true));",
"assert_eq!(Fix::from_num(0).overflowing_neg(), (Fix::from_num(0), false));
assert_eq!(Fix::from_num(5).overflowing_neg(), Fix::overflowing_from_num(-5));
let neg_five_bits = !Fix::from_num(5).to_bits() + 1;
@ -1219,8 +1215,8 @@ type Fix = ", $s_fixed, "<U4>;
let one = Fix::from_num(1);
let one_minus_bit = one - Fix::from_bits(1);
assert_eq!(Fix::from_num(3).overflowing_add(Fix::from_num(2)), (Fix::from_num(5), false));
assert_eq!(Fix::max_value().overflowing_add(one), (",
if_signed_else_empty_str! { $Signedness, "Fix::min_value() + " },
assert_eq!(Fix::MAX.overflowing_add(one), (",
if_signed_else_empty_str! { $Signedness, "Fix::MIN + " },
"one_minus_bit, true));
```
@ -1251,11 +1247,11 @@ let one_minus_bit = one - Fix::from_bits(1);
if_signed_unsigned! {
$Signedness,
"assert_eq!(Fix::from_num(3).overflowing_sub(Fix::from_num(5)), (Fix::from_num(-2), false));
assert_eq!(Fix::min_value()",
assert_eq!(Fix::MIN",
"assert_eq!(Fix::from_num(5).overflowing_sub(Fix::from_num(3)), (Fix::from_num(2), false));
assert_eq!(Fix::from_num(0)",
},
".overflowing_sub(one), (Fix::max_value() - one_minus_bit, true));
".overflowing_sub(one), (Fix::MAX - one_minus_bit, true));
```
[`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html
@ -1281,7 +1277,7 @@ use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
assert_eq!(Fix::from_num(3).overflowing_mul_int(2), (Fix::from_num(6), false));
let wrapped = Fix::from_bits(!0 << 2);
assert_eq!(Fix::max_value().overflowing_mul_int(4), (wrapped, true));
assert_eq!(Fix::MAX.overflowing_mul_int(4), (wrapped, true));
```
[`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html
@ -1322,7 +1318,7 @@ assert_eq!(Fix::from_num(3).overflowing_div_int(2), (one_point_5, false));
",
if_signed_else_empty_str! {
$Signedness,
"assert_eq!(Fix::min_value().overflowing_div_int(-1), (Fix::min_value(), true));
"assert_eq!(Fix::MIN.overflowing_div_int(-1), (Fix::MIN, true));
",
},
"```
@ -1407,7 +1403,7 @@ Overflow can only occur when trying to find the absolute value of the minimum va
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
assert_eq!(Fix::from_num(-5).overflowing_abs(), (Fix::from_num(5), false));
assert_eq!(Fix::min_value().overflowing_abs(), (Fix::min_value(), true));
assert_eq!(Fix::MIN.overflowing_abs(), (Fix::MIN, true));
```
[`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html
@ -1426,6 +1422,20 @@ assert_eq!(Fix::min_value().overflowing_abs(), (Fix::min_value(), true));
let not_mask = (cond as $Inner).wrapping_sub(1);
Self::from_bits((self.to_bits() & !not_mask) | (otherwise.to_bits() & not_mask))
}
/// Returns the smallest value that can be represented.
#[inline]
#[deprecated(since = "0.5.5", note = "replaced by `MIN`")]
pub const fn min_value() -> $Fixed<Frac> {
Self::MIN
}
/// Returns the largest value that can be represented.
#[inline]
#[deprecated(since = "0.5.5", note = "replaced by `MAX`")]
pub const fn max_value() -> $Fixed<Frac> {
Self::MAX
}
}
};
}

View File

@ -315,7 +315,7 @@ assert_eq!(Fix::from_num(2.5).checked_ceil(), Some(Fix::from_num(3)));
"assert_eq!(Fix::from_num(-2.5).checked_ceil(), Some(Fix::from_num(-2)));
",
},
"assert!(Fix::max_value().checked_ceil().is_none());
"assert!(Fix::MAX.checked_ceil().is_none());
```
[`None`]: https://doc.rust-lang.org/nightly/core/option/enum.Option.html#variant.None
@ -360,7 +360,7 @@ assert_eq!(Fix::from_num(2.5).checked_floor(), Some(Fix::from_num(2)));
$Signedness,
"assert_eq!(Fix::from_num(-2.5).checked_floor(), Some(Fix::from_num(-3)));
type AllFrac = ", $s_fixed, "<U", $s_nbits, ">;
assert!(AllFrac::min_value().checked_floor().is_none());
assert!(AllFrac::MIN.checked_floor().is_none());
",
},
"```
@ -397,7 +397,7 @@ assert_eq!(Fix::from_num(2.5).checked_round(), Some(Fix::from_num(3)));
"assert_eq!(Fix::from_num(-2.5).checked_round(), Some(Fix::from_num(-3)));
",
},
"assert!(Fix::max_value().checked_round().is_none());
"assert!(Fix::MAX.checked_round().is_none());
```
[`None`]: https://doc.rust-lang.org/nightly/core/option/enum.Option.html#variant.None
@ -420,7 +420,7 @@ use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
assert_eq!(Fix::from_num(2.5).checked_round_ties_to_even(), Some(Fix::from_num(2)));
assert_eq!(Fix::from_num(3.5).checked_round_ties_to_even(), Some(Fix::from_num(4)));
assert!(Fix::max_value().checked_round_ties_to_even().is_none());
assert!(Fix::MAX.checked_round_ties_to_even().is_none());
```
[`None`]: https://doc.rust-lang.org/nightly/core/option/enum.Option.html#variant.None
@ -448,13 +448,13 @@ assert_eq!(Fix::from_num(2.5).saturating_ceil(), Fix::from_num(3));
"assert_eq!(Fix::from_num(-2.5).saturating_ceil(), Fix::from_num(-2));
",
},
"assert_eq!(Fix::max_value().saturating_ceil(), Fix::max_value());
"assert_eq!(Fix::MAX.saturating_ceil(), Fix::MAX);
```
";
#[inline]
pub fn saturating_ceil(self) -> $Fixed<Frac> {
let (ceil, overflow) = self.overflowing_ceil();
if overflow { Self::max_value() } else { ceil }
if overflow { Self::MAX } else { ceil }
}
}
@ -491,7 +491,7 @@ assert_eq!(Fix::from_num(2.5).saturating_floor(), Fix::from_num(2));
$Signedness,
"assert_eq!(Fix::from_num(-2.5).saturating_floor(), Fix::from_num(-3));
type AllFrac = ", $s_fixed, "<U", $s_nbits, ">;
assert_eq!(AllFrac::min_value().saturating_floor(), AllFrac::min_value());
assert_eq!(AllFrac::MIN.saturating_floor(), AllFrac::MIN);
",
},
"```
@ -499,7 +499,7 @@ assert_eq!(AllFrac::min_value().saturating_floor(), AllFrac::min_value());
#[inline]
pub fn saturating_floor(self) -> $Fixed<Frac> {
let (floor, overflow) = self.overflowing_floor();
if overflow { Self::min_value() } else { floor }
if overflow { Self::MIN } else { floor }
}
}
@ -519,15 +519,15 @@ assert_eq!(Fix::from_num(2.5).saturating_round(), Fix::from_num(3));
"assert_eq!(Fix::from_num(-2.5).saturating_round(), Fix::from_num(-3));
",
},
"assert_eq!(Fix::max_value().saturating_round(), Fix::max_value());
"assert_eq!(Fix::MAX.saturating_round(), Fix::MAX);
```
";
#[inline]
pub fn saturating_round(self) -> $Fixed<Frac> {
let saturated = if self.to_bits() > 0 {
$Fixed::max_value()
$Fixed::MAX
} else {
$Fixed::min_value()
$Fixed::MIN
};
let (round, overflow) = self.overflowing_round();
if overflow { saturated } else { round }
@ -545,15 +545,15 @@ use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
assert_eq!(Fix::from_num(2.5).saturating_round_ties_to_even(), Fix::from_num(2));
assert_eq!(Fix::from_num(3.5).saturating_round_ties_to_even(), Fix::from_num(4));
assert_eq!(Fix::max_value().saturating_round_ties_to_even(), Fix::max_value());
assert_eq!(Fix::MAX.saturating_round_ties_to_even(), Fix::MAX);
```
";
#[inline]
pub fn saturating_round_ties_to_even(self) -> $Fixed<Frac> {
let saturated = if self.to_bits() > 0 {
$Fixed::max_value()
$Fixed::MAX
} else {
$Fixed::min_value()
$Fixed::MIN
};
let (round, overflow) = self.overflowing_round_ties_to_even();
if overflow { saturated } else { round }
@ -576,7 +576,7 @@ assert_eq!(Fix::from_num(2.5).wrapping_ceil(), Fix::from_num(3));
"assert_eq!(Fix::from_num(-2.5).wrapping_ceil(), Fix::from_num(-2));
",
},
"assert_eq!(Fix::max_value().wrapping_ceil(), Fix::min_value());
"assert_eq!(Fix::MAX.wrapping_ceil(), Fix::MIN);
```
";
#[inline]
@ -618,7 +618,7 @@ assert_eq!(Fix::from_num(2.5).wrapping_floor(), Fix::from_num(2));
$Signedness,
"assert_eq!(Fix::from_num(-2.5).wrapping_floor(), Fix::from_num(-3));
type AllFrac = ", $s_fixed, "<U", $s_nbits, ">;
assert_eq!(AllFrac::min_value().wrapping_floor(), AllFrac::from_num(0));
assert_eq!(AllFrac::MIN.wrapping_floor(), AllFrac::from_num(0));
",
},
"```
@ -645,7 +645,7 @@ assert_eq!(Fix::from_num(2.5).wrapping_round(), Fix::from_num(3));
"assert_eq!(Fix::from_num(-2.5).wrapping_round(), Fix::from_num(-3));
",
},
"assert_eq!(Fix::max_value().wrapping_round(), Fix::min_value());
"assert_eq!(Fix::MAX.wrapping_round(), Fix::MIN);
```
";
#[inline]
@ -665,7 +665,7 @@ use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
assert_eq!(Fix::from_num(2.5).wrapping_round_ties_to_even(), Fix::from_num(2));
assert_eq!(Fix::from_num(3.5).wrapping_round_ties_to_even(), Fix::from_num(4));
assert_eq!(Fix::max_value().wrapping_round_ties_to_even(), Fix::min_value());
assert_eq!(Fix::MAX.wrapping_round_ties_to_even(), Fix::MIN);
```
";
#[inline]
@ -693,7 +693,7 @@ assert_eq!(Fix::from_num(2.5).overflowing_ceil(), (Fix::from_num(3), false));
"assert_eq!(Fix::from_num(-2.5).overflowing_ceil(), (Fix::from_num(-2), false));
"
},
"assert_eq!(Fix::max_value().overflowing_ceil(), (Fix::min_value(), true));
"assert_eq!(Fix::MAX.overflowing_ceil(), (Fix::MIN, true));
```
[`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html
@ -756,7 +756,7 @@ assert_eq!(Fix::from_num(2.5).overflowing_floor(), (Fix::from_num(2), false));
$Signedness,
"assert_eq!(Fix::from_num(-2.5).overflowing_floor(), (Fix::from_num(-3), false));
type AllFrac = ", $s_fixed, "<U", $s_nbits, ">;
assert_eq!(AllFrac::min_value().overflowing_floor(), (AllFrac::from_num(0), true));
assert_eq!(AllFrac::MIN.overflowing_floor(), (AllFrac::from_num(0), true));
",
},
"```
@ -798,7 +798,7 @@ assert_eq!(Fix::from_num(2.5).overflowing_round(), (Fix::from_num(3), false));
"assert_eq!(Fix::from_num(-2.5).overflowing_round(), (Fix::from_num(-3), false));
",
},
"assert_eq!(Fix::max_value().overflowing_round(), (Fix::min_value(), true));
"assert_eq!(Fix::MAX.overflowing_round(), (Fix::MIN, true));
```
[`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html
@ -857,7 +857,7 @@ use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
assert_eq!(Fix::from_num(2.5).overflowing_round_ties_to_even(), (Fix::from_num(2), false));
assert_eq!(Fix::from_num(3.5).overflowing_round_ties_to_even(), (Fix::from_num(4), false));
assert_eq!(Fix::max_value().overflowing_round_ties_to_even(), (Fix::min_value(), true));
assert_eq!(Fix::MAX.overflowing_round_ties_to_even(), (Fix::MIN, true));
```
[`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html

View File

@ -246,11 +246,11 @@ where
/// [`frac_nbits`]: #tymethod.frac_nbits
type Frac: Unsigned;
/// Returns the smallest value that can be represented.
fn min_value() -> Self;
/// The smallest value that can be represented.
const MIN: Self;
/// Returns the largest value that can be represented.
fn max_value() -> Self;
/// The largest value that can be represented.
const MAX: Self;
/// Returns the number of integer bits.
fn int_nbits() -> u32;
@ -985,6 +985,18 @@ where
/// [tuple]: https://doc.rust-lang.org/nightly/std/primitive.tuple.html
fn overflowing_shr(self, rhs: u32) -> (Self, bool);
/// Returns the smallest value that can be represented.
#[deprecated(since = "0.5.5", note = "replaced by `MIN`")]
fn min_value() -> Self {
Self::MIN
}
/// Returns the largest value that can be represented.
#[deprecated(since = "0.5.5", note = "replaced by `MAX`")]
fn max_value() -> Self {
Self::MAX
}
/// Remainder for division by an integer.
///
/// # Panics
@ -1173,6 +1185,7 @@ where
}
}
// TODO when rustc requirement >= 1.43.0, use MAX instead of max_value() in example
/// This trait provides checked conversions from fixed-point numbers.
///
/// This trait is implemented for conversions between integer
@ -1261,7 +1274,7 @@ pub trait FromFixed {
/// let checked: Option<U8F8> = too_large.checked_to_num();
/// assert_eq!(checked, None);
/// let saturating: U8F8 = too_large.saturating_to_num();
/// assert_eq!(saturating, U8F8::max_value());
/// assert_eq!(saturating, U8F8::MAX);
/// let wrapping: U8F8 = too_large.wrapping_to_num();
/// assert_eq!(wrapping, U8F8::from_bits(0x3456));
/// let overflowing: (U8F8, bool) = too_large.overflowing_to_num();
@ -1747,8 +1760,8 @@ macro_rules! impl_fixed {
type Bits = $Bits;
type Bytes = [u8; mem::size_of::<$Bits>()];
type Frac = Frac;
trait_delegate! { fn min_value() -> Self }
trait_delegate! { fn max_value() -> Self }
const MIN: Self = Self::MIN;
const MAX: Self = Self::MAX;
trait_delegate! { fn int_nbits() -> u32 }
trait_delegate! { fn frac_nbits() -> u32 }
trait_delegate! { fn from_bits(bits: Self::Bits) -> Self }
@ -1923,18 +1936,14 @@ macro_rules! impl_fixed {
fn saturating_from_fixed<F: Fixed>(src: F) -> Self {
let conv = src.private_to_fixed_helper(Self::FRAC_NBITS, Self::INT_NBITS);
if conv.overflow {
return if src < 0 {
Self::min_value()
} else {
Self::max_value()
};
return if src < 0 { Self::MIN } else { Self::MAX };
}
let bits = if_signed_unsigned! {
$Signedness,
match conv.bits {
Widest::Unsigned(bits) => {
if (bits as $Bits) < 0 {
return Self::max_value();
return Self::MAX;
}
bits as $Bits
}
@ -1943,7 +1952,7 @@ macro_rules! impl_fixed {
match conv.bits {
Widest::Unsigned(bits) => bits as $Bits,
Widest::Negative(_) => {
return Self::min_value();
return Self::MIN;
}
},
};

View File

@ -42,40 +42,34 @@ use core::{
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// let max = Wrapping(I16F16::max_value());
/// let max = Wrapping(I16F16::MAX);
/// let delta = Wrapping(I16F16::from_bits(1));
/// assert_eq!(I16F16::min_value(), (max + delta).0);
/// assert_eq!(I16F16::MIN, (max + delta).0);
/// ```
#[repr(transparent)]
#[derive(Clone, Copy, Default, Hash, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub struct Wrapping<F>(pub F);
impl<F: Fixed> Wrapping<F> {
/// Returns the smallest value that can be represented.
/// The smallest value that can be represented.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// assert_eq!(Wrapping::<I16F16>::min_value(), Wrapping(I16F16::min_value()));
/// assert_eq!(Wrapping::<I16F16>::MIN, Wrapping(I16F16::MIN));
/// ```
#[inline]
pub fn min_value() -> Wrapping<F> {
Wrapping(F::min_value())
}
pub const MIN: Wrapping<F> = Wrapping(F::MIN);
/// Returns the largest value that can be represented.
/// The largest value that can be represented.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// assert_eq!(Wrapping::<I16F16>::max_value(), Wrapping(I16F16::max_value()));
/// assert_eq!(Wrapping::<I16F16>::MAX, Wrapping(I16F16::MAX));
/// ```
#[inline]
pub fn max_value() -> Wrapping<F> {
Wrapping(F::max_value())
}
pub const MAX: Wrapping<F> = Wrapping(F::MAX);
/// Returns the number of integer bits.
///
@ -233,8 +227,8 @@ impl<F: Fixed> Wrapping<F> {
/// assert_eq!(src.to_num::<I16F16>(), expected);
///
/// // conversion that wraps
/// let src = Wrapping(I4F4::max_value());
/// let wrapped = I2F6::from_bits(I2F6::max_value().to_bits() << 2);
/// let src = Wrapping(I4F4::MAX);
/// let wrapped = I2F6::from_bits(I2F6::MAX.to_bits() << 2);
/// assert_eq!(src.to_num::<I2F6>(), wrapped);
/// ```
///
@ -382,7 +376,7 @@ impl<F: Fixed> Wrapping<F> {
/// use fixed::{types::I16F16, Wrapping};
/// let two_half = Wrapping(I16F16::from_num(5) / 2);
/// assert_eq!(two_half.ceil(), Wrapping(I16F16::from_num(3)));
/// assert_eq!(Wrapping(I16F16::max_value()).ceil(), Wrapping(I16F16::min_value()));
/// assert_eq!(Wrapping(I16F16::MAX).ceil(), Wrapping(I16F16::MIN));
/// ```
#[inline]
pub fn ceil(self) -> Wrapping<F> {
@ -404,7 +398,7 @@ impl<F: Fixed> Wrapping<F> {
/// };
/// let two_half = Wrapping(I16F16::from_num(5) / 2);
/// assert_eq!(two_half.floor(), Wrapping(I16F16::from_num(2)));
/// assert_eq!(Wrapping(I0F32::min_value()).floor(), Wrapping(I0F32::from_num(0)));
/// assert_eq!(Wrapping(I0F32::MIN).floor(), Wrapping(I0F32::from_num(0)));
/// ```
#[inline]
pub fn floor(self) -> Wrapping<F> {
@ -421,7 +415,7 @@ impl<F: Fixed> Wrapping<F> {
/// let two_half = Wrapping(I16F16::from_num(5) / 2);
/// assert_eq!(two_half.round(), Wrapping(I16F16::from_num(3)));
/// assert_eq!((-two_half).round(), Wrapping(I16F16::from_num(-3)));
/// assert_eq!(Wrapping(I16F16::max_value()).round(), Wrapping(I16F16::min_value()));
/// assert_eq!(Wrapping(I16F16::MAX).round(), Wrapping(I16F16::MIN));
/// ```
#[inline]
pub fn round(self) -> Wrapping<F> {
@ -439,8 +433,8 @@ impl<F: Fixed> Wrapping<F> {
/// assert_eq!(two_half.round_ties_to_even(), Wrapping(I16F16::from_num(2)));
/// let three_half = Wrapping(I16F16::from_num(3.5));
/// assert_eq!(three_half.round_ties_to_even(), Wrapping(I16F16::from_num(4)));
/// let max = Wrapping(I16F16::max_value());
/// assert_eq!(max.round_ties_to_even(), Wrapping(I16F16::min_value()));
/// let max = Wrapping(I16F16::MAX);
/// assert_eq!(max.round_ties_to_even(), Wrapping(I16F16::MIN));
/// ```
#[inline]
pub fn round_ties_to_even(self) -> Wrapping<F> {
@ -555,8 +549,8 @@ impl<F: Fixed> Wrapping<F> {
/// let den = Wrapping(I16F16::from_num(2));
/// assert_eq!(num.div_euclid(den), Wrapping(I16F16::from_num(3)));
/// let quarter = Wrapping(I16F16::from_num(0.25));
/// let check = (Wrapping::max_value() * 4i32).round_to_zero();
/// assert_eq!(Wrapping::max_value().div_euclid(quarter), check);
/// let check = (Wrapping::MAX * 4i32).round_to_zero();
/// assert_eq!(Wrapping::MAX.div_euclid(quarter), check);
/// ```
#[inline]
pub fn div_euclid(self, divisor: Wrapping<F>) -> Wrapping<F> {
@ -595,7 +589,7 @@ impl<F: Fixed> Wrapping<F> {
/// use fixed::{types::I16F16, Wrapping};
/// let num = Wrapping(I16F16::from_num(7.5));
/// assert_eq!(num.div_euclid_int(2), Wrapping(I16F16::from_num(3)));
/// let min = Wrapping(I16F16::min_value());
/// let min = Wrapping(I16F16::MIN);
/// assert_eq!(min.div_euclid_int(-1), min);
/// ```
#[inline]
@ -621,6 +615,20 @@ impl<F: Fixed> Wrapping<F> {
pub fn rem_euclid_int(self, divisor: F::Bits) -> Wrapping<F> {
Wrapping(self.0.wrapping_rem_euclid_int(divisor))
}
/// Returns the smallest value that can be represented.
#[deprecated(since = "0.5.5", note = "replaced by `MIN`")]
#[inline]
pub fn min_value() -> Wrapping<F> {
Self::MIN
}
/// Returns the largest value that can be represented.
#[inline]
#[deprecated(since = "0.5.5", note = "replaced by `MAX`")]
pub fn max_value() -> Wrapping<F> {
Self::MAX
}
}
impl<F: FixedSigned> Wrapping<F> {
@ -669,7 +677,7 @@ impl<F: FixedSigned> Wrapping<F> {
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// assert_eq!(Wrapping(I16F16::from_num(-5)).abs(), Wrapping(I16F16::from_num(5)));
/// assert_eq!(Wrapping(I16F16::min_value()).abs(), Wrapping(I16F16::min_value()));
/// assert_eq!(Wrapping(I16F16::MIN).abs(), Wrapping(I16F16::MIN));
/// ```
#[inline]
pub fn abs(self) -> Wrapping<F> {
@ -749,7 +757,7 @@ impl<F: FixedUnsigned> Wrapping<F> {
/// let four = Wrapping(U16F16::from_num(4));
/// assert_eq!(Wrapping(U16F16::from_num(4)).next_power_of_two(), four);
/// let zero = Wrapping(U16F16::from_num(0));
/// assert_eq!(Wrapping(U16F16::max_value()).next_power_of_two(), zero);
/// assert_eq!(Wrapping(U16F16::MAX).next_power_of_two(), zero);
/// ```
#[inline]
pub fn next_power_of_two(self) -> Wrapping<F> {