pub struct I128<const BITS: u32, const SHIFT: i32>(/* private fields */);
Expand description
#[repr(transparent)]
struct containing
i128
interpreted as a fixed-point number.
Implements the trait fp::Num
for fixed-point manipulation.
Implementations§
Trait Implementations§
Source§impl<const B0: u32, const B1: u32, const S: i32> Add<I128<B1, S>> for I128<B0, S>
Two fixed-point integers with the same raw type and the same shift may be
added together. The result has the same raw type and the same shift. The result
has 1 more bit than the number of bits in the wider of the two inputs.
impl<const B0: u32, const B1: u32, const S: i32> Add<I128<B1, S>> for I128<B0, S>
Two fixed-point integers with the same raw type and the same shift may be added together. The result has the same raw type and the same shift. The result has 1 more bit than the number of bits in the wider of the two inputs.
Source§impl<const B0: u32, const B1: u32, const S0: i32, const S1: i32> Div<I128<B1, S1>> for I128<B0, S0>
impl<const B0: u32, const B1: u32, const S0: i32, const S1: i32> Div<I128<B1, S1>> for I128<B0, S0>
Source§impl<const B0: u32, const B1: u32, const S0: i32, const S1: i32> Div<I128<B1, S1>> for U128<B0, S0>
impl<const B0: u32, const B1: u32, const S0: i32, const S1: i32> Div<I128<B1, S1>> for U128<B0, S0>
Source§impl<const B0: u32, const B1: u32, const S0: i32, const S1: i32> Div<U128<B1, S1>> for I128<B0, S0>
impl<const B0: u32, const B1: u32, const S0: i32, const S1: i32> Div<U128<B1, S1>> for I128<B0, S0>
Source§impl<const B0: u32, const B1: u32, const S0: i32, const S1: i32> Mul<I128<B1, S1>> for I128<B0, S0>
impl<const B0: u32, const B1: u32, const S0: i32, const S1: i32> Mul<I128<B1, S1>> for I128<B0, S0>
Source§impl<const B0: u32, const B1: u32, const S0: i32, const S1: i32> Mul<I128<B1, S1>> for U128<B0, S0>
impl<const B0: u32, const B1: u32, const S0: i32, const S1: i32> Mul<I128<B1, S1>> for U128<B0, S0>
Source§impl<const B0: u32, const B1: u32, const S0: i32, const S1: i32> Mul<U128<B1, S1>> for I128<B0, S0>
impl<const B0: u32, const B1: u32, const S0: i32, const S1: i32> Mul<U128<B1, S1>> for I128<B0, S0>
Source§impl<const B: u32, const S: i32> Neg for I128<B, S>
Any fixed-point integer may be negated. The result of negation is always
signed. Negation adds a bit: unsigned values gain a sign bit; signed values
can overflow from MIN
to -MIN = MAX + 1
. The shift is unchanged.
impl<const B: u32, const S: i32> Neg for I128<B, S>
Any fixed-point integer may be negated. The result of negation is always
signed. Negation adds a bit: unsigned values gain a sign bit; signed values
can overflow from MIN
to -MIN = MAX + 1
. The shift is unchanged.
Source§impl<const BITS: u32, const SHIFT: i32> Num for I128<BITS, SHIFT>
impl<const BITS: u32, const SHIFT: i32> Num for I128<BITS, SHIFT>
Source§unsafe fn from_f32_unchecked(val: f32) -> Self
unsafe fn from_f32_unchecked(val: f32) -> Self
The caller must ensure that val
is within the range of this fixed-point type,
and that the quantity val / 2_f32.powi(-SHIFT)
is finite.
Source§unsafe fn from_f64_unchecked(val: f64) -> Self
unsafe fn from_f64_unchecked(val: f64) -> Self
The caller must ensure that val
is within the range of this fixed-point type,
and that the quantity val / 2_f64.powi(-SHIFT)
is finite.
Source§fn into_f32(self) -> f32
fn into_f32(self) -> f32
Conversion to f32 is guaranteed to be exact. Therefore, this function requires
BITS <= 24
(to prevent truncation), SHIFT <= 149
(to prevent underflow),
and BITS - SHIFT <= 128
(to prevent overflow).
For fixed-point numbers which do not meet these requirements, use raw_shr()
to reduce the number of bits and logical_shr()
to adjust the shift as needed.
Source§fn into_f64(self) -> f64
fn into_f64(self) -> f64
Conversion to f64 is guaranteed to be exact. Therefore, this function requires
BITS <= 53
(to prevent truncation), SHIFT <= 1074
(to prevent underflow),
and BITS - SHIFT <= 1024
(to prevent overflow).
For fixed-point numbers which do not meet these requirements, use raw_shr()
to reduce the number of bits and logical_shr()
to adjust the shift as needed.
Source§const BITS: u32
const BITS: u32
BITS
is the number of least-significant bits which are permitted to vary.
The Raw::BITS - BITS
high-order bits must be zero (for unsigned Raw
) or the
same as the high bit of the lower BITS
bits (for signed Raw
).Source§const SHIFT: i32 = SHIFT
const SHIFT: i32 = SHIFT
SHIFT
sets the scaling factor between the stored raw value (of type Raw
)
and the “logical” value with it represents. The logical value of this
fixed-point number is equal to the raw value divided by 2.pow(SHIFT)
. Read moreSource§type Raw = i128
type Raw = i128
i64
.Source§type Output<const B: u32, const S: i32> = I128<B, S>
type Output<const B: u32, const S: i32> = I128<B, S>
BITS
and/or SHIFT
are changed by an operation.Source§unsafe fn new_unchecked(val: i128) -> Self
unsafe fn new_unchecked(val: i128) -> Self
Self
.
Unsafe: no bounds checking is performed; the caller must ensure that the
result lies between Self::MIN
and Self::MAX
. It is almost always better
to use .new().unwrap()
instead of this function, so that an out-of-bounds
value panics with a reasonable message instead of propagating undefined
behavior.Source§fn raw(self) -> i128
fn raw(self) -> i128
Source§fn new(val: Self::Raw) -> Result<Self, RangeError>
fn new(val: Self::Raw) -> Result<Self, RangeError>
Self
,
or return a RangeError
if it is too small or too large to represent
a valid instance of Self
.Source§fn from_f32(val: f32) -> Result<Self, RangeError>
fn from_f32(val: f32) -> Result<Self, RangeError>
Self
which has a logical value of val
,
or return a RangeError if val
is too small or too large to be represented
by Self
.Source§fn from_f64(val: f64) -> Result<Self, RangeError>
fn from_f64(val: f64) -> Result<Self, RangeError>
Self
which has a logical value of val
,
or return a RangeError if val
is too small or too large to be represented
by Self
.Source§fn from_fp<T: Num, F: Num<Raw = T>>(val: F) -> Self
fn from_fp<T: Num, F: Num<Raw = T>>(val: F) -> Self
Self
which has the same logical value as val
.
F
and Self
must have the same shift and signedness. Self
must have at least as
many bits as F
.Source§fn into_fp<T, F: Num<Raw = T>>(self) -> F
fn into_fp<T, F: Num<Raw = T>>(self) -> F
F
which has the same logical value as self
.
F
and Self
must have the same shift and signedness. F
must have at least as
many bits as Self
.Source§fn add_bits<const N: u32>(self) -> Self::Output<{ _ }, { Self::SHIFT }>
fn add_bits<const N: u32>(self) -> Self::Output<{ _ }, { Self::SHIFT }>
Source§fn set_bits<const N: u32>(
self,
) -> Result<Self::Output<N, { Self::SHIFT }>, RangeError>
fn set_bits<const N: u32>( self, ) -> Result<Self::Output<N, { Self::SHIFT }>, RangeError>
Source§unsafe fn set_bits_unchecked<const N: u32>(
self,
) -> Self::Output<N, { Self::SHIFT }>
unsafe fn set_bits_unchecked<const N: u32>( self, ) -> Self::Output<N, { Self::SHIFT }>
.set_bits().unwrap()
instead, so that an out-of-bounds
value panics with a reasonable message instead of propagating undefined
behavior.Source§fn saturate<const N: u32>(self) -> Self::Output<N, { Self::SHIFT }>
fn saturate<const N: u32>(self) -> Self::Output<N, { Self::SHIFT }>
Source§fn logical_shl<const N: i32>(self) -> Self::Output<{ Self::BITS }, { _ }>
fn logical_shl<const N: i32>(self) -> Self::Output<{ Self::BITS }, { _ }>
Source§fn logical_shr<const N: i32>(self) -> Self::Output<{ Self::BITS }, { _ }>
fn logical_shr<const N: i32>(self) -> Self::Output<{ Self::BITS }, { _ }>
Source§impl<const BITS: u32, const SHIFT: i32> Ord for I128<BITS, SHIFT>
impl<const BITS: u32, const SHIFT: i32> Ord for I128<BITS, SHIFT>
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<const BITS: u32, const SHIFT: i32> PartialOrd for I128<BITS, SHIFT>
impl<const BITS: u32, const SHIFT: i32> PartialOrd for I128<BITS, SHIFT>
Source§impl<const B0: u32, const B1: u32, const S: i32> Sub<I128<B1, S>> for I128<B0, S>
Two fixed-point integers with the same raw type and the same shift may be
subtracted. The result is always signed, even if the inputs were unsigned.
The result has the same shift as the inputs, and 1 more bit than the number
of bits in the wider of the two inputs.
impl<const B0: u32, const B1: u32, const S: i32> Sub<I128<B1, S>> for I128<B0, S>
Two fixed-point integers with the same raw type and the same shift may be subtracted. The result is always signed, even if the inputs were unsigned. The result has the same shift as the inputs, and 1 more bit than the number of bits in the wider of the two inputs.