pub trait RoundInto<T> {
// Required method
fn round_into(self) -> T;
}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
RoundFrom.
The interface is identical to the standard Into, but for the conversions described in the
posit standard; therefore — unlike that which is the
convention for the From and Into traits —
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 Into also apply to RoundInto: 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 RoundInto<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_eq!(p16::ONE.next(), 1.0004883_f64.round_into());
assert_eq!(p32::ONE.next(), 1.0000000075_f64.round_into());
assert_eq!(p32::NAR, f64::NAN.round_into());Rounding to ints, floats:
assert_eq!(5.960464477539063e-8, p8::MIN_POSITIVE.round_into());
assert_eq!(1_i64 << 56, p16::MAX.round_into());
assert!(f64::is_nan(p32::NAR.round_into()));Required Methods§
Sourcefn round_into(self) -> T
fn round_into(self) -> T
Converts this type into the (usually inferred) 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
(Into if exact, TryInto if fallible), use those traits instead.