# Enum malachite_base::rounding_modes::RoundingMode

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

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more

Converts a RoundingMode to a String.

##### Examples
use malachite_base::rounding_modes::RoundingMode;

assert_eq!(RoundingMode::Down.to_string(), "Down");
assert_eq!(RoundingMode::Up.to_string(), "Up");
assert_eq!(RoundingMode::Floor.to_string(), "Floor");
assert_eq!(RoundingMode::Ceiling.to_string(), "Ceiling");
assert_eq!(RoundingMode::Nearest.to_string(), "Nearest");
assert_eq!(RoundingMode::Exact.to_string(), "Exact");

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;
use std::str::FromStr;

assert_eq!(RoundingMode::from_str("Down"), Ok(RoundingMode::Down));
assert_eq!(RoundingMode::from_str("Up"), Ok(RoundingMode::Up));
assert_eq!(RoundingMode::from_str("Floor"), Ok(RoundingMode::Floor));
assert_eq!(RoundingMode::from_str("Ceiling"), Ok(RoundingMode::Ceiling));
assert_eq!(RoundingMode::from_str("Nearest"), Ok(RoundingMode::Nearest));
assert_eq!(RoundingMode::from_str("Exact"), Ok(RoundingMode::Exact));
assert_eq!(RoundingMode::from_str("abc"), Err("abc".to_string()));
The associated error which can be returned from parsing.
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more

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

See the documentation for impl_named for more details.

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.

#### Examples

use malachite_base::rounding_modes::RoundingMode;

assert_eq!(-RoundingMode::Down, RoundingMode::Down);
assert_eq!(-RoundingMode::Up, RoundingMode::Up);
assert_eq!(-RoundingMode::Floor, RoundingMode::Ceiling);
assert_eq!(-RoundingMode::Ceiling, RoundingMode::Floor);
assert_eq!(-RoundingMode::Nearest, RoundingMode::Nearest);
assert_eq!(-RoundingMode::Exact, RoundingMode::Exact);
The resulting type after applying the - operator.
Performs the unary - operation. Read more

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.

##### Examples
use malachite_base::num::arithmetic::traits::NegAssign;
use malachite_base::rounding_modes::RoundingMode;

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

let mut rm = RoundingMode::Floor;
rm.neg_assign();
assert_eq!(rm, RoundingMode::Ceiling);
This method returns an Ordering between self and other. Read more
Compares and returns the maximum of two values. Read more
Compares and returns the minimum of two values. Read more
Restrict a value to a certain interval. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

## Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

Should always be Self

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)");
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.