Skip to content

Commit 993d23a

Browse files
markuswntrstephentyrone
authored andcommitted
Add canonicalized transform representation to Quaternion
1 parent da4b944 commit 993d23a

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

Sources/QuaternionModule/Quaternion.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,12 +270,41 @@ extension Quaternion {
270270
/// before passing across language boundaries, but it may also be useful
271271
/// for some serialization tasks. It's also a useful implementation detail for
272272
/// some primitive operations.
273+
///
274+
/// See also:
275+
/// -
276+
/// - `.transformCanonicalized`
273277
@_transparent
274278
public var canonicalized: Self {
275279
guard !isZero else { return .zero }
276280
guard isFinite else { return .infinity }
277281
return self.multiplied(by: 1)
278282
}
283+
284+
/// A "canonical transformation" representation of the value.
285+
///
286+
/// For normal quaternion instances with a RealType conforming to
287+
/// BinaryFloatingPoint (the common case) and a non-negative real component,
288+
/// 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).
291+
/// For zeros, the result has the representation (+0, +0, +0, +0). For
292+
/// infinite values, the result has the representation (+inf, +0, +0, +0).
293+
///
294+
/// If the RealType admits non-canonical representations, the x, y, z and r
295+
/// components are canonicalized in the result.
296+
///
297+
/// See also:
298+
/// -
299+
/// - `.canonicalized`
300+
@_transparent
301+
public var transformCanonicalized: Self {
302+
var canonical = canonicalized
303+
if canonical.real.sign == .plus { return canonical }
304+
// Clear the signbit of real even for -0
305+
canonical.real.negate()
306+
return canonical
307+
}
279308
}
280309

281310
// MARK: - Additional Initializers

0 commit comments

Comments
 (0)