RoundFrom

Trait RoundFrom 

Source
pub trait RoundFrom<T> {
    // Required method
    fn round_from(value: T) -> Self;
}
Expand description

Used to do value-to-value conversions using the rules prescribed for that conversion in the posit standard, which may round the input (see below). It is the reciprocal of RoundInto.

The interface is identical to the standard From, but for the conversions described in the posit standard; therefore — unlike that which is the convention for the From trait — these conversions are not necessarily lossless, and may round if necessary, according the definition in the posit standard.

The exact meaning of these conversions depends on the types involved; for the exact description of what each particular conversion does, consult the documentation for specific implementations of round_from.

Many of the usage guidelines for From also apply to RoundFrom: if you do implement it for your types, prefer implementing RoundFrom over RoundInto because implementing RoundFrom automatically provides one with an implementation of RoundInto, and prefer using RoundInto over RoundFrom when specifying trait bounds on a generic function. There’s also a blanket implementation of RoundFrom<T> for T, and RoundFrom<T> for U implies RoundInto<U> for T.

§Rounding

“Rounding” in this crate stands for the definition in the posit standard. In short:

  • If the value is greater in absolute value than the biggest posit, round to it (i.e., never overflow).
  • If the value is smaller in absolute value than the smallest positive posit, round to it (i.e., never underflow).
  • Otherwise, round to the nearest bit pattern, or in case of a tie, to the even bit pattern.

§Examples

Rounding from ints, floats:

assert!(p16::round_from(1) == p16::round_from(1.00000001));
assert!(p32::round_from(1) <  p32::round_from(1.00000001));

assert_eq!(p32::round_from(f64::NAN), p32::NAR);

Rounding to ints, floats:

assert_eq!(f32::round_from(p16::MIN_POSITIVE), 1.3877788e-17);
assert_eq!(i64::round_from(p8::MAX), 1 << 24);

assert!(f64::round_from(p32::NAR).is_nan());

Required Methods§

Source

fn round_from(value: T) -> Self

Converts to this type from the input type, according to the rules prescribed in the posit standard for this particular conversion.

This is the rounding conversion that is specified in the posit standard (see Rounding); if you’re looking for the usual Rust-y conversions (From if exact, TryFrom if fallible), use those traits instead.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<const N: u32, const ES: u32, Int: Int> RoundFrom<Posit<N, ES, Int>> for f32

Source§

fn round_from(value: Posit<N, ES, Int>) -> Self

Convert a Posit into an f32, rounding according to the standard:

  • If the value is 0, the result is +0.0.
  • If the value is NaR, the result is a quiet NaN.
  • Otherwise, the posit value is rounded to a float (if necessary) using the “roundTiesToEven” rule in the IEEE 754 standard (in short: underflow to ±0, overflow to ±∞, otherwise round to nearest, in case of a tie round to nearest even bit pattern).
Source§

impl<const N: u32, const ES: u32, Int: Int> RoundFrom<Posit<N, ES, Int>> for f64

Source§

fn round_from(value: Posit<N, ES, Int>) -> Self

Convert a Posit into an f64, rounding according to the standard:

  • If the value is 0, the result is +0.0.
  • If the value is NaR, the result is a quiet NaN.
  • Otherwise, the posit value is rounded to a float (if necessary) using the “roundTiesToEven” rule in the IEEE 754 standard (in short: underflow to ±0, overflow to ±∞, otherwise round to nearest, in case of a tie round to nearest even bit pattern).
Source§

impl<const N: u32, const ES: u32, Int: Int> RoundFrom<Posit<N, ES, Int>> for i8

Source§

fn round_from(value: Posit<N, ES, Int>) -> Self

Convert a Posit into an i8, rounding according to the standard:

  • If the value is NaR, or if overflows the target type, then it converts to i8::MIN (i.e. the value where the most significant bit is 1 and the rest are 0).
  • Otherwise, it returns the nearest integer to value, rounding ties to even.
Source§

impl<const N: u32, const ES: u32, Int: Int> RoundFrom<Posit<N, ES, Int>> for i16

Source§

fn round_from(value: Posit<N, ES, Int>) -> Self

Convert a Posit into an i16, rounding according to the standard:

  • If the value is NaR, or if overflows the target type, then it converts to i16::MIN (i.e. the value where the most significant bit is 1 and the rest are 0).
  • Otherwise, it returns the nearest integer to value, rounding ties to even.
Source§

impl<const N: u32, const ES: u32, Int: Int> RoundFrom<Posit<N, ES, Int>> for i32

Source§

fn round_from(value: Posit<N, ES, Int>) -> Self

Convert a Posit into an i32, rounding according to the standard:

  • If the value is NaR, or if overflows the target type, then it converts to i32::MIN (i.e. the value where the most significant bit is 1 and the rest are 0).
  • Otherwise, it returns the nearest integer to value, rounding ties to even.
Source§

impl<const N: u32, const ES: u32, Int: Int> RoundFrom<Posit<N, ES, Int>> for i64

Source§

fn round_from(value: Posit<N, ES, Int>) -> Self

Convert a Posit into an i64, rounding according to the standard:

  • If the value is NaR, or if overflows the target type, then it converts to i64::MIN (i.e. the value where the most significant bit is 1 and the rest are 0).
  • Otherwise, it returns the nearest integer to value, rounding ties to even.
Source§

impl<const N: u32, const ES: u32, Int: Int> RoundFrom<Posit<N, ES, Int>> for i128

Source§

fn round_from(value: Posit<N, ES, Int>) -> Self

Convert a Posit into an i128, rounding according to the standard:

  • If the value is NaR, or if overflows the target type, then it converts to i128::MIN (i.e. the value where the most significant bit is 1 and the rest are 0).
  • Otherwise, it returns the nearest integer to value, rounding ties to even.

Implementors§

Source§

impl<T> RoundFrom<T> for T

Source§

impl<const N: u32, const ES: u32, Int: Int> RoundFrom<f32> for Posit<N, ES, Int>

Source§

impl<const N: u32, const ES: u32, Int: Int> RoundFrom<f64> for Posit<N, ES, Int>

Source§

impl<const N: u32, const ES: u32, Int: Int> RoundFrom<i8> for Posit<N, ES, Int>

Source§

impl<const N: u32, const ES: u32, Int: Int> RoundFrom<i16> for Posit<N, ES, Int>

Source§

impl<const N: u32, const ES: u32, Int: Int> RoundFrom<i32> for Posit<N, ES, Int>

Source§

impl<const N: u32, const ES: u32, Int: Int> RoundFrom<i64> for Posit<N, ES, Int>

Source§

impl<const N: u32, const ES: u32, Int: Int> RoundFrom<i128> for Posit<N, ES, Int>

Source§

impl<const N: u32, const ES: u32, Int: Int, const SIZE: usize> RoundFrom<&Quire<N, ES, SIZE>> for Posit<N, ES, Int>