pub trait FixedBits: Sealedwhere
Self: Default + Hash + Ord,
Self: Contiguous + Pod,
Self: Debug + Display + Binary + Octal + LowerHex + UpperHex,
Self: FromStr<Err = ParseIntError>,
Self: Add<Output = Self> + AddAssign,
Self: Sub<Output = Self> + SubAssign,
Self: Mul<Output = Self> + MulAssign,
Self: Div<Output = Self> + DivAssign,
Self: Rem<Output = Self> + RemAssign,
Self: Not<Output = Self>,
Self: BitAnd<Output = Self> + BitAndAssign,
Self: BitOr<Output = Self> + BitOrAssign,
Self: BitXor<Output = Self> + BitXorAssign,
Self: Shl<u32, Output = Self> + ShlAssign<u32>,
Self: Shr<u32, Output = Self> + ShrAssign<u32>,
Self: Sum + Product,
Self: FixedEquiv<Equiv = Self::Fixed<0>>,
Self: FixedBitsCast<i8> + FixedBitsCast<i16> + FixedBitsCast<i32>,
Self: FixedBitsCast<i64> + FixedBitsCast<i128> + FixedBitsCast<isize>,
Self: FixedBitsCast<u8> + FixedBitsCast<u16> + FixedBitsCast<u32>,
Self: FixedBitsCast<u64> + FixedBitsCast<u128> + FixedBitsCast<usize>,
Self: FixedBitsOptionalArbitrary,
Self: FixedBitsOptionalBorsh,
Self: FixedBitsOptionalNum,
Self: FixedBitsOptionalSerde,{
type Fixed<const FRAC: i32>: Fixed<Bits = Self>;
const MIN: Self;
const MAX: Self;
const IS_SIGNED: bool;
const BITS: u32;
}
Expand description
This trait is implemented for Fixed::Bits
for all
fixed-point numbers.
This provides some facilities to manipulate bits in generic functions.
This trait is sealed and cannot be implemented for more types; it is
implemented for i8
, i16
, i32
, i64
, i128
, u8
,
u16
, u32
, u64
, and u128
.
Examples
use az::OverflowingAs;
use fixed::{traits::Fixed, types::*};
fn limited_positive_bits<F: Fixed>(fixed: F) -> Option<u32> {
let bits = fixed.to_bits();
match bits.overflowing_as::<u32>() {
(wrapped, false) => Some(wrapped),
(_, true) => None,
}
}
assert_eq!(limited_positive_bits(I16F16::from_bits(100)), Some(100));
assert_eq!(limited_positive_bits(I16F16::from_bits(-100)), None);
Required Associated Types
sourcetype Fixed<const FRAC: i32>: Fixed<Bits = Self>
type Fixed<const FRAC: i32>: Fixed<Bits = Self>
A fixed-point number with this type as the underlying bit representation.
Examples
#![feature(generic_const_exprs)]
use fixed::{
traits::{Fixed, FixedBits},
types::I4F4,
};
fn op<F: Fixed>(f: F) -> F {
let num = F::Bits::try_from(64).ok().unwrap();
// a requires more than 4 integer bits
let a = <F::Bits as FixedBits>::Fixed::<0>::TRY_ONE.unwrap() * num;
// b requires more than 4 fractional bits
let b = <F::Bits as FixedBits>::Fixed::<6>::TRY_ONE.unwrap() / num;
// a * b = 1, so this effectively returns f + 1
f.add_prod(a, b)
}
assert_eq!(op(I4F4::from_num(2.5)), 3.5);