Arithmetic

Trait Arithmetic 

Source
pub trait Arithmetic<T> {
    // Required methods
    fn add(&self, x: T, y: T) -> Result<T, ArithmeticError>;
    fn sub(&self, x: T, y: T) -> Result<T, ArithmeticError>;
    fn mul(&self, x: T, y: T) -> Result<T, ArithmeticError>;
    fn div(&self, x: T, y: T) -> Result<T, ArithmeticError>;
    fn pow(&self, x: T, y: T) -> Result<T, ArithmeticError>;
    fn neg(&self, x: T) -> Result<T, ArithmeticError>;
    fn eq(&self, x: &T, y: &T) -> bool;
}
Expand description

Encapsulates arithmetic operations on a certain primitive type (or an enum of primitive types).

Unlike operations on built-in integer types, arithmetic operations may be fallible. Additionally, the arithmetic can have a state. This is used, for example, in ModularArithmetic, which stores the modulus in the state.

Required Methods§

Source

fn add(&self, x: T, y: T) -> Result<T, ArithmeticError>

Adds two values.

§Errors

Returns an error if the operation is unsuccessful (e.g., on integer overflow).

Source

fn sub(&self, x: T, y: T) -> Result<T, ArithmeticError>

Subtracts two values.

§Errors

Returns an error if the operation is unsuccessful (e.g., on integer underflow).

Source

fn mul(&self, x: T, y: T) -> Result<T, ArithmeticError>

Multiplies two values.

§Errors

Returns an error if the operation is unsuccessful (e.g., on integer overflow).

Source

fn div(&self, x: T, y: T) -> Result<T, ArithmeticError>

Divides two values.

§Errors

Returns an error if the operation is unsuccessful (e.g., if y is zero or does not have a multiplicative inverse in the case of modular arithmetic).

Source

fn pow(&self, x: T, y: T) -> Result<T, ArithmeticError>

Raises x to the power of y.

§Errors

Returns an error if the operation is unsuccessful (e.g., on integer overflow).

Source

fn neg(&self, x: T) -> Result<T, ArithmeticError>

Negates a value.

§Errors

Returns an error if the operation is unsuccessful (e.g., on integer overflow).

Source

fn eq(&self, x: &T, y: &T) -> bool

Checks if two values are equal. Note that equality can be a non-trivial operation; e.g., different numbers may be equal as per modular arithmetic.

Implementations on Foreign Types§

Source§

impl<T> Arithmetic<T> for Box<dyn OrdArithmetic<T>>

Source§

fn add(&self, x: T, y: T) -> Result<T, ArithmeticError>

Source§

fn sub(&self, x: T, y: T) -> Result<T, ArithmeticError>

Source§

fn mul(&self, x: T, y: T) -> Result<T, ArithmeticError>

Source§

fn div(&self, x: T, y: T) -> Result<T, ArithmeticError>

Source§

fn pow(&self, x: T, y: T) -> Result<T, ArithmeticError>

Source§

fn neg(&self, x: T) -> Result<T, ArithmeticError>

Source§

fn eq(&self, x: &T, y: &T) -> bool

Implementors§

Source§

impl Arithmetic<BigUint> for ModularArithmetic<BigUint>

Available on crate feature bigint only.
Source§

impl<T> Arithmetic<T> for ModularArithmetic<T>

Source§

impl<T> Arithmetic<T> for StdArithmetic
where T: Clone + NumOps + PartialEq + Neg<Output = T> + Pow<T, Output = T>,

Source§

impl<T> Arithmetic<T> for WrappingArithmetic
where T: Copy + PartialEq + Zero + One + WrappingAdd + WrappingSub + WrappingMul + WrappingNeg + Div<T, Output = T>, usize: TryFrom<T>,

Source§

impl<T, A> Arithmetic<T> for FullArithmetic<T, A>
where A: Arithmetic<T>,

Source§

impl<T, Kind> Arithmetic<T> for CheckedArithmetic<Kind>