Module arithmetic_eval::arith
source · Expand description
Arithmetic
trait and its implementations.
§Traits
An Arithmetic
defines fallible arithmetic operations on primitive values
of an ExecutableModule
, namely, addition, subtraction, multiplication, division,
exponentiation (all binary ops), and negation (a unary op). Any module can be run
with any Arithmetic
on its primitive values, although some modules are reasonably tied
to a particular arithmetic or a class of arithmetics (e.g., arithmetics on finite fields).
OrdArithmetic
extends Arithmetic
with a partial comparison operation
(i.e., an analogue to PartialOrd
). This is motivated by the fact that comparisons
may be switched off during parsing, and some Arithmetic
s do not have well-defined comparisons.
ArithmeticExt
helps converting an Arithmetic
into an OrdArithmetic
.
§Implementations
This module defines the following kinds of arithmetics:
StdArithmetic
takes all implementations from the correspondingops
traits. This means that it’s safe to use provided the ops are infallible. As a counter-example, usingStdArithmetic
with built-in integer types (such asu64
) is usually not a good idea since the corresponding ops have failure modes (e.g., division by zero or integer overflow).WrappingArithmetic
is defined for integer types; it uses wrapping semantics for all ops.CheckedArithmetic
is defined for integer types; it uses checked semantics for all ops.ModularArithmetic
operates on integers modulo the specified number.
All defined Arithmetic
s strive to be as generic as possible.
Structs§
- Marker for
CheckedArithmetic
signalling that negation should be inherited from theCheckedNeg
trait. - Arithmetic on an integer type (e.g.,
i32
) that checks overflow and other failure conditions for all operations. - Wrapper type allowing to extend an
Arithmetic
to anOrdArithmetic
implementation. - Modular arithmetic on integers.
- Marker for
CheckedArithmetic
signalling that negation is only possible for zero. - Arithmetic on a number type that implements all necessary operations natively.
- Marker for
CheckedArithmetic
signalling that negation should be inherited from theNeg
trait. This is appropriate ifNeg
never panics (e.g., for signed big integers). - Arithmetic on an integer type (e.g.,
i32
), in which all operations have wrapping semantics.
Traits§
- Encapsulates arithmetic operations on a certain primitive type (or an enum of primitive types).
- Extension trait for
Arithmetic
allowing to combine the arithmetic with comparisons. - Helper trait for
CheckedArithmetic
describing how number negation should be implemented. - Encapsulates extension of an unsigned integer type into signed and unsigned double-width types. This allows performing certain operations (e.g., multiplication) without a possibility of integer overflow.
- Extends an
Arithmetic
with a comparison operation on values.