Simplify implementation of halfAngle

This commit is contained in:
Markus Winter authored and Stephen Canon committed 2025-08-14 21:50:10 -04:00
1 parent 8a9ad438cb
commit bb31091fdc
1 file changed
+4 -7
+4 -7
View File
@@ -91,17 +91,14 @@ extension Quaternion {
@inlinable
public var argument: RealType {
guard isFinite else { return .nan }
guard imaginary != .zero else {
// A zero quaternion does not encode transformation properties.
// If imaginary is zero, real must be non-zero or nan is returned.
return real.isZero ? .nan : .zero
}
// A zero quaternion does not encode transformation properties.
// If imaginary is zero, real must be non-zero or nan is returned.
guard !isReal else { return isPure ? .nan : .zero }
// If lengthSquared computes without over/underflow, everything is fine
// and the result is correct. If not, we have to do the computation
// carefully and unscale the quaternion first.
let lenSq = imaginary.lengthSquared
guard lenSq.isNormal else { return divided(by: magnitude).argument }
guard lenSq.isNormal else { return divided(by: magnitude).halfAngle }
return .atan2(y: .sqrt(lenSq), x: real)
}