pub trait FixedBits: Sealedwhere
Self: Default + Hash + Ord + Contiguous + Pod + Debug + Display + LowerExp + UpperExp + Binary + Octal + LowerHex + UpperHex + FromStr<Err = ParseIntError> + Add<Output = Self> + AddAssign + Sub<Output = Self> + SubAssign + Mul<Output = Self> + MulAssign + Div<Output = Self> + DivAssign + Rem<Output = Self> + RemAssign + Not<Output = Self> + BitAnd<Output = Self> + BitAndAssign + BitOr<Output = Self> + BitOrAssign + BitXor<Output = Self> + BitXorAssign + Shl<u32, Output = Self> + ShlAssign<u32> + Shr<u32, Output = Self> + ShrAssign<u32> + Sum + Product + FixedEquiv<Equiv = Self::Fixed<0>> + FixedBitsCast<i8> + FixedBitsCast<i16> + FixedBitsCast<i32> + FixedBitsCast<i64> + FixedBitsCast<i128> + FixedBitsCast<isize> + FixedBitsCast<u8> + FixedBitsCast<u16> + FixedBitsCast<u32> + FixedBitsCast<u64> + FixedBitsCast<u128> + FixedBitsCast<usize> + FixedBitsOptionalArbitrary + FixedBitsOptionalBorsh + FixedBitsOptionalNum + 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);