reciprocal
The (approximate) reciprocal (multiplicative inverse) of this number, if it is representable.
var reciprocal: Self? { get }
If self is zero and the type has no representation for infinity (as in a typical finite field implementation), or if a reciprocal would overflow or underflow such that it cannot be accurately represented, the result is nil.
Note that .zero.reciprocal
, somewhat surprisingly, is not nil for Real
or Complex
types, because these types have an .infinity
value that acts as the reciprocal of .zero
.
If x.reciprocal
is non-nil, you may be able to replace division by x
with multiplication by this value. It is not advantageous to do this for an isolated division unless it is a compile-time constant visible to the compiler, but if you are dividing many values by a single denominator, this will often be a significant performance win.
Note that this will slightly perturb results for some fields with approximate arithmetic, such as real types–using a normal division is generally more accurate–but no catastrophic loss of accuracy will result. For fields with exact arithmetic, or for the Complex types, the results are identical.
A typical use case looks something like this:
func divide<T: AlgebraicField>(data: [T], by divisor: T) -> [T] {
// If divisor is well-scaled, multiply by reciprocal.
if let recip = divisor.reciprocal {
return data.map { $0 * recip }
}
// Fallback on using division.
return data.map { $0 / divisor }
}