Skip to main content

primitive_float_round_to_integer

Function primitive_float_round_to_integer 

Source
pub fn primitive_float_round_to_integer<T>(x: T, rm: RoundingMode) -> T
where Float: From<T> + PartialOrd<T>, for<'a> T: ExactFrom<&'a Float> + PrimitiveFloat,
Expand description

Rounds a primitive float to an integer using the given rounding mode, using emulated Float arithmetic.

The result is always exactly representable. With Floor, Ceiling, Down, Up, and Nearest this matches the standard library’s floor, ceil, trunc (with Down; Up rounds away from zero, which has no standard equivalent), and round_ties_even; it serves as a reference implementation. NaN, infinities, and zeros are unchanged.

§Worst-case complexity

Constant time and additional memory.

§Panics

Panics if rm is Exact and the input is not an integer.

§Examples

use malachite_base::num::float::NiceFloat;
use malachite_base::rounding_modes::RoundingMode::*;
use malachite_float::float::arithmetic::round_to_integer::primitive_float_round_to_integer;

assert_eq!(
    NiceFloat(primitive_float_round_to_integer(2.5, Floor)),
    NiceFloat(2.0)
);
assert_eq!(
    NiceFloat(primitive_float_round_to_integer(2.5, Nearest)),
    NiceFloat(2.0)
);