[−][src]Trait maths_traits::algebra::group_like::multiplicative::PowZ
An auto-implemented trait for exponentiation by integers with associative and invertable types using inversion and repeated multiplication
This is intended as a simple and easy way to compute object powers in abstract algebraic
algorithms without resorting to explicitly applying multiplication repeatedly. For this reason, the
trait is automatically implemented for any relevant associative and invertable algebraic structure and
the supplied function is generic over the Integer
type.
assert_eq!(2.0f32.pow_z(3u8), 8.0); assert_eq!(2.0f32.pow_z(3u128), 8.0); assert_eq!(2.0f64.pow_z(3u8), 8.0); assert_eq!(2.0f64.pow_z(3u128), 8.0); assert_eq!(2.0f32.pow_z(-3i8), 0.125); assert_eq!(2.0f32.pow_z(-3i64), 0.125);
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 exponentiation by integers is very simply defined using repeated multiplication and inversion, 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_squaring_inv algorithm
Specifically, for a given Natural type N
, the auto-impl will first attempt to use
Pow<N>
, if implemented, then if that fails, it will use the general
repeated_squaring_inv algorithm