pub trait ConvFloat<T>: Sized {
// Required methods
fn try_conv_trunc(x: T) -> Result<Self, Error>;
fn try_conv_nearest(x: T) -> Result<Self, Error>;
fn try_conv_floor(x: T) -> Result<Self, Error>;
fn try_conv_ceil(x: T) -> Result<Self, Error>;
// Provided methods
fn conv_trunc(x: T) -> Self { ... }
fn conv_nearest(x: T) -> Self { ... }
fn conv_floor(x: T) -> Self { ... }
fn conv_ceil(x: T) -> Self { ... }
}
Expand description
Nearest / floor / ceiling conversions from floating point types
This trait is explicitly for conversions from floating-point values to integers, supporting four rounding modes.
As with Conv
, the try_conv_*
methods must be implemented and must fail
if conversion to the expected value is not possible. If the source is non-
finite (inf
or NaN
), then Error::Range
should be returned.
The conv_*
methods each have a default implementation over the try_..
variant which panics on failure. Implementations handle errors as follows:
- In debug builds, the methods must panic
- Otherwise, the method may panic or may return a different value; all results must be well-defined and safe.
- Implementations provided by this library will also panic if the
always_assert
orassert_float
feature flag is used.
The sister-trait CastFloat
supports “into” style usage.
Required Methods§
fn try_conv_trunc(x: T) -> Result<Self, Error>
fn try_conv_trunc(x: T) -> Result<Self, Error>
Try converting to integer with truncation
Rounds towards zero (same as as
).
fn try_conv_nearest(x: T) -> Result<Self, Error>
fn try_conv_nearest(x: T) -> Result<Self, Error>
Try converting to the nearest integer
Half-way cases are rounded away from 0
.
fn try_conv_floor(x: T) -> Result<Self, Error>
fn try_conv_floor(x: T) -> Result<Self, Error>
Try converting the floor to an integer
Returns the largest integer less than or equal to x
.
fn try_conv_ceil(x: T) -> Result<Self, Error>
fn try_conv_ceil(x: T) -> Result<Self, Error>
Try convert the ceiling to an integer
Returns the smallest integer greater than or equal to x
.
Provided Methods§
fn conv_trunc(x: T) -> Self
fn conv_trunc(x: T) -> Self
Convert to integer with truncatation
Rounds towards zero (same as as
).
fn conv_nearest(x: T) -> Self
fn conv_nearest(x: T) -> Self
Convert to the nearest integer
Half-way cases are rounded away from 0
.
fn conv_floor(x: T) -> Self
fn conv_floor(x: T) -> Self
Convert the floor to an integer
Returns the largest integer less than or equal to x
.
fn conv_ceil(x: T) -> Self
fn conv_ceil(x: T) -> Self
Convert the ceiling to an integer
Returns the smallest integer greater than or equal to x
.