use imports throughout doc examples

This commit is contained in:
Trevor Spiteri 2019-08-16 22:57:31 +02:00
parent 637a87524c
commit c6cacadfa0
6 changed files with 320 additions and 559 deletions

View File

@ -22,7 +22,8 @@ number, and are rounded down at that precision.
# Examples # Examples
```rust ```rust
let tau: fixed::types::I8F8 = fixed::consts::TAU.to_num(); use fixed::{consts, types::I8F8};
let tau = I8F8::from_num(consts::TAU);
println!("τ = 2π with eight binary places is {:b}", tau); println!("τ = 2π with eight binary places is {:b}", tau);
assert_eq!(format!("{:b}", tau), "110.01001000"); assert_eq!(format!("{:b}", tau), "110.01001000");
``` ```

View File

@ -299,21 +299,13 @@ are implemented by the Rust compiler.
# Examples # Examples
```rust ```rust
use fixed::{frac::U3, ", use fixed::{frac::U3, ", $s_fixed, "};
$s_fixed, let eleven = ", $s_fixed, "::<U3>::from_num(11);
"}; assert_eq!(eleven, ", $s_fixed, "::<U3>::from_bits(11 << 3));
let eleven = ",
$s_fixed,
"::<U3>::from_num(11);
assert_eq!(eleven, ",
$s_fixed,
"::<U3>::from_bits(11 << 3));
assert_eq!(eleven, 11); assert_eq!(eleven, 11);
assert_eq!(eleven.to_string(), \"11.0\"); assert_eq!(eleven.to_string(), \"11.0\");
let two_point_75 = eleven / 4; let two_point_75 = eleven / 4;
assert_eq!(two_point_75, ", assert_eq!(two_point_75, ", $s_fixed, "::<U3>::from_bits(11 << 1));
$s_fixed,
"::<U3>::from_bits(11 << 1));
assert_eq!(two_point_75, 2.75); assert_eq!(two_point_75, 2.75);
assert_eq!(two_point_75.to_string(), \"2.8\"); assert_eq!(two_point_75.to_string(), \"2.8\");
``` ```
@ -365,12 +357,9 @@ assert_eq!(two_point_75.to_string(), \"2.8\");
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>; assert_eq!(Fix::min_value(), Fix::from_bits(", $s_inner, "::min_value()));
assert_eq!(Fix::min_value(), Fix::from_bits(",
$s_inner,
"::min_value()));
``` ```
"; ";
$Fixed($Inner) => fn min_value() $Fixed($Inner) => fn min_value()
@ -381,12 +370,9 @@ assert_eq!(Fix::min_value(), Fix::from_bits(",
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>; assert_eq!(Fix::max_value(), Fix::from_bits(", $s_inner, "::max_value()));
assert_eq!(Fix::max_value(), Fix::from_bits(",
$s_inner,
"::max_value()));
``` ```
"; ";
$Fixed($Inner) => fn max_value() $Fixed($Inner) => fn max_value()
@ -398,12 +384,9 @@ assert_eq!(Fix::max_value(), Fix::from_bits(",
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U6, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U6>;
"<fixed::frac::U6>; assert_eq!(Fix::int_nbits(), ", $s_nbits, " - 6);
assert_eq!(Fix::int_nbits(), ",
$s_nbits,
" - 6);
``` ```
"; ";
#[inline] #[inline]
@ -418,9 +401,8 @@ assert_eq!(Fix::int_nbits(), ",
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U6, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U6>;
"<fixed::frac::U6>;
assert_eq!(Fix::frac_nbits(), 6); assert_eq!(Fix::frac_nbits(), 6);
``` ```
"; ";
@ -440,9 +422,8 @@ representation.
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
let f = Fix::from_bits(0b11_0010); let f = Fix::from_bits(0b11_0010);
assert_eq!(f.count_ones(), 3); assert_eq!(f.count_ones(), 3);
``` ```
@ -456,9 +437,8 @@ representation.
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
let f = Fix::from_bits(!0b11_0010); let f = Fix::from_bits(!0b11_0010);
assert_eq!(f.count_zeros(), 3); assert_eq!(f.count_zeros(), 3);
``` ```
@ -472,13 +452,10 @@ representation.
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
let f = Fix::from_bits(0b10_0000); let f = Fix::from_bits(0b10_0000);
assert_eq!(f.leading_zeros(), ", assert_eq!(f.leading_zeros(), ", $s_nbits, " - 6);
$s_nbits,
" - 6);
``` ```
"; ";
$Fixed => fn leading_zeros(self) -> u32 $Fixed => fn leading_zeros(self) -> u32
@ -490,9 +467,8 @@ representation.
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
let f = Fix::from_bits(0b10_0000); let f = Fix::from_bits(0b10_0000);
assert_eq!(f.trailing_zeros(), 5); assert_eq!(f.trailing_zeros(), 5);
``` ```
@ -506,14 +482,9 @@ truncated bits to the right end.
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>; let bits: ", $s_inner, " = (0b111 << (", $s_nbits, " - 3)) | 0b1010;
let bits: ",
$s_inner,
" = (0b111 << (",
$s_nbits,
" - 3)) | 0b1010;
let rot = 0b1010111; let rot = 0b1010111;
assert_eq!(bits.rotate_left(3), rot); assert_eq!(bits.rotate_left(3), rot);
assert_eq!(Fix::from_bits(bits).rotate_left(3), Fix::from_bits(rot)); assert_eq!(Fix::from_bits(bits).rotate_left(3), Fix::from_bits(rot));
@ -528,15 +499,10 @@ truncated bits to the left end.
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>; let bits: ", $s_inner, " = 0b1010111;
let bits: ", let rot = (0b111 << (", $s_nbits, " - 3)) | 0b1010;
$s_inner,
" = 0b1010111;
let rot = (0b111 << (",
$s_nbits,
" - 3)) | 0b1010;
assert_eq!(bits.rotate_right(3), rot); assert_eq!(bits.rotate_right(3), rot);
assert_eq!(Fix::from_bits(bits).rotate_right(3), Fix::from_bits(rot)); assert_eq!(Fix::from_bits(bits).rotate_right(3), Fix::from_bits(rot));
``` ```
@ -552,9 +518,8 @@ assert_eq!(Fix::from_bits(bits).rotate_right(3), Fix::from_bits(rot));
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
let five = Fix::from_num(5); let five = Fix::from_num(5);
let minus_five = Fix::from_num(-5); let minus_five = Fix::from_num(-5);
assert_eq!(five.abs(), five); assert_eq!(five.abs(), five);
@ -583,9 +548,8 @@ represented is almost certainly a bug.
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
assert_eq!(Fix::from_num(5).signum(), 1); assert_eq!(Fix::from_num(5).signum(), 1);
assert_eq!(Fix::from_num(0).signum(), 0); assert_eq!(Fix::from_num(0).signum(), 0);
assert_eq!(Fix::from_num(-5).signum(), -1); assert_eq!(Fix::from_num(-5).signum(), -1);
@ -613,9 +577,8 @@ assert_eq!(Fix::from_num(-5).signum(), -1);
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
// 3/8 is 0.0110 // 3/8 is 0.0110
let three_eights = Fix::from_bits(0b0110); let three_eights = Fix::from_bits(0b0110);
// 1/2 is 0.1000 // 1/2 is 0.1000
@ -642,9 +605,8 @@ future it panics.
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
// 3/8 is 0.0110 // 3/8 is 0.0110
let three_eights = Fix::from_bits(0b0110); let three_eights = Fix::from_bits(0b0110);
// 1/2 is 0.1000 // 1/2 is 0.1000
@ -663,9 +625,8 @@ assert_eq!(half.next_power_of_two(), half);
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
// 3/8 is 0.0110 // 3/8 is 0.0110
let three_eights = Fix::from_bits(0b0110); let three_eights = Fix::from_bits(0b0110);
// 1/2 is 0.1000 // 1/2 is 0.1000
@ -691,9 +652,8 @@ assert!(Fix::max_value().checked_next_power_of_two().is_none());
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
assert!(Fix::from_num(5).is_positive()); assert!(Fix::from_num(5).is_positive());
assert!(!Fix::from_num(0).is_positive()); assert!(!Fix::from_num(0).is_positive());
assert!(!Fix::from_num(-5).is_positive()); assert!(!Fix::from_num(-5).is_positive());
@ -710,9 +670,8 @@ assert!(!Fix::from_num(-5).is_positive());
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
assert!(!Fix::from_num(5).is_negative()); assert!(!Fix::from_num(5).is_negative());
assert!(!Fix::from_num(0).is_negative()); assert!(!Fix::from_num(0).is_negative());
assert!(Fix::from_num(-5).is_negative()); assert!(Fix::from_num(-5).is_negative());

View File

@ -29,9 +29,8 @@ macro_rules! fixed_checked_arith {
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
", ",
if_signed_unsigned!( if_signed_unsigned!(
$Signedness, $Signedness,
@ -57,9 +56,8 @@ assert_eq!(Fix::from_num(5).checked_neg(), None);",
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
let one = Fix::from_num(1); let one = Fix::from_num(1);
assert_eq!((Fix::max_value() - one).checked_add(one), Some(Fix::max_value())); 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_value().checked_add(one), None);
@ -79,9 +77,8 @@ assert_eq!(Fix::max_value().checked_add(one), None);
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
let one = Fix::from_num(1); let one = Fix::from_num(1);
assert_eq!((Fix::min_value() + one).checked_sub(one), Some(Fix::min_value())); 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_value().checked_sub(one), None);
@ -101,9 +98,8 @@ assert_eq!(Fix::min_value().checked_sub(one), None);
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::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(1)), Some(Fix::max_value()));
assert_eq!(Fix::max_value().checked_mul(Fix::from_num(2)), None); assert_eq!(Fix::max_value().checked_mul(Fix::from_num(2)), None);
``` ```
@ -127,9 +123,8 @@ the divisor is zero or on overflow.
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::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)), Some(Fix::max_value()));
assert_eq!(Fix::max_value().checked_div(Fix::from_num(1) / 2), None); assert_eq!(Fix::max_value().checked_div(Fix::from_num(1) / 2), None);
``` ```
@ -156,9 +151,8 @@ product, or [`None`] on overflow.
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
assert_eq!(Fix::max_value().checked_mul_int(1), Some(Fix::max_value())); 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_value().checked_mul_int(2), None);
``` ```
@ -184,9 +178,8 @@ assert_eq!(Fix::max_value().checked_mul_int(2), None);
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
assert_eq!(Fix::max_value().checked_div_int(1), Some(Fix::max_value())); assert_eq!(Fix::max_value().checked_div_int(1), Some(Fix::max_value()));
assert_eq!(Fix::from_num(1).checked_div_int(0), None); assert_eq!(Fix::from_num(1).checked_div_int(0), None);
", ",
@ -218,9 +211,8 @@ Returns the remainder, or [`None`] if the divisor is zero",
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
// binary 1.0101 / 8 = binary 0.0010 remainder 0.0101 // binary 1.0101 / 8 = binary 0.0010 remainder 0.0101
assert_eq!(Fix::from_bits(0b10101).checked_rem_int(8), Some(Fix::from_bits(0b101))); assert_eq!(Fix::from_bits(0b10101).checked_rem_int(8), Some(Fix::from_bits(0b101)));
assert_eq!(Fix::from_num(1).checked_rem_int(0), None); assert_eq!(Fix::from_num(1).checked_rem_int(0), None);
@ -241,20 +233,16 @@ assert_eq!(Fix::from_num(1).checked_rem_int(0), None);
); );
comment!( comment!(
"Checked shift left. Returns the shifted number, or [`None`] if `rhs` ≥ ", "Checked shift left. Returns the shifted number,
$s_nbits, or [`None`] if `rhs` ", $s_nbits, ".
".
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::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(3), Some(Fix::from_num(4)));
assert_eq!((Fix::from_num(1) / 2).checked_shl(", assert_eq!((Fix::from_num(1) / 2).checked_shl(", $s_nbits, "), None);
$s_nbits,
"), None);
``` ```
[`None`]: https://doc.rust-lang.org/nightly/std/option/enum.Option.html#variant.None [`None`]: https://doc.rust-lang.org/nightly/std/option/enum.Option.html#variant.None
@ -266,20 +254,16 @@ assert_eq!((Fix::from_num(1) / 2).checked_shl(",
); );
comment!( comment!(
"Checked shift right. Returns the shifted number, or [`None`] if `rhs` ≥ ", "Checked shift right. Returns the shifted number,
$s_nbits, or [`None`] if `rhs` ", $s_nbits, ".
".
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::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::from_num(1) / 2));
assert_eq!(Fix::from_num(4).checked_shr(", assert_eq!(Fix::from_num(4).checked_shr(", $s_nbits, "), None);
$s_nbits,
"), None);
``` ```
[`None`]: https://doc.rust-lang.org/nightly/std/option/enum.Option.html#variant.None [`None`]: https://doc.rust-lang.org/nightly/std/option/enum.Option.html#variant.None
@ -300,9 +284,8 @@ Overflow can only occur when trying to find the absolute value of the minimum va
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
assert_eq!(Fix::from_num(-5).checked_abs(), Some(Fix::from_num(5))); 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_value().checked_abs(), None);
``` ```
@ -330,9 +313,8 @@ assert_eq!(Fix::min_value().checked_abs(), None);
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
", ",
if_signed_unsigned!( if_signed_unsigned!(
$Signedness, $Signedness,
@ -360,9 +342,8 @@ assert_eq!(Fix::from_num(5).saturating_neg(), Fix::from_num(0));",
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
assert_eq!(Fix::from_num(3).saturating_add(Fix::from_num(2)), Fix::from_num(5)); 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_value().saturating_add(Fix::from_num(1)), Fix::max_value());
``` ```
@ -376,9 +357,8 @@ assert_eq!(Fix::max_value().saturating_add(Fix::from_num(1)), Fix::max_value());
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
", ",
if_signed_unsigned!( if_signed_unsigned!(
$Signedness, $Signedness,
@ -399,9 +379,8 @@ assert_eq!(Fix::from_num(0).saturating_sub(Fix::from_num(1)), Fix::from_num(0));
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
assert_eq!(Fix::from_num(3).saturating_mul(Fix::from_num(2)), Fix::from_num(6)); 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_value().saturating_mul(Fix::from_num(2)), Fix::max_value());
``` ```
@ -427,9 +406,8 @@ Panics if the divisor is zero.
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
let one_half = Fix::from_num(1) / 2; 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::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_value().saturating_div(one_half), Fix::max_value());
@ -452,9 +430,8 @@ assert_eq!(Fix::max_value().saturating_div(one_half), Fix::max_value());
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
assert_eq!(Fix::from_num(3).saturating_mul_int(2), Fix::from_num(6)); 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_value().saturating_mul_int(2), Fix::max_value());
``` ```
@ -475,9 +452,8 @@ Overflow can only occur when trying to find the absolute value of the minimum va
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
assert_eq!(Fix::from_num(-5).saturating_abs(), Fix::from_num(5)); 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_value().saturating_abs(), Fix::max_value());
``` ```
@ -503,9 +479,8 @@ assert_eq!(Fix::min_value().saturating_abs(), Fix::max_value());
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
", ",
if_signed_unsigned!( if_signed_unsigned!(
$Signedness, $Signedness,
@ -528,9 +503,8 @@ assert_eq!(Fix::from_num(5).wrapping_neg(), Fix::from_bits(neg_five_bits));",
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
let one = Fix::from_num(1); let one = Fix::from_num(1);
let one_minus_bit = one - Fix::from_bits(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::from_num(3).wrapping_add(Fix::from_num(2)), Fix::from_num(5));
@ -548,9 +522,8 @@ assert_eq!(Fix::max_value().wrapping_add(one), ",
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
let one = Fix::from_num(1); let one = Fix::from_num(1);
let one_minus_bit = one - Fix::from_bits(1); let one_minus_bit = one - Fix::from_bits(1);
", ",
@ -573,9 +546,8 @@ assert_eq!(Fix::from_num(0)",
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
assert_eq!(Fix::from_num(3).wrapping_mul(Fix::from_num(2)), Fix::from_num(6)); assert_eq!(Fix::from_num(3).wrapping_mul(Fix::from_num(2)), Fix::from_num(6));
let wrapped = Fix::from_bits(!0 << 2); let wrapped = Fix::from_bits(!0 << 2);
assert_eq!(Fix::max_value().wrapping_mul(Fix::from_num(4)), wrapped); assert_eq!(Fix::max_value().wrapping_mul(Fix::from_num(4)), wrapped);
@ -598,9 +570,8 @@ Panics if the divisor is zero.
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
let one_point_5 = Fix::from_bits(0b11 << (4 - 1)); 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); 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::from_num(1) / 4;
@ -621,9 +592,8 @@ assert_eq!(Fix::max_value().wrapping_div(quarter), wrapped);
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
assert_eq!(Fix::from_num(3).wrapping_mul_int(2), Fix::from_num(6)); assert_eq!(Fix::from_num(3).wrapping_mul_int(2), Fix::from_num(6));
let wrapped = Fix::from_bits(!0 << 2); let wrapped = Fix::from_bits(!0 << 2);
assert_eq!(Fix::max_value().wrapping_mul_int(4), wrapped); assert_eq!(Fix::max_value().wrapping_mul_int(4), wrapped);
@ -655,9 +625,8 @@ Panics if the divisor is zero.
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
// 1.5 is binary 1.1 // 1.5 is binary 1.1
let one_point_5 = Fix::from_bits(0b11 << (4 - 1)); let one_point_5 = Fix::from_bits(0b11 << (4 - 1));
assert_eq!(Fix::from_num(3).wrapping_div_int(2), one_point_5); assert_eq!(Fix::from_num(3).wrapping_div_int(2), one_point_5);
@ -695,9 +664,8 @@ Panics if the divisor is zero.
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
// binary 1.0101 / 8 = binary 0.0010 remainder 0.0101 // binary 1.0101 / 8 = binary 0.0010 remainder 0.0101
assert_eq!(Fix::from_bits(0b10101).wrapping_rem_int(8), Fix::from_bits(0b101)); assert_eq!(Fix::from_bits(0b10101).wrapping_rem_int(8), Fix::from_bits(0b101));
", ",
@ -715,40 +683,32 @@ assert_eq!(Fix::from_bits(0b10101).wrapping_rem_int(8), Fix::from_bits(0b101));
); );
delegate!( delegate!(
"Wrapping shift left. Wraps `rhs` if `rhs` ≥ ", "Wrapping shift left. Wraps `rhs` if `rhs` ≥ ", $s_nbits, ",
$s_nbits, then shifts and returns the number.
", then shifts and returns the number.
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::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), Fix::from_num(4));
assert_eq!((Fix::from_num(1) / 2).wrapping_shl(3 + ", assert_eq!((Fix::from_num(1) / 2).wrapping_shl(3 + ", $s_nbits, "), Fix::from_num(4));
$s_nbits,
"), Fix::from_num(4));
``` ```
"; ";
$Fixed => fn wrapping_shl(self, rhs: u32) $Fixed => fn wrapping_shl(self, rhs: u32)
); );
delegate!( delegate!(
"Wrapping shift right. Wraps `rhs` if `rhs` ≥ ", "Wrapping shift right. Wraps `rhs` if `rhs` ≥ ", $s_nbits, ",
$s_nbits, then shifts and returns the number.
", then shifts and returns the number.
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
assert_eq!((Fix::from_num(4)).wrapping_shr(3), Fix::from_num(1) / 2); assert_eq!((Fix::from_num(4)).wrapping_shr(3), Fix::from_num(1) / 2);
assert_eq!((Fix::from_num(4)).wrapping_shr(3 + ", assert_eq!((Fix::from_num(4)).wrapping_shr(3 + ", $s_nbits, "), Fix::from_num(1) / 2);
$s_nbits,
"), Fix::from_num(1) / 2);
``` ```
"; ";
$Fixed => fn wrapping_shr(self, rhs: u32) $Fixed => fn wrapping_shr(self, rhs: u32)
@ -764,9 +724,8 @@ Overflow can only occur when trying to find the absolute value of the minimum va
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
assert_eq!(Fix::from_num(-5).wrapping_abs(), Fix::from_num(5)); 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_value().wrapping_abs(), Fix::min_value());
``` ```
@ -792,9 +751,8 @@ an overflow has occurred. On overflow, the wrapped value is returned.
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
", ",
if_signed_unsigned!( if_signed_unsigned!(
$Signedness, $Signedness,
@ -827,9 +785,8 @@ overflow has occurred. On overflow, the wrapped value is returned.
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
let one = Fix::from_num(1); let one = Fix::from_num(1);
let one_minus_bit = one - Fix::from_bits(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::from_num(3).overflowing_add(Fix::from_num(2)), (Fix::from_num(5), false));
@ -857,9 +814,8 @@ overflow has occurred. On overflow, the wrapped value is returned.
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
let one = Fix::from_num(1); let one = Fix::from_num(1);
let one_minus_bit = one - Fix::from_bits(1); let one_minus_bit = one - Fix::from_bits(1);
", ",
@ -892,9 +848,8 @@ overflow has occurred. On overflow, the wrapped value is returned.
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
assert_eq!(Fix::from_num(3).overflowing_mul(Fix::from_num(2)), (Fix::from_num(6), false)); assert_eq!(Fix::from_num(3).overflowing_mul(Fix::from_num(2)), (Fix::from_num(6), false));
let wrapped = Fix::from_bits(!0 << 2); let wrapped = Fix::from_bits(!0 << 2);
assert_eq!(Fix::max_value().overflowing_mul(Fix::from_num(4)), (wrapped, true)); assert_eq!(Fix::max_value().overflowing_mul(Fix::from_num(4)), (wrapped, true));
@ -923,9 +878,8 @@ Panics if the divisor is zero.
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
let one_point_5 = Fix::from_bits(0b11 << (4 - 1)); 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)); 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::from_num(1) / 4;
@ -949,9 +903,8 @@ overflow has occurred. On overflow, the wrapped value is returned.
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
assert_eq!(Fix::from_num(3).overflowing_mul_int(2), (Fix::from_num(6), false)); assert_eq!(Fix::from_num(3).overflowing_mul_int(2), (Fix::from_num(6), false));
let wrapped = Fix::from_bits(!0 << 2); let wrapped = Fix::from_bits(!0 << 2);
assert_eq!(Fix::max_value().overflowing_mul_int(4), (wrapped, true)); assert_eq!(Fix::max_value().overflowing_mul_int(4), (wrapped, true));
@ -987,9 +940,8 @@ Panics if the divisor is zero.
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
// 1.5 is binary 1.1 // 1.5 is binary 1.1
let one_point_5 = Fix::from_bits(0b11 << (4 - 1)); let one_point_5 = Fix::from_bits(0b11 << (4 - 1));
assert_eq!(Fix::from_num(3).overflowing_div_int(2), (one_point_5, false)); assert_eq!(Fix::from_num(3).overflowing_div_int(2), (one_point_5, false));
@ -1031,9 +983,8 @@ Panics if the divisor is zero.
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
// binary 1.0101 / 8 = binary 0.0010 remainder 0.0101 // binary 1.0101 / 8 = binary 0.0010 remainder 0.0101
assert_eq!(Fix::from_bits(0b10101).overflowing_rem_int(8), (Fix::from_bits(0b101), false)); assert_eq!(Fix::from_bits(0b10101).overflowing_rem_int(8), (Fix::from_bits(0b101), false));
", ",
@ -1058,20 +1009,16 @@ assert_eq!(Fix::from_bits(0b10101).overflowing_rem_int(8), (Fix::from_bits(0b101
"Overflowing shift left. "Overflowing shift left.
Returns a [tuple] of the shifted value and a [`bool`] indicating whether Returns a [tuple] of the shifted value and a [`bool`] indicating whether
an overflow has occurred. Overflow occurs when `rhs` ", an overflow has occurred. Overflow occurs when `rhs` ", $s_nbits, ".
$s_nbits, On overflow `rhs` is wrapped before the shift operation.
". On overflow `rhs` is wrapped before the shift operation.
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::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), (Fix::from_num(4), false));
assert_eq!((Fix::from_num(1) / 2).overflowing_shl(3 + ", assert_eq!((Fix::from_num(1) / 2).overflowing_shl(3 + ", $s_nbits, "), (Fix::from_num(4), true));
$s_nbits,
"), (Fix::from_num(4), true));
``` ```
[`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html [`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html
@ -1088,20 +1035,16 @@ assert_eq!((Fix::from_num(1) / 2).overflowing_shl(3 + ",
"Overflowing shift right. "Overflowing shift right.
Returns a [tuple] of the shifted value and a [`bool`] indicating whether Returns a [tuple] of the shifted value and a [`bool`] indicating whether
an overflow has occurred. Overflow occurs when `rhs` ", an overflow has occurred. Overflow occurs when `rhs` ", $s_nbits, ".
$s_nbits, On overflow `rhs` is wrapped before the shift operation.
". On overflow `rhs` is wrapped before the shift operation.
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::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), (Fix::from_num(1) / 2, false));
assert_eq!((Fix::from_num(4)).overflowing_shr(3 + ", assert_eq!((Fix::from_num(4)).overflowing_shr(3 + ", $s_nbits, "), (Fix::from_num(1) / 2, true));
$s_nbits,
"), (Fix::from_num(1) / 2, true));
``` ```
[`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html [`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html
@ -1128,9 +1071,8 @@ Overflow can only occur when trying to find the absolute value of the minimum va
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
assert_eq!(Fix::from_num(-5).overflowing_abs(), (Fix::from_num(5), false)); 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_value().overflowing_abs(), (Fix::min_value(), true));
``` ```

View File

@ -22,12 +22,8 @@ representation identical to the given integer.
# Examples # Examples
```rust ```rust
use fixed::{frac::U4, ", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"};
type Fix = ",
$s_fixed,
"<U4>;
// 0010.0000 == 2 // 0010.0000 == 2
assert_eq!(Fix::from_bits(0b10_0000), 2); assert_eq!(Fix::from_bits(0b10_0000), 2);
``` ```
@ -48,12 +44,8 @@ identical to the given fixed-point number.
# Examples # Examples
```rust ```rust
use fixed::{frac::U4, ", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"};
type Fix = ",
$s_fixed,
"<U4>;
// 2 is 0010.0000 // 2 is 0010.0000
assert_eq!(Fix::from_num(2).to_bits(), 0b10_0000); assert_eq!(Fix::from_num(2).to_bits(), 0b10_0000);
``` ```
@ -92,12 +84,8 @@ it panics; if wrapping is required use [`wrapping_from_num`] instead.
# Examples # Examples
```rust ```rust
use fixed::{frac::U4, types::I16F16, ", use fixed::{frac::U4, types::I16F16, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"};
type Fix = ",
$s_fixed,
"<U4>;
// 1.75 is 1.11 in binary // 1.75 is 1.11 in binary
let src = I16F16::from_bits(0b111 << (16 - 2)); let src = I16F16::from_bits(0b111 << (16 - 2));
@ -176,12 +164,8 @@ it panics; if wrapping is required use [`wrapping_to_num`] instead.
# Examples # Examples
```rust ```rust
use fixed::{frac::U4, types::I30F2, ", use fixed::{frac::U4, types::I30F2, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"};
type Fix = ",
$s_fixed,
"<U4>;
// 1.75 is 1.11 in binary // 1.75 is 1.11 in binary
let src = Fix::from_bits(0b111 << (4 - 2)); let src = Fix::from_bits(0b111 << (4 - 2));
@ -261,26 +245,18 @@ The other number can be:
use fixed::{ use fixed::{
frac::{U2, U4}, frac::{U2, U4},
types::I16F16, types::I16F16,
", ", $s_fixed, ",
$s_fixed,
",
}; };
type Fix = ", type Fix = ", $s_fixed, "<U4>;
$s_fixed,
"<U4>;
// 1.75 is 1.11 in binary // 1.75 is 1.11 in binary
let src = I16F16::from_bits(0b111 << (16 - 2)); let src = I16F16::from_bits(0b111 << (16 - 2));
assert_eq!(Fix::checked_from_num(src), Some(Fix::from_bits(0b111 << (4 - 2)))); assert_eq!(Fix::checked_from_num(src), Some(Fix::from_bits(0b111 << (4 - 2))));
let too_large = ", let too_large = ", $s_fixed, "::<U2>::max_value();
$s_fixed,
"::<U2>::max_value();
assert!(Fix::checked_from_num(too_large).is_none()); assert!(Fix::checked_from_num(too_large).is_none());
assert_eq!(Fix::checked_from_num(3), Some(Fix::from_bits(3 << 4))); assert_eq!(Fix::checked_from_num(3), Some(Fix::from_bits(3 << 4)));
let too_large = ", let too_large = ", $s_inner, "::max_value();
$s_inner,
"::max_value();
assert!(Fix::checked_from_num(too_large).is_none()); assert!(Fix::checked_from_num(too_large).is_none());
let too_small = ", let too_small = ",
if_signed_unsigned!( if_signed_unsigned!(
@ -355,21 +331,15 @@ The other number can be:
use fixed::{ use fixed::{
frac::{U0, U4, U6}, frac::{U0, U4, U6},
types::I16F16, types::I16F16,
", ", $s_fixed, ",
$s_fixed,
",
}; };
type Fix = ", type Fix = ", $s_fixed, "<U4>;
$s_fixed,
"<U4>;
// 1.75 is 1.11 in binary // 1.75 is 1.11 in binary
let src = Fix::from_bits(0b111 << (4 - 2)); let src = Fix::from_bits(0b111 << (4 - 2));
let expected = I16F16::from_bits(0b111 << (16 - 2)); let expected = I16F16::from_bits(0b111 << (16 - 2));
assert_eq!(src.checked_to_num::<I16F16>(), Some(expected)); assert_eq!(src.checked_to_num::<I16F16>(), Some(expected));
type TooFewIntBits = ", type TooFewIntBits = ", $s_fixed, "<U6>;
$s_fixed,
"<U6>;
assert!(Fix::max_value().checked_to_num::<TooFewIntBits>().is_none()); assert!(Fix::max_value().checked_to_num::<TooFewIntBits>().is_none());
// 2.5 is 10.1 in binary // 2.5 is 10.1 in binary
@ -382,17 +352,14 @@ assert_eq!(",
"two_point_5.checked_to_num::<i64>(), Some(2", "two_point_5.checked_to_num::<i64>(), Some(2",
), ),
")); "));
type AllInt = ", type AllInt = ", $s_fixed, "<U0>;
$s_fixed,
"<U0>;
assert!(AllInt::", assert!(AllInt::",
if_signed_unsigned!( if_signed_unsigned!(
$Signedness, $Signedness,
"from_bits(-1).checked_to_num::<u", "from_bits(-1).checked_to_num::<u",
"max_value().checked_to_num::<i", "max_value().checked_to_num::<i",
), ),
$s_nbits, $s_nbits, ">().is_none());
">().is_none());
// 1.625 is 1.101 in binary // 1.625 is 1.101 in binary
let one_point_625 = Fix::from_bits(0b1101 << (4 - 3)); let one_point_625 = Fix::from_bits(0b1101 << (4 - 3));
@ -453,20 +420,14 @@ This method panics if the value is a floating-point [NaN].
use fixed::{ use fixed::{
frac::{U2, U4}, frac::{U2, U4},
types::I16F16, types::I16F16,
", ", $s_fixed, ",
$s_fixed,
",
}; };
type Fix = ", type Fix = ", $s_fixed, "<U4>;
$s_fixed,
"<U4>;
// 1.75 is 1.11 in binary // 1.75 is 1.11 in binary
let src = I16F16::from_bits(0b111 << (16 - 2)); let src = I16F16::from_bits(0b111 << (16 - 2));
assert_eq!(Fix::saturating_from_num(src), Fix::from_bits(0b111 << (4 - 2))); assert_eq!(Fix::saturating_from_num(src), Fix::from_bits(0b111 << (4 - 2)));
let too_large = ", let too_large = ", $s_fixed, "::<U2>::max_value();
$s_fixed,
"::<U2>::max_value();
assert_eq!(Fix::saturating_from_num(too_large), Fix::max_value()); assert_eq!(Fix::saturating_from_num(too_large), Fix::max_value());
assert_eq!(Fix::saturating_from_num(3), Fix::from_bits(3 << 4)); assert_eq!(Fix::saturating_from_num(3), Fix::from_bits(3 << 4));
@ -543,44 +504,29 @@ The other number can be:
use fixed::{ use fixed::{
frac::{U0, U4, U6}, frac::{U0, U4, U6},
types::I16F16, types::I16F16,
", ", $s_fixed, ",
$s_fixed,
",
}; };
type Fix = ", type Fix = ", $s_fixed, "<U4>;
$s_fixed,
"<U4>;
// 1.75 is 1.11 in binary // 1.75 is 1.11 in binary
let src = Fix::from_bits(0b111 << (4 - 2)); let src = Fix::from_bits(0b111 << (4 - 2));
let expected = I16F16::from_bits(0b111 << (16 - 2)); let expected = I16F16::from_bits(0b111 << (16 - 2));
assert_eq!(src.saturating_to_num::<I16F16>(), expected); assert_eq!(src.saturating_to_num::<I16F16>(), expected);
type TooFewIntBits = ", type TooFewIntBits = ", $s_fixed, "<U6>;
$s_fixed,
"<U6>;
let saturated = Fix::max_value().saturating_to_num::<TooFewIntBits>(); let saturated = Fix::max_value().saturating_to_num::<TooFewIntBits>();
assert_eq!(saturated, TooFewIntBits::max_value()); assert_eq!(saturated, TooFewIntBits::max_value());
// 2.5 is 10.1 in binary // 2.5 is 10.1 in binary
let two_point_5 = Fix::from_bits(0b101 << (4 - 1)); let two_point_5 = Fix::from_bits(0b101 << (4 - 1));
assert_eq!(two_point_5.saturating_to_num::<i32>(), 2); assert_eq!(two_point_5.saturating_to_num::<i32>(), 2);
type AllInt = ", type AllInt = ", $s_fixed, "<U0>;
$s_fixed,
"<U0>;
assert_eq!(", assert_eq!(",
if_signed_unsigned!( if_signed_unsigned!(
$Signedness, $Signedness,
concat!("AllInt::from_bits(-1).saturating_to_num::<u", $s_nbits, ">(), 0"),
concat!( concat!(
"AllInt::from_bits(-1).saturating_to_num::<u", "AllInt::max_value().saturating_to_num::<i", $s_nbits, ">(), ",
$s_nbits, "i", $s_nbits, "::max_value()",
">(), 0",
),
concat!(
"AllInt::max_value().saturating_to_num::<i",
$s_nbits,
">(), i",
$s_nbits,
"::max_value()",
), ),
), ),
"); ");
@ -642,55 +588,29 @@ For floating-point numbers, panics if the value is not [finite].
use fixed::{ use fixed::{
frac::{U0, U4}, frac::{U0, U4},
types::I16F16, types::I16F16,
", ", $s_fixed, ",
$s_fixed,
",
}; };
type Fix = ", type Fix = ", $s_fixed, "<U4>;
$s_fixed,
"<U4>;
// 1.75 is 1.11 in binary // 1.75 is 1.11 in binary
let src = I16F16::from_bits(0b111 << (16 - 2)); let src = I16F16::from_bits(0b111 << (16 - 2));
assert_eq!(Fix::wrapping_from_num(src), Fix::from_bits(0b111 << (4 - 2))); assert_eq!(Fix::wrapping_from_num(src), Fix::from_bits(0b111 << (4 - 2)));
// integer 0b1101 << (", // integer 0b1101 << (", $s_nbits, " - 7) will wrap to fixed-point 1010...
$s_nbits, let too_large = ", $s_fixed, "::<U0>::from_bits(0b1101 << (", $s_nbits, " - 7));
" - 7) will wrap to fixed-point 1010... let wrapped = Fix::from_bits(0b1010 << (", $s_nbits, " - 4));
let too_large = ",
$s_fixed,
"::<U0>::from_bits(0b1101 << (",
$s_nbits,
" - 7));
let wrapped = Fix::from_bits(0b1010 << (",
$s_nbits,
" - 4));
assert_eq!(Fix::wrapping_from_num(too_large), wrapped); assert_eq!(Fix::wrapping_from_num(too_large), wrapped);
// integer 0b1101 << (", // integer 0b1101 << (", $s_nbits, " - 7) will wrap to fixed-point 1010...
$s_nbits, let large: ", $s_inner, " = 0b1101 << (", $s_nbits, " - 7);
" - 7) will wrap to fixed-point 1010... let wrapped = Fix::from_bits(0b1010 << (", $s_nbits, " - 4));
let large: ",
$s_inner,
" = 0b1101 << (",
$s_nbits,
" - 7);
let wrapped = Fix::from_bits(0b1010 << (",
$s_nbits,
" - 4));
assert_eq!(Fix::wrapping_from_num(large), wrapped); assert_eq!(Fix::wrapping_from_num(large), wrapped);
// 1.75 is 1.11 in binary // 1.75 is 1.11 in binary
let expected = Fix::from_bits(0b111 << (4 - 2)); let expected = Fix::from_bits(0b111 << (4 - 2));
assert_eq!(Fix::wrapping_from_num(1.75f32), expected); assert_eq!(Fix::wrapping_from_num(1.75f32), expected);
// 1.75 << (", // 1.75 << (", $s_nbits, " - 4) wraps to binary 11000...
$s_nbits, let large = 1.75 * 2f32.powi(", $s_nbits, " - 4);
" - 4) wraps to binary 11000... let wrapped = Fix::from_bits(0b1100 << (", $s_nbits, " - 4));
let large = 1.75 * 2f32.powi(",
$s_nbits,
" - 4);
let wrapped = Fix::from_bits(0b1100 << (",
$s_nbits,
" - 4));
assert_eq!(Fix::wrapping_from_num(large), wrapped); assert_eq!(Fix::wrapping_from_num(large), wrapped);
``` ```
@ -744,45 +664,30 @@ The other number can be:
use fixed::{ use fixed::{
frac::{U0, U4, U6}, frac::{U0, U4, U6},
types::I16F16, types::I16F16,
", ", $s_fixed, ",
$s_fixed,
",
}; };
type Fix = ", type Fix = ", $s_fixed, "<U4>;
$s_fixed,
"<U4>;
// 1.75 is 1.11 in binary // 1.75 is 1.11 in binary
let src = Fix::from_bits(0b111 << (4 - 2)); let src = Fix::from_bits(0b111 << (4 - 2));
let expected = I16F16::from_bits(0b111 << (16 - 2)); let expected = I16F16::from_bits(0b111 << (16 - 2));
assert_eq!(src.wrapping_to_num::<I16F16>(), expected); assert_eq!(src.wrapping_to_num::<I16F16>(), expected);
type TooFewIntBits = ", type TooFewIntBits = ", $s_fixed, "<U6>;
$s_fixed,
"<U6>;
let wrapped = TooFewIntBits::from_bits(Fix::max_value().to_bits() << 2); let wrapped = TooFewIntBits::from_bits(Fix::max_value().to_bits() << 2);
assert_eq!(Fix::max_value().wrapping_to_num::<TooFewIntBits>(), wrapped); assert_eq!(Fix::max_value().wrapping_to_num::<TooFewIntBits>(), wrapped);
// 2.5 is 10.1 in binary // 2.5 is 10.1 in binary
let two_point_5 = Fix::from_bits(0b101 << (4 - 1)); let two_point_5 = Fix::from_bits(0b101 << (4 - 1));
assert_eq!(two_point_5.wrapping_to_num::<i32>(), 2); assert_eq!(two_point_5.wrapping_to_num::<i32>(), 2);
type AllInt = ", type AllInt = ", $s_fixed, "<U0>;
$s_fixed,
"<U0>;
assert_eq!(", assert_eq!(",
if_signed_unsigned!( if_signed_unsigned!(
$Signedness, $Signedness,
concat!( concat!(
"AllInt::from_bits(-1).wrapping_to_num::<u", "AllInt::from_bits(-1).wrapping_to_num::<u", $s_nbits, ">(), ",
$s_nbits, "u", $s_nbits, "::max_value()",
">(), u",
$s_nbits,
"::max_value()",
),
concat!(
"AllInt::max_value().wrapping_to_num::<i",
$s_nbits,
">(), -1",
), ),
concat!("AllInt::max_value().wrapping_to_num::<i", $s_nbits, ">(), -1"),
), ),
"); ");
@ -846,57 +751,31 @@ For floating-point numbers, panics if the value is not [finite].
use fixed::{ use fixed::{
frac::{U0, U4}, frac::{U0, U4},
types::I16F16, types::I16F16,
", ", $s_fixed, ",
$s_fixed,
",
}; };
type Fix = ", type Fix = ", $s_fixed, "<U4>;
$s_fixed,
"<U4>;
// 1.75 is 1.11 in binary // 1.75 is 1.11 in binary
let src = I16F16::from_bits(0b111 << (16 - 2)); let src = I16F16::from_bits(0b111 << (16 - 2));
let expected = Fix::from_bits(0b111 << (4 - 2)); let expected = Fix::from_bits(0b111 << (4 - 2));
assert_eq!(Fix::overflowing_from_num(src), (expected, false)); assert_eq!(Fix::overflowing_from_num(src), (expected, false));
// integer 0b1101 << (", // integer 0b1101 << (", $s_nbits, " - 7) will wrap to fixed-point 1010...
$s_nbits, let too_large = ", $s_fixed, "::<U0>::from_bits(0b1101 << (", $s_nbits, " - 7));
" - 7) will wrap to fixed-point 1010... let wrapped = Fix::from_bits(0b1010 << (", $s_nbits, " - 4));
let too_large = ",
$s_fixed,
"::<U0>::from_bits(0b1101 << (",
$s_nbits,
" - 7));
let wrapped = Fix::from_bits(0b1010 << (",
$s_nbits,
" - 4));
assert_eq!(Fix::overflowing_from_num(too_large), (wrapped, true)); assert_eq!(Fix::overflowing_from_num(too_large), (wrapped, true));
assert_eq!(Fix::overflowing_from_num(3), (Fix::from_bits(3 << 4), false)); assert_eq!(Fix::overflowing_from_num(3), (Fix::from_bits(3 << 4), false));
// integer 0b1101 << (", // integer 0b1101 << (", $s_nbits, " - 7) will wrap to fixed-point 1010...
$s_nbits, let large: ", $s_inner, " = 0b1101 << (", $s_nbits, " - 7);
" - 7) will wrap to fixed-point 1010... let wrapped = Fix::from_bits(0b1010 << (", $s_nbits, " - 4));
let large: ",
$s_inner,
" = 0b1101 << (",
$s_nbits,
" - 7);
let wrapped = Fix::from_bits(0b1010 << (",
$s_nbits,
" - 4));
assert_eq!(Fix::overflowing_from_num(large), (wrapped, true)); assert_eq!(Fix::overflowing_from_num(large), (wrapped, true));
// 1.75 is 1.11 in binary // 1.75 is 1.11 in binary
let expected = Fix::from_bits(0b111 << (4 - 2)); let expected = Fix::from_bits(0b111 << (4 - 2));
assert_eq!(Fix::overflowing_from_num(1.75f32), (expected, false)); assert_eq!(Fix::overflowing_from_num(1.75f32), (expected, false));
// 1.75 << (", // 1.75 << (", $s_nbits, " - 4) wraps to binary 11000...
$s_nbits, let large = 1.75 * 2f32.powi(", $s_nbits, " - 4);
" - 4) wraps to binary 11000... let wrapped = Fix::from_bits(0b1100 << (", $s_nbits, " - 4));
let large = 1.75 * 2f32.powi(",
$s_nbits,
" - 4);
let wrapped = Fix::from_bits(0b1100 << (",
$s_nbits,
" - 4));
assert_eq!(Fix::overflowing_from_num(large), (wrapped, true)); assert_eq!(Fix::overflowing_from_num(large), (wrapped, true));
``` ```
@ -954,30 +833,22 @@ The other number can be:
use fixed::{ use fixed::{
frac::{U0, U4, U6}, frac::{U0, U4, U6},
types::I16F16, types::I16F16,
", ", $s_fixed, ",
$s_fixed,
",
}; };
type Fix = ", type Fix = ", $s_fixed, "<U4>;
$s_fixed,
"<U4>;
// 1.75 is 1.11 in binary // 1.75 is 1.11 in binary
let src = Fix::from_bits(0b111 << (4 - 2)); let src = Fix::from_bits(0b111 << (4 - 2));
let expected = I16F16::from_bits(0b111 << (16 - 2)); let expected = I16F16::from_bits(0b111 << (16 - 2));
assert_eq!(src.overflowing_to_num::<I16F16>(), (expected, false)); assert_eq!(src.overflowing_to_num::<I16F16>(), (expected, false));
type TooFewIntBits = ", type TooFewIntBits = ", $s_fixed, "<U6>;
$s_fixed,
"<U6>;
let wrapped = TooFewIntBits::from_bits(Fix::max_value().to_bits() << 2); let wrapped = TooFewIntBits::from_bits(Fix::max_value().to_bits() << 2);
assert_eq!(Fix::max_value().overflowing_to_num::<TooFewIntBits>(), (wrapped, true)); assert_eq!(Fix::max_value().overflowing_to_num::<TooFewIntBits>(), (wrapped, true));
// 2.5 is 10.1 in binary // 2.5 is 10.1 in binary
let two_point_5 = Fix::from_bits(0b101 << (4 - 1)); let two_point_5 = Fix::from_bits(0b101 << (4 - 1));
assert_eq!(two_point_5.overflowing_to_num::<i32>(), (2, false)); assert_eq!(two_point_5.overflowing_to_num::<i32>(), (2, false));
let does_not_fit = ", let does_not_fit = ", $s_fixed, "::<U0>::",
$s_fixed,
"::<U0>::",
if_signed_unsigned!($Signedness, "from_bits(-1)", "max_value()"), if_signed_unsigned!($Signedness, "from_bits(-1)", "max_value()"),
"; ";
let wrapped = ", let wrapped = ",
@ -989,8 +860,7 @@ let wrapped = ",
"; ";
assert_eq!(does_not_fit.overflowing_to_num::<", assert_eq!(does_not_fit.overflowing_to_num::<",
if_signed_unsigned!($Signedness, "u", "i"), if_signed_unsigned!($Signedness, "u", "i"),
$s_nbits, $s_nbits, ">(), (wrapped, true));
">(), (wrapped, true));
// 1.625 is 1.101 in binary // 1.625 is 1.101 in binary
let one_point_625 = Fix::from_bits(0b1101 << (4 - 3)); let one_point_625 = Fix::from_bits(0b1101 << (4 - 3));
@ -1030,12 +900,8 @@ assert_eq!(one_point_625.overflowing_to_num::<f32>(), (1.625f32, false));
# Examples # Examples
```rust ```rust
use fixed::{frac::U4, ", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"};
type Fix = ",
$s_fixed,
"<U4>;
// 1.75 is 1.11 in binary // 1.75 is 1.11 in binary
let f = Fix::from_str_binary(\"1.11\"); let f = Fix::from_str_binary(\"1.11\");
let check = Fix::from_bits(0b111 << (4 - 2)); let check = Fix::from_bits(0b111 << (4 - 2));
@ -1061,12 +927,8 @@ assert_eq!(neg, Ok(-check));
# Examples # Examples
```rust ```rust
use fixed::{frac::U4, ", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"};
type Fix = ",
$s_fixed,
"<U4>;
// 1.75 is 1.11 in binary, 1.6 in octal // 1.75 is 1.11 in binary, 1.6 in octal
let f = Fix::from_str_octal(\"1.6\"); let f = Fix::from_str_octal(\"1.6\");
let check = Fix::from_bits(0b111 << (4 - 2)); let check = Fix::from_bits(0b111 << (4 - 2));
@ -1092,12 +954,8 @@ assert_eq!(neg, Ok(-check));
# Examples # Examples
```rust ```rust
use fixed::{frac::U4, ", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"};
type Fix = ",
$s_fixed,
"<U4>;
// 1.75 is 1.11 in binary, 1.C in hexadecimal // 1.75 is 1.11 in binary, 1.C in hexadecimal
let f = Fix::from_str_hex(\"1.C\"); let f = Fix::from_str_hex(\"1.C\");
let check = Fix::from_bits(0b111 << (4 - 2)); let check = Fix::from_bits(0b111 << (4 - 2));

View File

@ -24,20 +24,15 @@ macro_rules! fixed_round {
"Note that since the numbers are stored in twos "Note that since the numbers are stored in twos
complement, negative numbers with non-zero fractional parts will be complement, negative numbers with non-zero fractional parts will be
rounded towards , except in the case where there are no integer rounded towards , except in the case where there are no integer
bits, that is `", bits, that is `", $s_fixed, "<U", $s_nbits, ">`, where the return value is always zero.
$s_fixed,
"<U",
$s_nbits,
">`, where the return value is always zero.
", ",
), ),
"# Examples "# Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
// 0010.0000 // 0010.0000
let two = Fix::from_num(2); let two = Fix::from_num(2);
// 0010.0100 // 0010.0100
@ -68,11 +63,8 @@ assert_eq!((-two_and_quarter).int(), -three);
$Signedness, $Signedness,
"Note that since the numbers are stored in twos "Note that since the numbers are stored in twos
complement, the returned fraction will be non-negative for negative complement, the returned fraction will be non-negative for negative
numbers, except in the case where there are no integer bits, that is `", numbers, except in the case where there are no integer bits, that is
$s_fixed, `", $s_fixed, "<U", $s_nbits, ">` where the return value is always equal to
"<U",
$s_nbits,
">` where the return value is always equal to
`self`. `self`.
", ",
@ -80,9 +72,8 @@ numbers, except in the case where there are no integer bits, that is `",
"# Examples "# Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
// 0000.0100 // 0000.0100
let quarter = Fix::from_num(1) / 4; let quarter = Fix::from_num(1) / 4;
// 0010.0100 // 0010.0100
@ -118,9 +109,8 @@ it panics; if wrapping is required use [`wrapping_ceil`] instead.
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
let two_half = Fix::from_num(5) / 2; let two_half = Fix::from_num(5) / 2;
assert_eq!(two_half.ceil(), Fix::from_num(3)); assert_eq!(two_half.ceil(), Fix::from_num(3));
", ",
@ -162,9 +152,8 @@ Overflow can only occur when there are zero integer bits.
"# Examples "# Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
let two_half = Fix::from_num(5) / 2; let two_half = Fix::from_num(5) / 2;
assert_eq!(two_half.floor(), Fix::from_num(2)); assert_eq!(two_half.floor(), Fix::from_num(2));
", ",
@ -200,9 +189,8 @@ it panics; if wrapping is required use [`wrapping_round`] instead.
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, r"<U4>;
r"<fixed::frac::U4>;
let two_half = Fix::from_num(5) / 2; let two_half = Fix::from_num(5) / 2;
assert_eq!(two_half.round(), Fix::from_num(3)); assert_eq!(two_half.round(), Fix::from_num(3));
", ",
@ -231,9 +219,8 @@ returning [`None`] on overflow.
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
let two_half = Fix::from_num(5) / 2; let two_half = Fix::from_num(5) / 2;
assert_eq!(two_half.checked_ceil(), Some(Fix::from_num(3))); assert_eq!(two_half.checked_ceil(), Some(Fix::from_num(3)));
", ",
@ -268,20 +255,26 @@ Overflow can only occur when there are zero integer bits.",
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{",
$s_fixed, if_signed_unsigned!(
"<fixed::frac::U4>; $Signedness,
concat!(
"
frac::{U4, U", $s_nbits, "},
", $s_fixed, ",
",
),
concat!("frac::U4, ", $s_fixed),
),
"};
type Fix = ", $s_fixed, "<U4>;
let two_half = Fix::from_num(5) / 2; let two_half = Fix::from_num(5) / 2;
assert_eq!(two_half.checked_floor(), Some(Fix::from_num(2))); assert_eq!(two_half.checked_floor(), Some(Fix::from_num(2)));
", ",
if_signed_else_empty_str!( if_signed_else_empty_str!(
$Signedness, $Signedness,
"assert_eq!((-two_half).checked_floor(), Some(Fix::from_num(-3))); "assert_eq!((-two_half).checked_floor(), Some(Fix::from_num(-3)));
type AllFrac = fixed::", type AllFrac = ", $s_fixed, "<U", $s_nbits, ">;
$s_fixed,
"<fixed::frac::U",
$s_nbits,
">;
assert!(AllFrac::min_value().checked_floor().is_none()); assert!(AllFrac::min_value().checked_floor().is_none());
", ",
), ),
@ -310,9 +303,8 @@ rounded away from zero, returning [`None`] on overflow.
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
let two_half = Fix::from_num(5) / 2; let two_half = Fix::from_num(5) / 2;
assert_eq!(two_half.checked_round(), Some(Fix::from_num(3))); assert_eq!(two_half.checked_round(), Some(Fix::from_num(3)));
", ",
@ -340,9 +332,8 @@ saturating on overflow.
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
let two_half = Fix::from_num(5) / 2; let two_half = Fix::from_num(5) / 2;
assert_eq!(two_half.saturating_ceil(), Fix::from_num(3)); assert_eq!(two_half.saturating_ceil(), Fix::from_num(3));
", ",
@ -375,20 +366,26 @@ Overflow can only occur when there are zero integer bits.",
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{",
$s_fixed, if_signed_unsigned!(
"<fixed::frac::U4>; $Signedness,
concat!(
"
frac::{U4, U", $s_nbits, "},
", $s_fixed, ",
",
),
concat!("frac::U4, ", $s_fixed),
),
"};
type Fix = ", $s_fixed, "<U4>;
let two_half = Fix::from_num(5) / 2; let two_half = Fix::from_num(5) / 2;
assert_eq!(two_half.saturating_floor(), Fix::from_num(2)); assert_eq!(two_half.saturating_floor(), Fix::from_num(2));
", ",
if_signed_else_empty_str!( if_signed_else_empty_str!(
$Signedness, $Signedness,
"assert_eq!((-two_half).saturating_floor(), Fix::from_num(-3)); "assert_eq!((-two_half).saturating_floor(), Fix::from_num(-3));
type AllFrac = fixed::", type AllFrac = ", $s_fixed, "<U", $s_nbits, ">;
$s_fixed,
"<fixed::frac::U",
$s_nbits,
">;
assert_eq!(AllFrac::min_value().saturating_floor(), AllFrac::min_value()); assert_eq!(AllFrac::min_value().saturating_floor(), AllFrac::min_value());
", ",
), ),
@ -408,9 +405,8 @@ ties rounded away from zero, and saturating on overflow.
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
let two_half = Fix::from_num(5) / 2; let two_half = Fix::from_num(5) / 2;
assert_eq!(two_half.saturating_round(), Fix::from_num(3)); assert_eq!(two_half.saturating_round(), Fix::from_num(3));
", ",
@ -441,9 +437,8 @@ wrapping on overflow.
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
let two_half = Fix::from_num(5) / 2; let two_half = Fix::from_num(5) / 2;
assert_eq!(two_half.wrapping_ceil(), Fix::from_num(3)); assert_eq!(two_half.wrapping_ceil(), Fix::from_num(3));
", ",
@ -475,20 +470,26 @@ Overflow can only occur when there are zero integer bits.",
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{",
$s_fixed, if_signed_unsigned!(
"<fixed::frac::U4>; $Signedness,
concat!(
"
frac::{U4, U", $s_nbits, "},
", $s_fixed, ",
",
),
concat!("frac::U4, ", $s_fixed),
),
"};
type Fix = ", $s_fixed, "<U4>;
let two_half = Fix::from_num(5) / 2; let two_half = Fix::from_num(5) / 2;
assert_eq!(two_half.wrapping_floor(), Fix::from_num(2)); assert_eq!(two_half.wrapping_floor(), Fix::from_num(2));
", ",
if_signed_else_empty_str!( if_signed_else_empty_str!(
$Signedness, $Signedness,
"assert_eq!((-two_half).wrapping_floor(), Fix::from_num(-3)); "assert_eq!((-two_half).wrapping_floor(), Fix::from_num(-3));
type AllFrac = fixed::", type AllFrac = ", $s_fixed, "<U", $s_nbits, ">;
$s_fixed,
"<fixed::frac::U",
$s_nbits,
">;
assert_eq!(AllFrac::min_value().wrapping_floor(), AllFrac::from_num(0)); assert_eq!(AllFrac::min_value().wrapping_floor(), AllFrac::from_num(0));
", ",
), ),
@ -507,9 +508,8 @@ nearest, with ties rounded away from zero, and wrapping on overflow.
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
let two_half = Fix::from_num(5) / 2; let two_half = Fix::from_num(5) / 2;
assert_eq!(two_half.wrapping_round(), Fix::from_num(3)); assert_eq!(two_half.wrapping_round(), Fix::from_num(3));
", ",
@ -537,9 +537,8 @@ returned.
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
let two_half = Fix::from_num(5) / 2; let two_half = Fix::from_num(5) / 2;
assert_eq!(two_half.overflowing_ceil(), (Fix::from_num(3), false)); assert_eq!(two_half.overflowing_ceil(), (Fix::from_num(3), false));
", ",
@ -591,20 +590,26 @@ occur when there are zero integer bits.",
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{",
$s_fixed, if_signed_unsigned!(
"<fixed::frac::U4>; $Signedness,
concat!(
"
frac::{U4, U", $s_nbits, "},
", $s_fixed, ",
",
),
concat!("frac::U4, ", $s_fixed),
),
"};
type Fix = ", $s_fixed, "<U4>;
let two_half = Fix::from_num(5) / 2; let two_half = Fix::from_num(5) / 2;
assert_eq!(two_half.overflowing_floor(), (Fix::from_num(2), false)); assert_eq!(two_half.overflowing_floor(), (Fix::from_num(2), false));
", ",
if_signed_else_empty_str!( if_signed_else_empty_str!(
$Signedness, $Signedness,
"assert_eq!((-two_half).overflowing_floor(), (Fix::from_num(-3), false)); "assert_eq!((-two_half).overflowing_floor(), (Fix::from_num(-3), false));
type AllFrac = fixed::", type AllFrac = ", $s_fixed, "<U", $s_nbits, ">;
$s_fixed,
"<fixed::frac::U",
$s_nbits,
">;
assert_eq!(AllFrac::min_value().overflowing_floor(), (AllFrac::from_num(0), true)); assert_eq!(AllFrac::min_value().overflowing_floor(), (AllFrac::from_num(0), true));
", ",
), ),
@ -637,9 +642,8 @@ returned.
# Examples # Examples
```rust ```rust
type Fix = fixed::", use fixed::{frac::U4, ", $s_fixed, "};
$s_fixed, type Fix = ", $s_fixed, "<U4>;
"<fixed::frac::U4>;
let two_half = Fix::from_num(5) / 2; let two_half = Fix::from_num(5) / 2;
assert_eq!(two_half.overflowing_round(), (Fix::from_num(3), false)); assert_eq!(two_half.overflowing_round(), (Fix::from_num(3), false));
", ",

View File

@ -37,11 +37,10 @@ use core::{
/// # Examples /// # Examples
/// ///
/// ```rust /// ```rust
/// use fixed::Wrapping; /// use fixed::{types::I16F16, Wrapping};
/// type Fix = fixed::types::I16F16; /// let max = Wrapping(I16F16::max_value());
/// let max = Wrapping(Fix::max_value()); /// let delta = Wrapping(I16F16::from_bits(1));
/// let delta = Wrapping(Fix::from_bits(1)); /// assert_eq!(I16F16::min_value(), (max + delta).0);
/// assert_eq!(Fix::min_value(), (max + delta).0);
/// ``` /// ```
#[repr(transparent)] #[repr(transparent)]
#[derive(Clone, Copy, Default, Hash, Debug, Eq, PartialEq, Ord, PartialOrd)] #[derive(Clone, Copy, Default, Hash, Debug, Eq, PartialEq, Ord, PartialOrd)]
@ -122,11 +121,10 @@ impl<F: Fixed> Wrapping<F> {
/// # Examples /// # Examples
/// ///
/// ```rust /// ```rust
/// use fixed::Wrapping; /// use fixed::{types::I16F16, Wrapping};
/// type Fix = fixed::types::I16F16; /// let two_half = Wrapping(I16F16::from_num(5) / 2);
/// let two_half = Wrapping(Fix::from_num(5) / 2); /// assert_eq!(two_half.ceil(), Wrapping(I16F16::from_num(3)));
/// assert_eq!(two_half.ceil(), Wrapping(Fix::from_num(3))); /// assert_eq!(Wrapping(I16F16::max_value()).ceil(), Wrapping(I16F16::min_value()));
/// assert_eq!(Wrapping(Fix::max_value()).ceil(), Wrapping(Fix::min_value()));
/// ``` /// ```
pub fn ceil(self) -> Wrapping<F> { pub fn ceil(self) -> Wrapping<F> {
Wrapping(self.0.wrapping_ceil()) Wrapping(self.0.wrapping_ceil())
@ -141,12 +139,13 @@ impl<F: Fixed> Wrapping<F> {
/// # Examples /// # Examples
/// ///
/// ```rust /// ```rust
/// use fixed::Wrapping; /// use fixed::{
/// type Fix = fixed::types::I16F16; /// types::{I0F32, I16F16},
/// let two_half = Wrapping(Fix::from_num(5) / 2); /// Wrapping,
/// assert_eq!(two_half.floor(), Wrapping(Fix::from_num(2))); /// };
/// type AllFrac = fixed::types::I0F32; /// let two_half = Wrapping(I16F16::from_num(5) / 2);
/// assert_eq!(Wrapping(AllFrac::min_value()).floor(), Wrapping(AllFrac::from_num(0))); /// assert_eq!(two_half.floor(), Wrapping(I16F16::from_num(2)));
/// assert_eq!(Wrapping(I0F32::min_value()).floor(), Wrapping(I0F32::from_num(0)));
/// ``` /// ```
pub fn floor(self) -> Wrapping<F> { pub fn floor(self) -> Wrapping<F> {
Wrapping(self.0.wrapping_floor()) Wrapping(self.0.wrapping_floor())
@ -158,12 +157,11 @@ impl<F: Fixed> Wrapping<F> {
/// # Examples /// # Examples
/// ///
/// ```rust /// ```rust
/// use fixed::Wrapping; /// use fixed::{types::I16F16, Wrapping};
/// type Fix = fixed::types::I16F16; /// let two_half = Wrapping(I16F16::from_num(5) / 2);
/// let two_half = Wrapping(Fix::from_num(5) / 2); /// assert_eq!(two_half.round(), Wrapping(I16F16::from_num(3)));
/// assert_eq!(two_half.round(), Wrapping(Fix::from_num(3))); /// assert_eq!((-two_half).round(), Wrapping(I16F16::from_num(-3)));
/// assert_eq!((-two_half).round(), Wrapping(Fix::from_num(-3))); /// assert_eq!(Wrapping(I16F16::max_value()).round(), Wrapping(I16F16::min_value()));
/// assert_eq!(Wrapping(Fix::max_value()).round(), Wrapping(Fix::min_value()));
/// ``` /// ```
pub fn round(self) -> Wrapping<F> { pub fn round(self) -> Wrapping<F> {
Wrapping(self.0.wrapping_round()) Wrapping(self.0.wrapping_round())
@ -178,10 +176,9 @@ impl<F: Fixed> Wrapping<F> {
/// # Examples /// # Examples
/// ///
/// ```rust /// ```rust
/// use fixed::Wrapping; /// use fixed::{types::I16F16, Wrapping};
/// type Fix = fixed::types::I16F16; /// assert_eq!(Wrapping(I16F16::from_num(-5)).abs(), Wrapping(I16F16::from_num(5)));
/// assert_eq!(Wrapping(Fix::from_num(-5)).abs(), Wrapping(Fix::from_num(5))); /// assert_eq!(Wrapping(I16F16::min_value()).abs(), Wrapping(I16F16::min_value()));
/// assert_eq!(Wrapping(Fix::min_value()).abs(), Wrapping(Fix::min_value()));
/// ``` /// ```
pub fn abs(self) -> Wrapping<F> pub fn abs(self) -> Wrapping<F>
where where