use ZERO, DELTA, ONE constants in doc examples

This commit is contained in:
Trevor Spiteri 2021-04-02 21:47:37 +02:00
parent 00fe0d9268
commit 91e9d4b1ee
7 changed files with 116 additions and 123 deletions

View File

@ -585,12 +585,11 @@ fixed! {
///
/// ```rust
/// use fixed::{types::I16F16, F128Bits};
/// let one_fixed = I16F16::from_num(1);
/// // binary128 representation for 1.0 is 0x3FFF << 112
/// let one = F128Bits(0x3FFF_u128 << 112);
///
/// assert_eq!(one_fixed.to_num::<F128Bits>(), one);
/// assert_eq!(I16F16::from_num(one), one_fixed);
/// assert_eq!(I16F16::ONE.to_num::<F128Bits>(), one);
/// assert_eq!(I16F16::from_num(one), I16F16::ONE);
///
/// // fixed-point numbers can be compared directly to F128Bits values
/// assert!(I16F16::from_num(1.5) > one);

View File

@ -127,7 +127,7 @@ Returns the logarithm or [`None`] if the fixed-point number is
```rust
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
assert_eq!(Fix::from_num(0).checked_int_log2(), None);
assert_eq!(Fix::ZERO.checked_int_log2(), None);
assert_eq!(Fix::from_num(4).checked_int_log2(), Some(2));
assert_eq!(Fix::from_num(3.9375).checked_int_log2(), Some(1));
assert_eq!(Fix::from_num(0.25).checked_int_log2(), Some(-2));
@ -156,7 +156,7 @@ use fixed::{
types::extra::{U2, U6},
", $s_fixed, ",
};
assert_eq!(", $s_fixed, "::<U2>::from_num(0).checked_int_log10(), None);
assert_eq!(", $s_fixed, "::<U2>::ZERO.checked_int_log10(), None);
assert_eq!(", $s_fixed, "::<U2>::from_num(10).checked_int_log10(), Some(1));
assert_eq!(", $s_fixed, "::<U2>::from_num(9.75).checked_int_log10(), Some(0));
assert_eq!(", $s_fixed, "::<U6>::from_num(0.109375).checked_int_log10(), Some(-1));
@ -204,7 +204,7 @@ represented is almost certainly a bug.
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
assert_eq!(Fix::from_num(5).signum(), 1);
assert_eq!(Fix::from_num(0).signum(), 0);
assert_eq!(Fix::ZERO.signum(), 0);
assert_eq!(Fix::from_num(-5).signum(), -1);
```
";
@ -397,8 +397,8 @@ use fixed::{
", $s_fixed, ",
};
type Fix = ", $s_fixed, "<U4>;
assert_eq!(Fix::from_num(5).checked_signum(), Some(Fix::from_num(1)));
assert_eq!(Fix::from_num(0).checked_signum(), Some(Fix::from_num(0)));
assert_eq!(Fix::from_num(5).checked_signum(), Some(Fix::ONE));
assert_eq!(Fix::ZERO.checked_signum(), Some(Fix::ZERO));
assert_eq!(Fix::from_num(-5).checked_signum(), Some(Fix::from_num(-1)));
type OneIntBit = ", $s_fixed, "<U", $s_nbits_m1, ">;
@ -427,7 +427,7 @@ assert_eq!(ZeroIntBits::from_num(-0.5).checked_signum(), None);
```rust
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
assert_eq!(Fix::MAX.checked_mul(Fix::from_num(1)), Some(Fix::MAX));
assert_eq!(Fix::MAX.checked_mul(Fix::ONE), Some(Fix::MAX));
assert_eq!(Fix::MAX.checked_mul(Fix::from_num(2)), None);
```
";
@ -450,8 +450,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.checked_div(Fix::from_num(1)), Some(Fix::MAX));
assert_eq!(Fix::MAX.checked_div(Fix::from_num(1) / 2), None);
assert_eq!(Fix::MAX.checked_div(Fix::ONE), Some(Fix::MAX));
assert_eq!(Fix::MAX.checked_div(Fix::ONE / 2), None);
```
";
#[inline]
@ -477,7 +477,7 @@ assert_eq!(Fix::MAX.checked_div(Fix::from_num(1) / 2), None);
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
assert_eq!(Fix::from_num(2).checked_recip(), Some(Fix::from_num(0.5)));
assert_eq!(Fix::from_num(0).checked_recip(), None);
assert_eq!(Fix::ZERO.checked_recip(), None);
```
";
#[inline]
@ -503,7 +503,7 @@ assert_eq!(Fix::from_num(0).checked_recip(), None);
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::from_num(7.5).checked_div_euclid(Fix::ZERO), None);
assert_eq!(Fix::MAX.checked_div_euclid(Fix::from_num(0.25)), None);
",
if_signed_else_empty_str! {
@ -695,7 +695,7 @@ use fixed::{
};
type Fix = ", $s_fixed, "<U4>;
assert_eq!(Fix::from_num(5).saturating_signum(), 1);
assert_eq!(Fix::from_num(0).saturating_signum(), 0);
assert_eq!(Fix::ZERO.saturating_signum(), 0);
assert_eq!(Fix::from_num(-5).saturating_signum(), -1);
type OneIntBit = ", $s_fixed, "<U", $s_nbits_m1, ">;
@ -756,8 +756,8 @@ Panics if the divisor is zero.
```rust
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);
let one_half = Fix::ONE / 2;
assert_eq!(Fix::ONE.saturating_div(Fix::from_num(2)), one_half);
assert_eq!(Fix::MAX.saturating_div(one_half), Fix::MAX);
```
";
@ -876,7 +876,7 @@ use fixed::{
};
type Fix = ", $s_fixed, "<U4>;
assert_eq!(Fix::from_num(5).wrapping_signum(), 1);
assert_eq!(Fix::from_num(0).wrapping_signum(), 0);
assert_eq!(Fix::ZERO.wrapping_signum(), 0);
assert_eq!(Fix::from_num(-5).wrapping_signum(), -1);
type OneIntBit = ", $s_fixed, "<U", $s_nbits_m1, ">;
@ -932,7 +932,7 @@ use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
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 quarter = Fix::ONE / 4;
let wrapped = Fix::from_bits(!0 << 2);
assert_eq!(Fix::MAX.wrapping_div(quarter), wrapped);
```
@ -959,7 +959,7 @@ Panics if the fixed-point number is zero.
use fixed::{types::extra::U", $s_nbits_m1, ", ", $s_fixed, "};
// only one integer bit
type Fix = ", $s_fixed, "<U", $s_nbits_m1, ">;
assert_eq!(Fix::from_num(0.25).wrapping_recip(), Fix::from_num(0));
assert_eq!(Fix::from_num(0.25).wrapping_recip(), Fix::ZERO);
```
";
#[inline]
@ -1096,7 +1096,7 @@ Panics if the result does not fit.
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
assert_eq!(Fix::from_num(5).unwrapped_signum(), 1);
assert_eq!(Fix::from_num(0).unwrapped_signum(), 0);
assert_eq!(Fix::ZERO.unwrapped_signum(), 0);
assert_eq!(Fix::from_num(-5).unwrapped_signum(), -1);
```
@ -1168,7 +1168,7 @@ The following panics because of overflow.
```should_panic
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
let quarter = Fix::from_num(1) / 4;
let quarter = Fix::ONE / 4;
let _overflow = Fix::MAX.unwrapped_div(quarter);
```
";
@ -1409,15 +1409,15 @@ use fixed::{
", $s_fixed, ",
};
type Fix = ", $s_fixed, "<U4>;
assert_eq!(Fix::from_num(5).overflowing_signum(), (Fix::from_num(1), false));
assert_eq!(Fix::from_num(0).overflowing_signum(), (Fix::from_num(0), false));
assert_eq!(Fix::from_num(5).overflowing_signum(), (Fix::ONE, false));
assert_eq!(Fix::ZERO.overflowing_signum(), (Fix::ZERO, false));
assert_eq!(Fix::from_num(-5).overflowing_signum(), (Fix::from_num(-1), false));
type OneIntBit = ", $s_fixed, "<U", $s_nbits_m1, ">;
type ZeroIntBits = ", $s_fixed, "<U", $s_nbits, ">;
assert_eq!(OneIntBit::from_num(0.5).overflowing_signum(), (OneIntBit::from_num(-1), true));
assert_eq!(ZeroIntBits::from_num(0.25).overflowing_signum(), (ZeroIntBits::from_num(0), true));
assert_eq!(ZeroIntBits::from_num(-0.5).overflowing_signum(), (ZeroIntBits::from_num(0), true));
assert_eq!(ZeroIntBits::from_num(0.25).overflowing_signum(), (ZeroIntBits::ZERO, true));
assert_eq!(ZeroIntBits::from_num(-0.5).overflowing_signum(), (ZeroIntBits::ZERO, true));
```
";
#[inline]
@ -1472,7 +1472,7 @@ use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
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 quarter = Fix::ONE / 4;
let wrapped = Fix::from_bits(!0 << 2);
assert_eq!(Fix::MAX.overflowing_div(quarter), (wrapped, true));
```
@ -1506,7 +1506,7 @@ type Fix = ", $s_fixed, "<U4>;
// only one integer bit
type Small = ", $s_fixed, "<U", $s_nbits_m1, ">;
assert_eq!(Fix::from_num(0.25).overflowing_recip(), (Fix::from_num(4), false));
assert_eq!(Small::from_num(0.25).overflowing_recip(), (Small::from_num(0), true));
assert_eq!(Small::from_num(0.25).overflowing_recip(), (Small::ZERO, true));
```
";
#[inline]

View File

@ -116,7 +116,7 @@ type Fix = ", $s_fixed, "<U4>;
let src = Fix::from_bits(0b111 << (4 - 2));
assert_eq!(src.to_num::<I30F2>(), I30F2::from_bits(0b111));
// src >> 2 is 0.0111, which for I30F2 is truncated to 0.01
assert_eq!((src >> 2u32).to_num::<I30F2>(), I30F2::from_bits(1));
assert_eq!((src >> 2u32).to_num::<I30F2>(), I30F2::from_bits(0b1));
// 2.5 is 10.1 in binary
let two_point_5 = Fix::from_bits(0b101 << (4 - 1));
@ -924,7 +924,7 @@ assert_eq!(I8F8::saturating_from_str(\"-9999\"), Ok(I8F8::MIN));
",
"use fixed::types::U8F8;
assert_eq!(U8F8::saturating_from_str(\"9999\"), Ok(U8F8::MAX));
assert_eq!(U8F8::saturating_from_str(\"-1\"), Ok(U8F8::from_num(0)));
assert_eq!(U8F8::saturating_from_str(\"-1\"), Ok(U8F8::ZERO));
",
},
"```
@ -953,7 +953,7 @@ 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));
assert_eq!(U8F8::saturating_from_str_binary(\"-1\"), Ok(U8F8::from_num(0)));
assert_eq!(U8F8::saturating_from_str_binary(\"-1\"), Ok(U8F8::ZERO));
",
},
"```
@ -982,7 +982,7 @@ 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));
assert_eq!(U8F8::saturating_from_str_octal(\"-1\"), Ok(U8F8::from_num(0)));
assert_eq!(U8F8::saturating_from_str_octal(\"-1\"), Ok(U8F8::ZERO));
",
},
"```
@ -1011,7 +1011,7 @@ 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));
assert_eq!(U8F8::saturating_from_str_hex(\"-1\"), Ok(U8F8::from_num(0)));
assert_eq!(U8F8::saturating_from_str_hex(\"-1\"), Ok(U8F8::ZERO));
",
},
"```

View File

@ -430,7 +430,7 @@ representation.
```rust
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
let all_ones = !Fix::from_bits(0);
let all_ones = !Fix::ZERO;
let f = all_ones - Fix::from_bits(0b10_0000);
assert_eq!(f.leading_ones(), ", $s_nbits, " - 6);
```
@ -630,7 +630,7 @@ assert_eq!(Fix::from_bits(bits).rotate_right(3), Fix::from_bits(rot));
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::ZERO.is_positive());
assert!(!Fix::from_num(-5).is_positive());
```
";
@ -649,7 +649,7 @@ assert!(!Fix::from_num(-5).is_positive());
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::ZERO.is_negative());
assert!(Fix::from_num(-5).is_negative());
```
";
@ -857,7 +857,7 @@ type Fix = ", $s_fixed, "<U4>;
type UFix = ", $s_ufixed, "<U4>;
assert_eq!(Fix::from_num(-5).unsigned_abs(), UFix::from_num(5));
// min_as_unsigned has only highest bit set
let min_as_unsigned = UFix::from_num(1) << (UFix::INT_NBITS - 1);
let min_as_unsigned = UFix::ONE << (UFix::INT_NBITS - 1);
assert_eq!(Fix::MIN.unsigned_abs(), min_as_unsigned);
```
";
@ -886,7 +886,7 @@ assert_eq!(Fix::from_bits(0b11_0010).highest_one(), Fix::from_bits(0b10_0000));
assert_eq!(Fix::from_num(0.3).highest_one(), Fix::from_num(0.25));
assert_eq!(Fix::from_num(4).highest_one(), Fix::from_num(4));
assert_eq!(Fix::from_num(6.5).highest_one(), Fix::from_num(4));
assert_eq!(Fix::from_num(0).highest_one(), Fix::from_num(0));
assert_eq!(Fix::ZERO.highest_one(), Fix::ZERO);
```
";
#[inline]
@ -982,7 +982,7 @@ type Fix = ", $s_fixed, "<U4>;
$Signedness,
"assert_eq!(Fix::from_num(5).checked_neg(), Some(Fix::from_num(-5)));
assert_eq!(Fix::MIN.checked_neg(), None);",
"assert_eq!(Fix::from_num(0).checked_neg(), Some(Fix::from_num(0)));
"assert_eq!(Fix::ZERO.checked_neg(), Some(Fix::ZERO));
assert_eq!(Fix::from_num(5).checked_neg(), None);",
},
"
@ -1005,9 +1005,8 @@ assert_eq!(Fix::from_num(5).checked_neg(), None);",
```rust
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
let one = Fix::from_num(1);
assert_eq!((Fix::MAX - one).checked_add(one), Some(Fix::MAX));
assert_eq!(Fix::MAX.checked_add(one), None);
assert_eq!((Fix::MAX - Fix::ONE).checked_add(Fix::ONE), Some(Fix::MAX));
assert_eq!(Fix::MAX.checked_add(Fix::ONE), None);
```
";
#[inline]
@ -1028,9 +1027,8 @@ assert_eq!(Fix::MAX.checked_add(one), None);
```rust
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
let one = Fix::from_num(1);
assert_eq!((Fix::MIN + one).checked_sub(one), Some(Fix::MIN));
assert_eq!(Fix::MIN.checked_sub(one), None);
assert_eq!((Fix::MIN + Fix::ONE).checked_sub(Fix::ONE), Some(Fix::MIN));
assert_eq!(Fix::MIN.checked_sub(Fix::ONE), None);
```
";
#[inline]
@ -1052,8 +1050,8 @@ the divisor is zero.
```rust
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
assert_eq!(Fix::from_num(1.5).checked_rem(Fix::from_num(1)), Some(Fix::from_num(0.5)));
assert_eq!(Fix::from_num(1.5).checked_rem(Fix::from_num(0)), None);
assert_eq!(Fix::from_num(1.5).checked_rem(Fix::ONE), Some(Fix::from_num(0.5)));
assert_eq!(Fix::from_num(1.5).checked_rem(Fix::ZERO), None);
```
";
#[inline]
@ -1099,8 +1097,8 @@ assert_eq!(
Fix::from_num(4).checked_mul_add(Fix::from_num(0.5), Fix::from_num(3)),
Some(Fix::from_num(5))
);
assert_eq!(Fix::MAX.checked_mul_add(Fix::from_num(1), Fix::from_num(0)), Some(Fix::MAX));
assert_eq!(Fix::MAX.checked_mul_add(Fix::from_num(1), Fix::from_bits(1)), None);
assert_eq!(Fix::MAX.checked_mul_add(Fix::ONE, Fix::ZERO), Some(Fix::MAX));
assert_eq!(Fix::MAX.checked_mul_add(Fix::ONE, Fix::DELTA), None);
",
if_signed_else_empty_str! {
$Signedness,
@ -1167,7 +1165,7 @@ assert_eq!(Fix::MAX.checked_mul_int(2), None);
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
assert_eq!(Fix::MAX.checked_div_int(1), Some(Fix::MAX));
assert_eq!(Fix::from_num(1).checked_div_int(0), None);
assert_eq!(Fix::ONE.checked_div_int(0), None);
",
if_signed_else_empty_str! {
$Signedness,
@ -1197,7 +1195,7 @@ use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
let num = Fix::from_num(7.5);
assert_eq!(num.checked_rem_euclid(Fix::from_num(2)), Some(Fix::from_num(1.5)));
assert_eq!(num.checked_rem_euclid(Fix::from_num(0)), None);
assert_eq!(num.checked_rem_euclid(Fix::ZERO), None);
",
if_signed_else_empty_str! {
$Signedness,
@ -1232,8 +1230,8 @@ or [`None`] if `rhs` ≥ ", $s_nbits, ".
```rust
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
assert_eq!((Fix::from_num(1) / 2).checked_shl(3), Some(Fix::from_num(4)));
assert_eq!((Fix::from_num(1) / 2).checked_shl(", $s_nbits, "), None);
assert_eq!((Fix::ONE / 2).checked_shl(3), Some(Fix::from_num(4)));
assert_eq!((Fix::ONE / 2).checked_shl(", $s_nbits, "), None);
```
";
#[inline]
@ -1255,7 +1253,7 @@ or [`None`] if `rhs` ≥ ", $s_nbits, ".
```rust
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
assert_eq!(Fix::from_num(4).checked_shr(3), Some(Fix::from_num(1) / 2));
assert_eq!(Fix::from_num(4).checked_shr(3), Some(Fix::ONE / 2));
assert_eq!(Fix::from_num(4).checked_shr(", $s_nbits, "), None);
```
";
@ -1345,8 +1343,8 @@ type Fix = ", $s_fixed, "<U4>;
$Signedness,
"assert_eq!(Fix::from_num(5).saturating_neg(), Fix::from_num(-5));
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));",
"assert_eq!(Fix::ZERO.saturating_neg(), Fix::from_num(0));
assert_eq!(Fix::from_num(5).saturating_neg(), Fix::ZERO);",
},
"
```
@ -1375,7 +1373,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.saturating_add(Fix::from_num(1)), Fix::MAX);
assert_eq!(Fix::MAX.saturating_add(Fix::ONE), Fix::MAX);
```
";
#[inline]
@ -1403,10 +1401,10 @@ 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.saturating_sub(Fix::from_num(1)), Fix::MIN);",
"assert_eq!(Fix::ONE.saturating_sub(Fix::from_num(3)), Fix::from_num(-2));
assert_eq!(Fix::MIN.saturating_sub(Fix::ONE), 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));",
assert_eq!(Fix::ZERO.saturating_sub(Fix::from_num(1)), Fix::from_num(0));",
},
"
```
@ -1573,7 +1571,7 @@ type Fix = ", $s_fixed, "<U4>;
$Signedness,
"assert_eq!(Fix::from_num(5).wrapping_neg(), Fix::from_num(-5));
assert_eq!(Fix::MIN.wrapping_neg(), Fix::MIN);",
"assert_eq!(Fix::from_num(0).wrapping_neg(), Fix::from_num(0));
"assert_eq!(Fix::ZERO.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;
assert_eq!(Fix::from_num(5).wrapping_neg(), Fix::from_bits(neg_five_bits));",
@ -1595,12 +1593,11 @@ assert_eq!(Fix::from_num(5).wrapping_neg(), Fix::from_bits(neg_five_bits));",
```rust
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
let one = Fix::from_num(1);
let one_minus_bit = one - Fix::from_bits(1);
let one_minus_delta = Fix::ONE - Fix::DELTA;
assert_eq!(Fix::from_num(3).wrapping_add(Fix::from_num(2)), Fix::from_num(5));
assert_eq!(Fix::MAX.wrapping_add(one), ",
assert_eq!(Fix::MAX.wrapping_add(Fix::ONE), ",
if_signed_else_empty_str! { $Signedness, "Fix::MIN + " },
"one_minus_bit);
"one_minus_delta);
```
";
#[inline]
@ -1618,17 +1615,16 @@ assert_eq!(Fix::MAX.wrapping_add(one), ",
```rust
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
let one = Fix::from_num(1);
let one_minus_bit = one - Fix::from_bits(1);
let one_minus_delta = Fix::ONE - Fix::DELTA;
",
if_signed_unsigned! {
$Signedness,
"assert_eq!(Fix::from_num(3).wrapping_sub(Fix::from_num(5)), Fix::from_num(-2));
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)",
assert_eq!(Fix::ZERO",
},
".wrapping_sub(one), Fix::MAX - one_minus_bit);
".wrapping_sub(Fix::ONE), Fix::MAX - one_minus_delta);
```
";
#[inline]
@ -1654,8 +1650,8 @@ assert_eq!(
Fix::from_num(4).wrapping_mul_add(Fix::from_num(0.5), Fix::from_num(3)),
Fix::from_num(5)
);
assert_eq!(Fix::MAX.wrapping_mul_add(Fix::from_num(1), Fix::from_num(0)), Fix::MAX);
assert_eq!(Fix::MAX.wrapping_mul_add(Fix::from_num(1), Fix::from_bits(1)), Fix::MIN);
assert_eq!(Fix::MAX.wrapping_mul_add(Fix::ONE, Fix::from_num(0)), Fix::MAX);
assert_eq!(Fix::MAX.wrapping_mul_add(Fix::ONE, Fix::from_bits(1)), Fix::MIN);
let wrapped = Fix::MAX.wrapping_mul_int(4);
assert_eq!(Fix::MAX.wrapping_mul_add(Fix::from_num(3), Fix::MAX), wrapped);
```
@ -1745,8 +1741,8 @@ then shifts and returns the number.
```rust
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
assert_eq!((Fix::from_num(1) / 2).wrapping_shl(3), Fix::from_num(4));
assert_eq!((Fix::from_num(1) / 2).wrapping_shl(3 + ", $s_nbits, "), Fix::from_num(4));
assert_eq!((Fix::ONE / 2).wrapping_shl(3), Fix::from_num(4));
assert_eq!((Fix::ONE / 2).wrapping_shl(3 + ", $s_nbits, "), Fix::from_num(4));
```
";
#[inline]
@ -1765,8 +1761,8 @@ then shifts and returns the number.
```rust
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
assert_eq!((Fix::from_num(4)).wrapping_shr(3), Fix::from_num(1) / 2);
assert_eq!((Fix::from_num(4)).wrapping_shr(3 + ", $s_nbits, "), Fix::from_num(1) / 2);
assert_eq!((Fix::from_num(4)).wrapping_shr(3), Fix::ONE / 2);
assert_eq!((Fix::from_num(4)).wrapping_shr(3 + ", $s_nbits, "), Fix::ONE / 2);
```
";
#[inline]
@ -1863,7 +1859,7 @@ type Fix = ", $s_fixed, "<U4>;
let _overflow = Fix::MIN.unwrapped_neg();",
),
concat!(
"assert_eq!(Fix::from_num(0).unwrapped_neg(), Fix::from_num(0));
"assert_eq!(Fix::ZERO.unwrapped_neg(), Fix::ZERO);
```
The following panics because of overflow.
@ -1904,7 +1900,7 @@ The following panics because of overflow.
```should_panic
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
let _overflow = Fix::MAX.unwrapped_add(Fix::from_bits(1));
let _overflow = Fix::MAX.unwrapped_add(Fix::DELTA);
```
";
#[inline]
@ -1942,7 +1938,7 @@ 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_sub(Fix::from_bits(1));
let _overflow = Fix::MIN.unwrapped_sub(Fix::DELTA);
```
";
#[inline]
@ -1965,7 +1961,7 @@ Panics if the divisor is zero.
```rust
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
assert_eq!(Fix::from_num(1.5).unwrapped_rem(Fix::from_num(1)), Fix::from_num(0.5));
assert_eq!(Fix::from_num(1.5).unwrapped_rem(Fix::ONE), Fix::from_num(0.5));
```
The following panics because the divisor is zero.
@ -1973,7 +1969,7 @@ 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(1.5).unwrapped_rem(Fix::from_num(0));
let _divisor_is_zero = Fix::from_num(1.5).unwrapped_rem(Fix::ZERO);
```
";
#[inline]
@ -2028,7 +2024,7 @@ The following panics because of overflow.
```should_panic
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
let _overflow = Fix::MAX.unwrapped_mul_add(Fix::from_num(1), Fix::from_bits(1));
let _overflow = Fix::MAX.unwrapped_mul_add(Fix::ONE, Fix::DELTA);
```
";
#[inline]
@ -2149,7 +2145,7 @@ 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_rem_euclid(Fix::from_num(0));
let _divisor_is_zero = Fix::from_num(3).unwrapped_rem_euclid(Fix::ZERO);
```
";
#[inline]
@ -2172,7 +2168,7 @@ Panics if `rhs` ≥ ", $s_nbits, ".
```rust
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
assert_eq!((Fix::from_num(1) / 2).unwrapped_shl(3), Fix::from_num(4));
assert_eq!((Fix::ONE / 2).unwrapped_shl(3), Fix::from_num(4));
```
The following panics because of overflow.
@ -2180,7 +2176,7 @@ The following panics because of overflow.
```should_panic
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
let _overflow = Fix::from_num(1).unwrapped_shl(", $s_nbits, ");
let _overflow = Fix::ONE.unwrapped_shl(", $s_nbits, ");
```
";
#[inline]
@ -2203,7 +2199,7 @@ Panics if `rhs` ≥ ", $s_nbits, ".
```rust
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
assert_eq!((Fix::from_num(4)).unwrapped_shr(3), Fix::from_num(1) / 2);
assert_eq!((Fix::from_num(4)).unwrapped_shr(3), Fix::ONE / 2);
```
The following panics because of overflow.
@ -2211,7 +2207,7 @@ The following panics because of overflow.
```should_panic
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
let _overflow = Fix::from_num(1).unwrapped_shr(", $s_nbits, ");
let _overflow = Fix::ONE.unwrapped_shr(", $s_nbits, ");
```
";
#[inline]
@ -2319,7 +2315,7 @@ type Fix = ", $s_fixed, "<U4>;
$Signedness,
"assert_eq!(Fix::from_num(5).overflowing_neg(), (Fix::from_num(-5), false));
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::ZERO.overflowing_neg(), (Fix::ZERO, 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;
assert_eq!(Fix::from_num(5).overflowing_neg(), (Fix::from_bits(neg_five_bits), true));",
@ -2345,12 +2341,11 @@ overflow has occurred. On overflow, the wrapped value is returned.
```rust
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
let one = Fix::from_num(1);
let one_minus_bit = one - Fix::from_bits(1);
let one_minus_delta = Fix::ONE - Fix::DELTA;
assert_eq!(Fix::from_num(3).overflowing_add(Fix::from_num(2)), (Fix::from_num(5), false));
assert_eq!(Fix::MAX.overflowing_add(one), (",
assert_eq!(Fix::MAX.overflowing_add(Fix::ONE), (",
if_signed_else_empty_str! { $Signedness, "Fix::MIN + " },
"one_minus_bit, true));
"one_minus_delta, true));
```
";
#[inline]
@ -2372,17 +2367,16 @@ overflow has occurred. On overflow, the wrapped value is returned.
```rust
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
let one = Fix::from_num(1);
let one_minus_bit = one - Fix::from_bits(1);
let one_minus_delta = Fix::ONE - Fix::DELTA;
",
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",
"assert_eq!(Fix::from_num(5).overflowing_sub(Fix::from_num(3)), (Fix::from_num(2), false));
assert_eq!(Fix::from_num(0)",
assert_eq!(Fix::ZERO",
},
".overflowing_sub(one), (Fix::MAX - one_minus_bit, true));
".overflowing_sub(Fix::ONE), (Fix::MAX - one_minus_delta, true));
```
";
#[inline]
@ -2409,11 +2403,11 @@ The `mul` parameter can have a fixed-point type like
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
assert_eq!(
Fix::MAX.overflowing_mul_add(Fix::from_num(1), Fix::from_num(0)),
Fix::MAX.overflowing_mul_add(Fix::ONE, Fix::ZERO),
(Fix::MAX, false)
);
assert_eq!(
Fix::MAX.overflowing_mul_add(Fix::from_num(1), Fix::from_bits(1)),
Fix::MAX.overflowing_mul_add(Fix::ONE, Fix::DELTA),
(Fix::MIN, true)
);
assert_eq!(
@ -2525,8 +2519,8 @@ On overflow `rhs` is wrapped before the shift operation.
```rust
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
assert_eq!((Fix::from_num(1) / 2).overflowing_shl(3), (Fix::from_num(4), false));
assert_eq!((Fix::from_num(1) / 2).overflowing_shl(3 + ", $s_nbits, "), (Fix::from_num(4), true));
assert_eq!((Fix::ONE / 2).overflowing_shl(3), (Fix::from_num(4), false));
assert_eq!((Fix::ONE / 2).overflowing_shl(3 + ", $s_nbits, "), (Fix::from_num(4), true));
```
";
#[inline]
@ -2549,8 +2543,8 @@ On overflow `rhs` is wrapped before the shift operation.
```rust
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
assert_eq!((Fix::from_num(4)).overflowing_shr(3), (Fix::from_num(1) / 2, false));
assert_eq!((Fix::from_num(4)).overflowing_shr(3 + ", $s_nbits, "), (Fix::from_num(1) / 2, true));
assert_eq!((Fix::from_num(4)).overflowing_shr(3), (Fix::ONE / 2, false));
assert_eq!((Fix::from_num(4)).overflowing_shr(3 + ", $s_nbits, "), (Fix::ONE / 2, true));
```
";
#[inline]

View File

@ -84,7 +84,7 @@ numbers, except in the case where there are no integer bits, that is
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
// 0000.0100
let quarter = Fix::from_num(1) / 4;
let quarter = Fix::ONE / 4;
// 0010.0100
let two_and_quarter = quarter * 9;
assert_eq!(two_and_quarter.frac(), quarter);
@ -608,7 +608,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.wrapping_floor(), AllFrac::from_num(0));
assert_eq!(AllFrac::MIN.wrapping_floor(), AllFrac::ZERO);
",
},
"```
@ -893,7 +893,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.overflowing_floor(), (AllFrac::from_num(0), true));
assert_eq!(AllFrac::MIN.overflowing_floor(), (AllFrac::ZERO, true));
",
},
"```

View File

@ -44,7 +44,7 @@ use core::{
/// ```should_panic
/// use fixed::{types::I16F16, Unwrapped};
/// let max = Unwrapped(I16F16::MAX);
/// let delta = Unwrapped(I16F16::from_bits(1));
/// let delta = Unwrapped(I16F16::DELTA);
/// let _overflow = max + delta;
/// ```
#[repr(transparent)]
@ -878,7 +878,7 @@ impl<F: Fixed> Unwrapped<F> {
///
/// ```should_panic
/// use fixed::{types::I8F24, Unwrapped};
/// let frac_1_512 = Unwrapped(I8F24::from_num(1) / 512);
/// let frac_1_512 = Unwrapped(I8F24::ONE / 512);
/// let _overflow = frac_1_512.recip();
/// ```
#[inline]
@ -910,7 +910,7 @@ impl<F: Fixed> Unwrapped<F> {
///
/// ```should_panic
/// use fixed::{types::I16F16, Unwrapped};
/// let one = Unwrapped(I16F16::from_num(1));
/// let one = Unwrapped(I16F16::ONE);
/// let max = Unwrapped(I16F16::MAX);
/// let _overflow = max.mul_add(one, one);
/// ```
@ -1062,7 +1062,7 @@ impl<F: FixedSigned> Unwrapped<F> {
/// ```rust
/// use fixed::{types::I16F16, Unwrapped};
/// assert!(Unwrapped(I16F16::from_num(4.3)).is_positive());
/// assert!(!Unwrapped(I16F16::from_num(0)).is_positive());
/// assert!(!Unwrapped(I16F16::ZERO).is_positive());
/// assert!(!Unwrapped(I16F16::from_num(-4.3)).is_positive());
/// ```
#[inline]
@ -1077,7 +1077,7 @@ impl<F: FixedSigned> Unwrapped<F> {
/// ```rust
/// use fixed::{types::I16F16, Unwrapped};
/// assert!(!Unwrapped(I16F16::from_num(4.3)).is_negative());
/// assert!(!Unwrapped(I16F16::from_num(0)).is_negative());
/// assert!(!Unwrapped(I16F16::ZERO).is_negative());
/// assert!(Unwrapped(I16F16::from_num(-4.3)).is_negative());
/// ```
#[inline]
@ -1129,8 +1129,8 @@ impl<F: FixedSigned> Unwrapped<F> {
/// ```rust
/// use fixed::{types::I16F16, Unwrapped};
/// assert_eq!(Unwrapped(<I16F16>::from_num(-3.9)).signum(), Unwrapped(I16F16::from_num(-1)));
/// assert_eq!(Unwrapped(<I16F16>::from_num(0)).signum(), Unwrapped(I16F16::from_num(0)));
/// assert_eq!(Unwrapped(<I16F16>::from_num(3.9)).signum(), Unwrapped(I16F16::from_num(1)));
/// assert_eq!(Unwrapped(<I16F16>::ZERO).signum(), Unwrapped(I16F16::from_num(0)));
/// assert_eq!(Unwrapped(<I16F16>::from_num(3.9)).signum(), Unwrapped(I16F16::ONE));
/// ```
///
/// The following panics because of overflow.
@ -1194,7 +1194,7 @@ impl<F: FixedUnsigned> Unwrapped<F> {
/// assert_eq!(T::from_num(0.3).highest_one(), T::from_num(0.25));
/// assert_eq!(T::from_num(4).highest_one(), T::from_num(4));
/// assert_eq!(T::from_num(6.5).highest_one(), T::from_num(4));
/// assert_eq!(T::from_num(0).highest_one(), T::from_num(0));
/// assert_eq!(T::ZERO.highest_one(), T::ZERO);
/// ```
#[inline]
pub fn highest_one(self) -> Unwrapped<F> {

View File

@ -41,7 +41,7 @@ use core::{
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// let max = Wrapping(I16F16::MAX);
/// let delta = Wrapping(I16F16::from_bits(1));
/// let delta = Wrapping(I16F16::DELTA);
/// assert_eq!(I16F16::MIN, (max + delta).0);
/// ```
#[repr(transparent)]
@ -593,7 +593,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).floor(), Wrapping(I0F32::from_num(0)));
/// assert_eq!(Wrapping(I0F32::MIN).floor(), Wrapping(I0F32::ZERO));
/// ```
#[inline]
pub fn floor(self) -> Wrapping<F> {
@ -813,9 +813,9 @@ impl<F: Fixed> Wrapping<F> {
/// ```rust
/// use fixed::{types::I8F24, Wrapping};
/// let quarter = Wrapping(I8F24::from_num(0.25));
/// let frac_1_512 = Wrapping(I8F24::from_num(1) / 512);
/// let frac_1_512 = Wrapping(I8F24::ONE / 512);
/// assert_eq!(quarter.recip(), Wrapping(I8F24::from_num(4)));
/// assert_eq!(frac_1_512.recip(), Wrapping(I8F24::from_num(0)));
/// assert_eq!(frac_1_512.recip(), Wrapping(I8F24::ZERO));
/// ```
#[inline]
pub fn recip(self) -> Wrapping<F> {
@ -958,7 +958,7 @@ impl<F: FixedSigned> Wrapping<F> {
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// assert!(Wrapping(I16F16::from_num(4.3)).is_positive());
/// assert!(!Wrapping(I16F16::from_num(0)).is_positive());
/// assert!(!Wrapping(I16F16::ZERO).is_positive());
/// assert!(!Wrapping(I16F16::from_num(-4.3)).is_positive());
/// ```
#[inline]
@ -973,7 +973,7 @@ impl<F: FixedSigned> Wrapping<F> {
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// assert!(!Wrapping(I16F16::from_num(4.3)).is_negative());
/// assert!(!Wrapping(I16F16::from_num(0)).is_negative());
/// assert!(!Wrapping(I16F16::ZERO).is_negative());
/// assert!(Wrapping(I16F16::from_num(-4.3)).is_negative());
/// ```
#[inline]
@ -1024,12 +1024,12 @@ impl<F: FixedSigned> Wrapping<F> {
/// Wrapping,
/// };
/// assert_eq!(Wrapping(<I16F16>::from_num(-3.9)).signum(), Wrapping(I16F16::from_num(-1)));
/// assert_eq!(Wrapping(<I16F16>::from_num(0)).signum(), Wrapping(I16F16::from_num(0)));
/// assert_eq!(Wrapping(<I16F16>::from_num(3.9)).signum(), Wrapping(I16F16::from_num(1)));
/// assert_eq!(Wrapping(<I16F16>::ZERO).signum(), Wrapping(I16F16::ZERO));
/// assert_eq!(Wrapping(<I16F16>::from_num(3.9)).signum(), Wrapping(I16F16::ONE));
///
/// assert_eq!(Wrapping(<I1F31>::from_num(0.5)).signum(), Wrapping(I1F31::from_num(-1)));
/// assert_eq!(Wrapping(<I0F32>::from_num(0.25)).signum(), Wrapping(I0F32::from_num(0)));
/// assert_eq!(Wrapping(<I0F32>::from_num(-0.5)).signum(), Wrapping(I0F32::from_num(0)));
/// assert_eq!(Wrapping(<I0F32>::from_num(0.25)).signum(), Wrapping(I0F32::ZERO));
/// assert_eq!(Wrapping(<I0F32>::from_num(-0.5)).signum(), Wrapping(I0F32::ZERO));
/// ```
///
/// [`I0F16`]: `crate::types::I0F16`
@ -1088,7 +1088,7 @@ impl<F: FixedUnsigned> Wrapping<F> {
/// assert_eq!(T::from_num(0.3).highest_one(), T::from_num(0.25));
/// assert_eq!(T::from_num(4).highest_one(), T::from_num(4));
/// assert_eq!(T::from_num(6.5).highest_one(), T::from_num(4));
/// assert_eq!(T::from_num(0).highest_one(), T::from_num(0));
/// assert_eq!(T::ZERO.highest_one(), T::ZERO);
/// ```
#[inline]
pub fn highest_one(self) -> Wrapping<F> {
@ -1109,7 +1109,7 @@ impl<F: FixedUnsigned> Wrapping<F> {
/// assert_eq!(T::from_num(4).next_power_of_two(), T::from_num(4));
/// assert_eq!(T::from_num(6.5).next_power_of_two(), T::from_num(8));
/// // if the next power of two is too large, it is wrapped to zero
/// assert_eq!(T::MAX.next_power_of_two(), T::from_num(0));
/// assert_eq!(T::MAX.next_power_of_two(), T::ZERO);
/// ```
#[inline]
pub fn next_power_of_two(self) -> Wrapping<F> {