Skip to main content

Number

Trait Number 

Source
pub trait Number: Copy + Default {
    // Required methods
    fn parse(literal: &str) -> Option<Self>;
    fn add(self, rhs: Self) -> Result<Self, ErrorKind>;
    fn subtract(self, rhs: Self) -> Result<Self, ErrorKind>;
    fn multiply(self, rhs: Self) -> Result<Self, ErrorKind>;
    fn divide(self, rhs: Self) -> Result<Self, ErrorKind>;
    fn remainder(self, rhs: Self) -> Result<Self, ErrorKind>;
    fn power(self, rhs: Self) -> Result<Self, ErrorKind>;
    fn negate(self) -> Result<Self, ErrorKind>;

    // Provided methods
    fn compare(self, rhs: Self) -> Result<Ordering, ErrorKind> { ... }
    fn is_zero(self) -> Result<bool, ErrorKind> { ... }
    fn from_bool(value: bool) -> Result<Self, ErrorKind> { ... }
    fn factorial(self) -> Result<Self, ErrorKind> { ... }
}
Expand description

A value type expressions are evaluated in.

Implemented for f32 and f64 with the libm feature, and for every primitive integer type. Implement it for your own type, such as a fixed-point number, to evaluate expressions in it.

Each operation returns the ErrorKind to report instead of producing a silent NaN, infinity or wrapped value.

Comparisons (<, ==…), logic operators (&&, ||, !, ? :) and factorials (n!) need Number::compare, Number::is_zero and Number::from_bool. They have default implementations, so a type that only implements arithmetic keeps working: override Number::compare to support comparisons, logic and factorials, as the others are derived from it.

Required Methods§

Source

fn parse(literal: &str) -> Option<Self>

Parses a literal such as 42, 3.5 or 1e3, or returns None if the type cannot represent it.

Source

fn add(self, rhs: Self) -> Result<Self, ErrorKind>

Returns self + rhs.

Source

fn subtract(self, rhs: Self) -> Result<Self, ErrorKind>

Returns self - rhs.

Source

fn multiply(self, rhs: Self) -> Result<Self, ErrorKind>

Returns self * rhs.

Source

fn divide(self, rhs: Self) -> Result<Self, ErrorKind>

Returns self / rhs.

Source

fn remainder(self, rhs: Self) -> Result<Self, ErrorKind>

Returns the remainder of self / rhs, with the sign of self.

Source

fn power(self, rhs: Self) -> Result<Self, ErrorKind>

Returns self raised to the power rhs.

Source

fn negate(self) -> Result<Self, ErrorKind>

Returns -self.

Provided Methods§

Source

fn compare(self, rhs: Self) -> Result<Ordering, ErrorKind>

Returns how self orders against rhs.

Return an error for values that cannot be ordered, such as ErrorKind::Domain for a float NaN. The default returns ErrorKind::Unsupported.

Source

fn is_zero(self) -> Result<bool, ErrorKind>

Returns whether self is zero, which logic operators read as false.

The default compares self with Default::default, which it assumes to be zero.

Source

fn from_bool(value: bool) -> Result<Self, ErrorKind>

Returns 1 for true and 0 for false, the results of comparisons and logic operators.

The default parses the literals 1 and 0, and returns ErrorKind::Unsupported if that fails.

Source

fn factorial(self) -> Result<Self, ErrorKind>

Returns the factorial self!, the product 1 * 2 * ... * self, with 0! = 1.

Return ErrorKind::Domain when self is negative or not an integer, and ErrorKind::Overflow when the result does not fit. The default checks that self is a non-negative integer with Number::compare and a Number::remainder by one, then counts from zero to self with Number::add, multiplying as it goes, so it needs Number::compare and rounds each product as Number::multiply does. It gives up with ErrorKind::Overflow after 10 000 multiplications, so a type with a huge or unbounded range cannot compute for ever. Override it to round only once, as Decimal does.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl Number for f32

Source§

fn parse(literal: &str) -> Option<Self>

Source§

