Trait easy_cast::ConvFloat[][src]

pub trait ConvFloat<T>: Sized {
    fn conv_trunc(x: T) -> Self;
fn conv_nearest(x: T) -> Self;
fn conv_floor(x: T) -> Self;
fn conv_ceil(x: T) -> Self;
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>; }

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 the always_assert or the assert_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 and NaN) 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.

Loading content...

Implementations on Foreign Types

impl ConvFloat<f32> for i8[src]

impl ConvFloat<f32> for i16[src]

impl ConvFloat<f32> for i32[src]

impl ConvFloat<f32> for i64[src]

impl ConvFloat<f32> for i128[src]

impl ConvFloat<f32> for isize[src]

impl ConvFloat<f32> for u8[src]

impl ConvFloat<f32> for u16[src]

impl ConvFloat<f32> for u32[src]

impl ConvFloat<f32> for u64[src]

impl ConvFloat<f32> for usize[src]

impl ConvFloat<f64> for i8[src]

impl ConvFloat<f64> for i16[src]

impl ConvFloat<f64> for i32[src]

impl ConvFloat<f64> for i64[src]

impl ConvFloat<f64> for i128[src]

impl ConvFloat<f64> for isize[src]

impl ConvFloat<f64> for u8[src]

impl ConvFloat<f64> for u16[src]

impl ConvFloat<f64> for u32[src]

impl ConvFloat<f64> for u64[src]

impl ConvFloat<f64> for u128[src]

impl ConvFloat<f64> for usize[src]

impl ConvFloat<f32> for u128[src]

Loading content...

Implementors

Loading content...