Skip to content

Commit b16dee7

Browse files
markuswntrstephentyrone
authored andcommitted
Fix invalid canonicalizedTransform for quaternions with negative real
1 parent 0b9603f commit b16dee7

File tree

2 files changed

+15
-17
lines changed

2 files changed

+15
-17
lines changed

Sources/QuaternionModule/Quaternion.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,8 @@ extension Quaternion {
286286
/// For normal quaternion instances with a RealType conforming to
287287
/// BinaryFloatingPoint (the common case) and a non-negative real component,
288288
/// the result is simply this value unmodified. For instances with a negative
289-
/// real component, the result is a quaternion with a positive real component
290-
/// of equal magnitude and an unmodified imaginary compontent (-r, x, y, z).
289+
/// real component, the result is this quaternion negated -(r, x, y, z); so
290+
/// the real component is always positive.
291291
/// For zeros, the result has the representation (+0, +0, +0, +0). For
292292
/// infinite values, the result has the representation (+inf, +0, +0, +0).
293293
///
@@ -299,11 +299,9 @@ extension Quaternion {
299299
/// - `.canonicalized`
300300
@_transparent
301301
public var canonicalizedTransform: Self {
302-
var canonical = canonicalized
302+
let canonical = canonicalized
303303
if canonical.real.sign == .plus { return canonical }
304-
// Clear the signbit of real even for -0
305-
canonical.real.negate()
306-
return canonical
304+
return -canonical
307305
}
308306
}
309307

Tests/QuaternionTests/PropertyTests.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,28 +104,28 @@ final class PropertyTests: XCTestCase {
104104
// that rotations in *R³* of `q` and `-q` will hash to same value.
105105
let pairs: [(lhs: Quaternion<T>, rhs: Quaternion<T>)] = [
106106
(
107-
Quaternion<T>(real: .pi, imaginary: .pi, .pi, .pi),
108-
Quaternion<T>(real: -.pi, imaginary: .pi, .pi, .pi)
107+
Quaternion<T>(real: -.pi, imaginary: .pi, .pi, .pi),
108+
Quaternion<T>(real: .pi, imaginary: -.pi, -.pi, -.pi)
109109
), (
110110
Quaternion<T>(real: .pi, imaginary: -.pi, .pi, .pi),
111-
Quaternion<T>(real: -.pi, imaginary: -.pi, .pi, .pi)
111+
Quaternion<T>(real: -.pi, imaginary: .pi, -.pi, -.pi)
112112
), (
113113
Quaternion<T>(real: .pi, imaginary: .pi, -.pi, .pi),
114-
Quaternion<T>(real: -.pi, imaginary: .pi, -.pi, .pi)
114+
Quaternion<T>(real: -.pi, imaginary: -.pi, .pi, -.pi)
115115
), (
116116
Quaternion<T>(real: .pi, imaginary: .pi, .pi, -.pi),
117-
Quaternion<T>(real: -.pi, imaginary: .pi, .pi, -.pi)
118-
), (
119-
Quaternion<T>(real: .pi, imaginary: -.pi, -.pi, .pi),
120117
Quaternion<T>(real: -.pi, imaginary: -.pi, -.pi, .pi)
121118
), (
122-
Quaternion<T>(real: .pi, imaginary: -.pi, .pi, -.pi),
123-
Quaternion<T>(real: -.pi, imaginary: -.pi, .pi, -.pi)
119+
Quaternion<T>(real: -.pi, imaginary: -.pi, .pi, .pi),
120+
Quaternion<T>(real: .pi, imaginary: .pi, -.pi, -.pi)
121+
), (
122+
Quaternion<T>(real: .pi, imaginary: -.pi, -.pi, .pi),
123+
Quaternion<T>(real: -.pi, imaginary: .pi, .pi, -.pi)
124124
), (
125125
Quaternion<T>(real: .pi, imaginary: .pi, -.pi, -.pi),
126-
Quaternion<T>(real: -.pi, imaginary: .pi, -.pi, -.pi)
126+
Quaternion<T>(real: -.pi, imaginary: -.pi, .pi, .pi)
127127
), (
128-
Quaternion<T>(real: .pi, imaginary: -.pi, -.pi, -.pi),
128+
Quaternion<T>(real: .pi, imaginary: .pi, .pi, .pi),
129129
Quaternion<T>(real: -.pi, imaginary: -.pi, -.pi, -.pi)
130130
)
131131
]

0 commit comments

Comments
 (0)