[−][src]Trait fixed::sealed::Float
This trait is implemented for the primitive floating-point types,
and for f16 if the f16 feature is enabled.
This trait is sealed and cannot be implemented for more types; it
is implemented for f32 and f64, and for f16 if the
f16 feature is enabled.
Provided methods
fn from_fixed<F>(val: F) -> Self where
F: Fixed,
F: Fixed,
Converts from a fixed-point number.
This method rounds to the nearest, with ties rounding to even.
Examples
use fixed::sealed::Float; type Fix = fixed::FixedI16<fixed::frac::U8>; // 1.625 is 1.101 in binary let fix = Fix::from_bits(0b1101 << (8 - 3)); assert_eq!(f32::from_fixed(fix), 1.625);
fn to_fixed<F>(self) -> F where
F: Fixed,
F: Fixed,
Converts to a fixed-point number.
This method rounds to the nearest, with ties rounding to even.
Panics
Panics if the value is not finite.
When debug assertions are enabled, also panics if the value
does not fit. When debug assertions are not enabled, the
wrapped value can be returned, but it is not considered a
breaking change if in the future it panics; if wrapping is
required use wrapping_to_fixed instead.
Examples
use fixed::sealed::Float; type Fix = fixed::FixedI16<fixed::frac::U8>; // 1.625 is 1.101 in binary let fix: Fix = 1.625.to_fixed(); assert_eq!(fix, Fix::from_bits(0b1101 << (8 - 3)));
fn checked_to_fixed<F>(self) -> Option<F> where
F: Fixed,
F: Fixed,
Converts to a fixed-point number if it fits, otherwise returns None.
Examples
use fixed::sealed::Float; type Fix = fixed::FixedI16<fixed::frac::U8>; // 1.625 is 1.101 in binary let checked_fix: Option<Fix> = 1.625f32.checked_to_fixed(); let one_point_625 = Fix::from_bits(0b1101 << (8 - 3)); assert_eq!(checked_fix, Some(one_point_625)); assert!(1000f32.checked_to_fixed::<Fix>().is_none()); assert!(std::f64::NAN.checked_to_fixed::<Fix>().is_none());
fn saturating_to_fixed<F>(self) -> F where
F: Fixed,
F: Fixed,
Converts to a fixed-point number, saturating if it does not fit.
Panics
This method panics if the value is NaN.
Examples
use fixed::sealed::Float; type Fix = fixed::FixedI16<fixed::frac::U8>; // 1.625 is 1.101 in binary let fix: Fix = 1.625f32.saturating_to_fixed(); assert_eq!(fix, Fix::from_bits(0b1101 << (8 - 3))); let neg_inf_to_fixed: Fix = std::f64::NEG_INFINITY.saturating_to_fixed(); assert_eq!(neg_inf_to_fixed, Fix::min_value());
fn wrapping_to_fixed<F>(self) -> F where
F: Fixed,
F: Fixed,
Converts to a fixed-point number, wrapping if it does not fit.
Panics
This method panics if the value is not finite.
Examples
use fixed::sealed::Float; type Fix = fixed::FixedU16<fixed::frac::U8>; // 6.5 is 110.1 in binary let six_point_5 = Fix::from_bits(0b1101 << (8 - 1)); assert_eq!(6.5f32.wrapping_to_fixed::<Fix>(), six_point_5); // 1030.5 = 1024 + 6.5, 1024 is a power of 2 that will be wrapped assert_eq!(1030.5f64.wrapping_to_fixed::<Fix>(), six_point_5);
fn overflowing_to_fixed<F>(self) -> (F, bool) where
F: Fixed,
F: Fixed,
Converts to a fixed-point number.
Returns a tuple of the fixed-point number and a bool
indicating whether an overflow has occurred. On overflow, the
wrapped value is returned.
Panics
This method panics if the value is not finite.
Examples
use fixed::sealed::Float; type Fix = fixed::FixedU16<fixed::frac::U8>; // 6.5 is 110.1 in binary let six_point_5 = Fix::from_bits(0b1101 << (8 - 1)); assert_eq!(6.5f32.overflowing_to_fixed::<Fix>(), (six_point_5, false)); // 1030.5 = 1024 + 6.5, 1024 is a power of 2 that will be wrapped assert_eq!(1030.5f64.overflowing_to_fixed::<Fix>(), (six_point_5, true));