Enum malachite_base::rounding_modes::RoundingMode

source ·
pub enum RoundingMode {
    Down,
    Up,
    Floor,
    Ceiling,
    Nearest,
    Exact,
}
Expand description

An enum that specifies how a value should be rounded.

A RoundingMode can often be specified when a function conceptually returns a value of one type, but must be rounded to another type. The most common case is a conceptually real-valued function whose result must be rounded to an integer, like div_round.

§Examples

Here are some examples of how floating-point values would be rounded to integer values using the different RoundingModes.

xFloorCeilingDownUpNearestExact
3.0333333
3.234343panic!()
3.834344panic!()
3.534344panic!()
4.545454panic!()
-3.2-4-3-3-4-3panic!()
-3.8-4-3-3-4-4panic!()
-3.5-4-3-3-4-4panic!()
-4.5-5-4-4-5-4panic!()

Sometimes a RoundingMode is used in an unusual context, such as rounding an integer to a floating-point number, in which case further explanation of its behavior is provided at the usage site.

A RoundingMode takes up 1 byte of space.

Variants§

§

Down

Applies the function $x \mapsto \operatorname{sgn}(x) \lfloor |x| \rfloor$. In other words, the value is rounded towards $0$.

§

Up

Applies the function $x \mapsto \operatorname{sgn}(x) \lceil |x| \rceil$. In other words, the value is rounded away from $0$.

§

Floor

Applies the floor function: $x \mapsto \lfloor x \rfloor$. In other words, the value is rounded towards $-\infty$.

§

Ceiling

Applies the ceiling function: $x \mapsto \lceil x \rceil$. In other words, the value is rounded towards $\infty$.

§

Nearest

Applies the function $$ x \mapsto \begin{cases} \lfloor x \rfloor & x - \lfloor x \rfloor < \frac{1}{2} \\ \lceil x \rceil & x - \lfloor x \rfloor > \frac{1}{2} \\ \lfloor x \rfloor & x - \lfloor x \rfloor = \frac{1}{2} \ \text{and} \ \lfloor x \rfloor \ \text{is even} \\ \lceil x \rceil & x - \lfloor x \rfloor = \frac{1}{2} \ \text{and} \ \lfloor x \rfloor \ \text{is odd.} \end{cases} $$ In other words, it rounds to the nearest integer, and when there’s a tie, it rounds to the nearest even integer. This is also called bankers’ rounding and is often used as a default.

§

Exact

Panics if the value is not already rounded.

Trait Implementations§

source§

impl Clone for RoundingMode

source§

fn clone(&self) -> RoundingMode

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for RoundingMode

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Display for RoundingMode

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Converts a RoundingMode to a [String].

§Worst-case complexity

Constant time and additional memory.

§Examples
use malachite_base::rounding_modes::RoundingMode::*;

assert_eq!(Down.to_string(), "Down");
assert_eq!(Up.to_string(), "Up");
assert_eq!(Floor.to_string(), "Floor");
assert_eq!(Ceiling.to_string(), "Ceiling");
assert_eq!(Nearest.to_string(), "Nearest");
assert_eq!(Exact.to_string(), "Exact");
source§

impl FromStr for RoundingMode

source§

fn from_str(src: &str) -> Result<RoundingMode, String>

Converts a string to a RoundingMode.

If the string does not represent a valid RoundingMode, an Err is returned with the unparseable string.

§Worst-case complexity

$T(n) = O(n)$

$M(n) = O(n)$

where $T$ is time, $M$ is additional memory, and $n$ = src.len().

The worst case occurs when the input string is invalid and must be copied into an Err.

§Examples
use malachite_base::rounding_modes::RoundingMode::{self, *};
use std::str::FromStr;

assert_eq!(RoundingMode::from_str("Down"), Ok(Down));
assert_eq!(RoundingMode::from_str("Up"), Ok(Up));
assert_eq!(RoundingMode::from_str("Floor"), Ok(Floor));
assert_eq!(RoundingMode::from_str("Ceiling"), Ok(Ceiling));
assert_eq!(RoundingMode::from_str("Nearest"), Ok(Nearest));
assert_eq!(RoundingMode::from_str("Exact"), Ok(Exact));
assert_eq!(RoundingMode::from_str("abc"), Err("abc".to_string()));
source§

type Err = String

The associated error which can be returned from parsing.
source§

impl Hash for RoundingMode

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl Named for RoundingMode

source§

const NAME: &'static str = "RoundingMode"

The name of this type, as given by the stringify macro.

See the documentation for impl_named for more details.

source§

impl Neg for RoundingMode

Returns the negative of a RoundingMode.

The negative is defined so that if a RoundingMode $m$ is used to round the result of an odd function $f$, then $f(x, -m) = -f(-x, m)$. Floor and Ceiling are swapped, and the other modes are unchanged.

§Worst-case complexity

Constant time and additional memory.

§Examples

use malachite_base::rounding_modes::RoundingMode::*;

assert_eq!(-Down, Down);
assert_eq!(-Up, Up);
assert_eq!(-Floor, Ceiling);
assert_eq!(-Ceiling, Floor);
assert_eq!(-Nearest, Nearest);
assert_eq!(-Exact, Exact);
source§

type Output = RoundingMode

The resulting type after applying the - operator.
source§

fn neg(self) -> RoundingMode

Performs the unary - operation. Read more
source§

impl NegAssign for RoundingMode

source§

fn neg_assign(&mut self)

Replaces a RoundingMode with its negative.

The negative is defined so that if a RoundingMode $m$ is used to round the result of an odd function $f$, then $f(x, -m) = -f(-x, m)$. Floor and Ceiling are swapped, and the other modes are unchanged.

§Worst-case complexity

Constant time and additional memory.

§Examples
use malachite_base::num::arithmetic::traits::NegAssign;
use malachite_base::rounding_modes::RoundingMode::*;

let mut rm = Down;
rm.neg_assign();
assert_eq!(rm, Down);

let mut rm = Floor;
rm.neg_assign();
assert_eq!(rm, Ceiling);
source§

impl Ord for RoundingMode

source§

fn cmp(&self, other: &RoundingMode) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl PartialEq for RoundingMode

source§

fn eq(&self, other: &RoundingMode) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd for RoundingMode

source§

fn partial_cmp(&self, other: &RoundingMode) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Copy for RoundingMode

source§

impl Eq for RoundingMode

source§

impl StructuralPartialEq for RoundingMode

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
source§

impl<T, U> ExactFrom<T> for U
where U: TryFrom<T>,

source§

fn exact_from(value: T) -> U

source§

impl<T, U> ExactInto<U> for T
where U: ExactFrom<T>,

source§

fn exact_into(self) -> U

source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

impl<T, U> OverflowingInto<U> for T
where U: OverflowingFrom<T>,

source§

impl<T, U> RoundingInto<U> for T
where U: RoundingFrom<T>,

source§

impl<T, U> SaturatingInto<U> for T
where U: SaturatingFrom<T>,

source§

impl<T> ToDebugString for T
where T: Debug,

source§

fn to_debug_string(&self) -> String

Returns the String produced by Ts Debug implementation.

§Examples
use malachite_base::strings::ToDebugString;

assert_eq!([1, 2, 3].to_debug_string(), "[1, 2, 3]");
assert_eq!(
    [vec![2, 3], vec![], vec![4]].to_debug_string(),
    "[[2, 3], [], [4]]"
);
assert_eq!(Some(5).to_debug_string(), "Some(5)");
source§

impl<T> ToOwned for T
where T: Clone,

source§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T, U> WrappingInto<U> for T
where U: WrappingFrom<T>,

source§

fn wrapping_into(self) -> U