[−][src]Trait maths_traits::algebra::group_like::additive::MulZ
An auto-implemented trait for multiplication by integers with associative and negatable types using negation and repeated addition
This is intended as a simple and easy way to compute object multiples in abstract algebraic
algorithms without resorting to explicitly applying addition repeatedly. For this reason, the
trait is automatically implemented for any relevant associative and negatable algebraic structure and
the supplied function is generic over the Integer
type.
assert_eq!(2.5f32.mul_z(5u8), 12.5); assert_eq!(2.5f32.mul_z(5u128), 12.5); assert_eq!(2.5f64.mul_z(5u8), 12.5); assert_eq!(2.5f64.mul_z(5u128), 12.5); assert_eq!(2.5f32.mul_z(-5i8), -12.5); assert_eq!(2.5f32.mul_z(-5i64), -12.5);
Usage Notes
This trait is intended for use with small integers and can make no guarrantee that the mathematical output will actually fit in the valid range for the output type. As such, it is possible for the method to panic, overflow, or return an NaN or error value, so if this is an expected possibility, it is recommended to use TryInto or a different to perform the operation.
It is worth noting that this particular design was chosen over returning a Result or Option since this general behavior is already the default for primitive types despite the relative ease with which it can happen.
Implementation Notes
Note that while multiplication by integers is very simply defined using repeated addition and subtraction, in order to add flexibility in implementation and the possibility for proper optimization, the automatic implmentation of this trait will first try to use other traits as a base before defaulting to the general repeated_doubling_neg algorithm
Specifically, for a given Integer type Z
, the auto-impl will first attempt to use
Mul<Z>
, if implemented. If that fails, it will then try to convert using From<Z>
and multiplying if if it implemented and the struct is a [Ring].
Finally, in the general case, it will use the repeated_doubling_neg function.