From af266bb9d9639dd978636bbb30136e9d42c6a4bf Mon Sep 17 00:00:00 2001 From: Markus Winter Date: Mon, 11 Oct 2021 14:28:15 +0200 Subject: [PATCH] Add an early skip when calculating the length in exp --- Sources/QuaternionModule/ElementaryFunctions.swift | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/Sources/QuaternionModule/ElementaryFunctions.swift b/Sources/QuaternionModule/ElementaryFunctions.swift index 6c51189..e9b18c0 100644 --- a/Sources/QuaternionModule/ElementaryFunctions.swift +++ b/Sources/QuaternionModule/ElementaryFunctions.swift @@ -31,15 +31,10 @@ extension Quaternion/*: ElementaryFunctions */ { /// `exp(r) cos(θ)` would not be). public static func exp(_ q: Quaternion) -> Quaternion { guard q.isFinite else { return q } - // Firstly evaluate θ and v/θ where θ = ||v|| (as discussed above) - // There are 2 special cases for ||v|| that we need to take care of: - // The value of ||v|| may be invalid due to an overflow in `.lengthSquared`. - // As the internal `SIMD3.length` helper functions deals with overflow and - // underflow of `.lengthSquared`, we can safely ignore this case here. - // However, we still have to check for ||v|| = 0 before evaluating v/θ - // as it would incorrectly yield a division by zero. - let phase = q.imaginary.length - let unitAxis = !phase.isZero ? (q.imaginary / phase) : .zero + // For real quaternions we can skip phase and axis calculations + // TODO: Replace q.imaginary == .zero with `q.isReal` + let phase = q.imaginary == .zero ? .zero : q.imaginary.length + let unitAxis = q.imaginary == .zero ? .zero : (q.imaginary / phase) // If real < log(greatestFiniteMagnitude), then exp(q.real) does not overflow. // To protect ourselves against sketchy log or exp implementations in // an unknown host library, or slight rounding disagreements between