pub trait Rounding {
// Required methods
fn kernel_ceil(self) -> Self;
fn kernel_floor(self) -> Self;
fn kernel_fract(self) -> Self;
fn kernel_round(self) -> Self;
fn kernel_round_ties_even(self) -> Self;
fn kernel_trunc(self) -> Self;
}
Expand description
Provides methods for rounding floating-point numbers.
Required Methods§
Sourcefn kernel_ceil(self) -> Self
fn kernel_ceil(self) -> Self
Returns the smallest integer greater than or equal to self
.
Sourcefn kernel_floor(self) -> Self
fn kernel_floor(self) -> Self
Returns the largest integer smaller than or equal to self
.
Sourcefn kernel_fract(self) -> Self
fn kernel_fract(self) -> Self
Returns the fractional part of self
.
Sourcefn kernel_round(self) -> Self
fn kernel_round(self) -> Self
Rounds self
to the nearest integer, rounding half-way cases away from zero.
Sourcefn kernel_round_ties_even(self) -> Self
fn kernel_round_ties_even(self) -> Self
Returns the nearest integer to a number. Rounds half-way cases to the number with an even least significant digit.
This function always returns the precise result.
§Examples
use num_valid::functions::Rounding;
let f = 3.3_f64;
let g = -3.3_f64;
let h = 3.5_f64;
let i = 4.5_f64;
assert_eq!(f.kernel_round_ties_even(), 3.0);
assert_eq!(g.kernel_round_ties_even(), -3.0);
assert_eq!(h.kernel_round_ties_even(), 4.0);
assert_eq!(i.kernel_round_ties_even(), 4.0);
Sourcefn kernel_trunc(self) -> Self
fn kernel_trunc(self) -> Self
Returns the integer part of self
. This means that non-integer numbers are always truncated towards zero.
§Examples
use num_valid::{RealScalar, functions::Rounding};
let f = 3.7_f64;
let g = 3.0_f64;
let h = -3.7_f64;
assert_eq!(f.kernel_trunc(), 3.0);
assert_eq!(g.kernel_trunc(), 3.0);
assert_eq!(h.kernel_trunc(), -3.0);
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementations on Foreign Types§
Source§impl Rounding for f64
impl Rounding for f64
Source§fn kernel_ceil(self) -> Self
fn kernel_ceil(self) -> Self
Returns the smallest integer greater than or equal to self
.
Source§fn kernel_floor(self) -> Self
fn kernel_floor(self) -> Self
Returns the largest integer smaller than or equal to self
.
Source§fn kernel_fract(self) -> Self
fn kernel_fract(self) -> Self
Returns the fractional part of self
.
Source§fn kernel_round(self) -> Self
fn kernel_round(self) -> Self
Rounds self
to the nearest integer, rounding half-way cases away from zero.
Source§fn kernel_round_ties_even(self) -> Self
fn kernel_round_ties_even(self) -> Self
Returns the nearest integer to a number. Rounds half-way cases to the number with an even least significant digit.
This function always returns the precise result.
§Examples
use num_valid::{RealScalar, functions::Rounding};
let f = 3.3_f64;
let g = -3.3_f64;
let h = 3.5_f64;
let i = 4.5_f64;
assert_eq!(f.kernel_round_ties_even(), 3.0);
assert_eq!(g.kernel_round_ties_even(), -3.0);
assert_eq!(h.kernel_round_ties_even(), 4.0);
assert_eq!(i.kernel_round_ties_even(), 4.0);
Source§fn kernel_trunc(self) -> Self
fn kernel_trunc(self) -> Self
Returns the integer part of self
. This means that non-integer numbers are always truncated towards zero.
§Examples
use num_valid::{RealScalar, functions::Rounding};
let f = 3.7_f64;
let g = 3.0_f64;
let h = -3.7_f64;
assert_eq!(f.kernel_trunc(), 3.0);
assert_eq!(g.kernel_trunc(), 3.0);
assert_eq!(h.kernel_trunc(), -3.0);
Source§impl Rounding for Float
impl Rounding for Float
Source§fn kernel_ceil(self) -> Self
fn kernel_ceil(self) -> Self
Returns the smallest integer greater than or equal to self
.
Source§fn kernel_floor(self) -> Self
fn kernel_floor(self) -> Self
Returns the largest integer smaller than or equal to self
.
Source§fn kernel_fract(self) -> Self
fn kernel_fract(self) -> Self
Returns the fractional part of self
.
Source§fn kernel_round(self) -> Self
fn kernel_round(self) -> Self
Rounds self
to the nearest integer, rounding half-way cases away from zero.
Source§fn kernel_round_ties_even(self) -> Self
fn kernel_round_ties_even(self) -> Self
Returns the nearest integer to a number. Rounds half-way cases to the number with an even least significant digit.
This function always returns the precise result.
§Examples
use num_valid::{RealScalar, RealRugStrictFinite, functions::Rounding};
const PRECISION: u32 = 100;
let f = RealRugStrictFinite::<PRECISION>::try_from_f64(3.3).unwrap();
let g = RealRugStrictFinite::<PRECISION>::try_from_f64(-3.3).unwrap();
let h = RealRugStrictFinite::<PRECISION>::try_from_f64(3.5).unwrap();
let i = RealRugStrictFinite::<PRECISION>::try_from_f64(-4.5).unwrap();
assert_eq!(f.kernel_round_ties_even(), RealRugStrictFinite::<PRECISION>::try_from_f64(3.).unwrap());
assert_eq!(g.kernel_round_ties_even(), RealRugStrictFinite::<PRECISION>::try_from_f64(-3.).unwrap());
assert_eq!(h.kernel_round_ties_even(), RealRugStrictFinite::<PRECISION>::try_from_f64(4.).unwrap());
assert_eq!(i.kernel_round_ties_even(), RealRugStrictFinite::<PRECISION>::try_from_f64(-4.).unwrap());
Source§fn kernel_trunc(self) -> Self
fn kernel_trunc(self) -> Self
Returns the integer part of self
. This means that non-integer numbers are always truncated towards zero.
§Examples
use num_valid::{RealScalar, RealRugStrictFinite, functions::Rounding};
const PRECISION: u32 = 100;
let f = RealRugStrictFinite::<PRECISION>::try_from_f64(3.7).unwrap();
let g = RealRugStrictFinite::<PRECISION>::try_from_f64(3.).unwrap();
let h = RealRugStrictFinite::<PRECISION>::try_from_f64(-3.7).unwrap();
assert_eq!(f.kernel_trunc(), RealRugStrictFinite::<PRECISION>::try_from_f64(3.).unwrap());
assert_eq!(g.kernel_trunc(), RealRugStrictFinite::<PRECISION>::try_from_f64(3.).unwrap());
assert_eq!(h.kernel_trunc(), RealRugStrictFinite::<PRECISION>::try_from_f64(-3.).unwrap());