fn add(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn subtract(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn multiply(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn divide(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn remainder(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn power(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn negate(self) -> Result<Self, ErrorKind>

Source§

fn compare(self, rhs: Self) -> Result<Ordering, ErrorKind>

Source§

fn is_zero(self) -> Result<bool, ErrorKind>

Source§

fn from_bool(value: bool) -> Result<Self, ErrorKind>

Source§

impl Number for f64

Source§

fn parse(literal: &str) -> Option<Self>

Source§

fn add(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn subtract(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn multiply(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn divide(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn remainder(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn power(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn negate(self) -> Result<Self, ErrorKind>

Source§

fn compare(self, rhs: Self) -> Result<Ordering, ErrorKind>

Source§

fn is_zero(self) -> Result<bool, ErrorKind>

Source§

fn from_bool(value: bool) -> Result<Self, ErrorKind>

Source§

impl Number for i8

Source§

fn parse(literal: &str) -> Option<Self>

Source§

fn add(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn subtract(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn multiply(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn divide(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn remainder(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn power(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn negate(self) -> Result<Self, ErrorKind>

Source§

fn compare(self, rhs: Self) -> Result<Ordering, ErrorKind>

Source§

fn is_zero(self) -> Result<bool, ErrorKind>

Source§

fn from_bool(value: bool) -> Result<Self, ErrorKind>

Source§

impl Number for i16

Source§

fn parse(literal: &str) -> Option<Self>

Source§

fn add(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn subtract(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn multiply(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn divide(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn remainder(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn power(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn negate(self) -> Result<Self, ErrorKind>

Source§

fn compare(self, rhs: Self) -> Result<Ordering, ErrorKind>

Source§

fn is_zero(self) -> Result<bool, ErrorKind>

Source§

fn from_bool(value: bool) -> Result<Self, ErrorKind>

Source§

impl Number for i32

Source§

fn parse(literal: &str) -> Option<Self>

Source§

fn add(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn subtract(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn multiply(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn divide(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn remainder(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn power(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn negate(self) -> Result<Self, ErrorKind>

Source§

fn compare(self, rhs: Self) -> Result<Ordering, ErrorKind>

Source§

fn is_zero(self) -> Result<bool, ErrorKind>

Source§

fn from_bool(value: bool) -> Result<Self, ErrorKind>

Source§

impl Number for i64

Source§

fn parse(literal: &str) -> Option<Self>

Source§

fn add(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn subtract(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn multiply(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn divide(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn remainder(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn power(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn negate(self) -> Result<Self, ErrorKind>

Source§

fn compare(self, rhs: Self) -> Result<Ordering, ErrorKind>

Source§

fn is_zero(self) -> Result<bool, ErrorKind>

Source§

fn from_bool(value: bool) -> Result<Self, ErrorKind>

Source§

impl Number for i128

Source§

fn parse(literal: &str) -> Option<Self>

Source§

fn add(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn subtract(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn multiply(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn divide(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn remainder(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn power(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn negate(self) -> Result<Self, ErrorKind>

Source§

fn compare(self, rhs: Self) -> Result<Ordering, ErrorKind>

Source§

fn is_zero(self) -> Result<bool, ErrorKind>

Source§

fn from_bool(value: bool) -> Result<Self, ErrorKind>

Source§

impl Number for isize

Source§

fn parse(literal: &str) -> Option<Self>

Source§

fn add(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn subtract(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn multiply(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn divide(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn remainder(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn power(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn negate(self) -> Result<Self, ErrorKind>

Source§

fn compare(self, rhs: Self) -> Result<Ordering, ErrorKind>

Source§

fn is_zero(self) -> Result<bool, ErrorKind>

Source§

fn from_bool(value: bool) -> Result<Self, ErrorKind>

Source§

impl Number for u8

Source§

fn parse(literal: &str) -> Option<Self>

Source§

fn add(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn subtract(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn multiply(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn divide(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn remainder(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn power(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn negate(self) -> Result<Self, ErrorKind>

Source§

fn compare(self, rhs: Self) -> Result<Ordering, ErrorKind>

Source§

fn is_zero(self) -> Result<bool, ErrorKind>

Source§

fn from_bool(value: bool) -> Result<Self, ErrorKind>

Source§

impl Number for u16

Source§

fn parse(literal: &str) -> Option<Self>

Source§

fn add(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn subtract(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn multiply(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn divide(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn remainder(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn power(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn negate(self) -> Result<Self, ErrorKind>

Source§

fn compare(self, rhs: Self) -> Result<Ordering, ErrorKind>

Source§

fn is_zero(self) -> Result<bool, ErrorKind>

Source§

fn from_bool(value: bool) -> Result<Self, ErrorKind>

Source§

impl Number for u32

Source§

fn parse(literal: &str) -> Option<Self>

Source§

fn add(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn subtract(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn multiply(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn divide(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn remainder(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn power(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn negate(self) -> Result<Self, ErrorKind>

Source§

fn compare(self, rhs: Self) -> Result<Ordering, ErrorKind>

Source§

fn is_zero(self) -> Result<bool, ErrorKind>

Source§

fn from_bool(value: bool) -> Result<Self, ErrorKind>

Source§

impl Number for u64

Source§

fn parse(literal: &str) -> Option<Self>

Source§

fn add(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn subtract(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn multiply(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn divide(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn remainder(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn power(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn negate(self) -> Result<Self, ErrorKind>

Source§

fn compare(self, rhs: Self) -> Result<Ordering, ErrorKind>

Source§

fn is_zero(self) -> Result<bool, ErrorKind>

Source§

fn from_bool(value: bool) -> Result<Self, ErrorKind>

Source§

impl Number for u128

Source§

fn parse(literal: &str) -> Option<Self>

Source§

fn add(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn subtract(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn multiply(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn divide(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn remainder(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn power(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn negate(self) -> Result<Self, ErrorKind>

Source§

fn compare(self, rhs: Self) -> Result<Ordering, ErrorKind>

Source§

fn is_zero(self) -> Result<bool, ErrorKind>

Source§

fn from_bool(value: bool) -> Result<Self, ErrorKind>

Source§

impl Number for usize

Source§

fn parse(literal: &str) -> Option<Self>

Source§

fn add(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn subtract(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn multiply(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn divide(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn remainder(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn power(self, rhs: Self) -> Result<Self, ErrorKind>

Source§

fn negate(self) -> Result<Self, ErrorKind>

Source§

fn compare(self, rhs: Self) -> Result<Ordering, ErrorKind>

Source§

fn is_zero(self) -> Result<bool, ErrorKind>

Source§

fn from_bool(value: bool) -> Result<Self, ErrorKind>

Implementors§

Source§

impl Number for Fraction

Source§

impl<const DIGITS: u32, const MAX_EXPONENT: i32> Number for Decimal<DIGITS, MAX_EXPONENT>

Source§

impl<const DIGITS: u32, const MAX_EXPONENT: i32> Number for Exact<DIGITS, MAX_EXPONENT>