Trait easy_cast::ConvFloat [−][src]
Nearest / floor / ceil conversions from floating point types
This trait is explicitly for conversions from floating-point values to integers, supporting four rounding modes for fallible and for “success expected” conversions.
Two sets of methods are provided:
conv_*
methods are for “success expected” conversions. In debug builds and when using thealways_assert
or theassert_float
feature flag, out-of-range conversions will panic. In other cases, conversions may produce incorrect values (according to the behaviour of as, which is saturating cast since Rust 1.45.0 and undefined for older compilers). Non-finite source values (inf
andNaN
) are considered out-of-range.try_conv_*
methods are for fallible conversions and always produce an error if the conversion would be out of range.
For f64
to f32
where loss-of-precision is allowable, it is probably
acceptable to use as
(and if need be, check that the result is finite
with x.is_finite()
). The reverse, f32
to f64
, is always exact.
Required methods
fn conv_trunc(x: T) -> Self
[src]
Convert to integer with truncatation
Rounds towards zero (same as as
).
fn conv_nearest(x: T) -> Self
[src]
Convert to the nearest integer
Half-way cases are rounded away from 0
.
fn conv_floor(x: T) -> Self
[src]
Convert the floor to an integer
Returns the largest integer less than or equal to x
.
fn conv_ceil(x: T) -> Self
[src]
Convert the ceiling to an integer
Returns the smallest integer greater than or equal to x
.
fn try_conv_trunc(x: T) -> Result<Self, Error>
[src]
Try converting to integer with truncation
Rounds towards zero (same as as
).
fn try_conv_nearest(x: T) -> Result<Self, Error>
[src]
Try converting to the nearest integer
Half-way cases are rounded away from 0
.
fn try_conv_floor(x: T) -> Result<Self, Error>
[src]
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>
[src]
Try convert the ceiling to an integer
Returns the smallest integer greater than or equal to x
.