Skip to content

Commit da4b944

Browse files
markuswntrstephentyrone
authored andcommitted
Add canonicalized quaternion representation
1 parent 687c446 commit da4b944

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Sources/QuaternionModule/Quaternion.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,28 @@ extension Quaternion {
254254
public var isPure: Bool {
255255
real.isZero
256256
}
257+
258+
/// A "canonical" representation of the value.
259+
///
260+
/// For normal quaternion instances with a RealType conforming to
261+
/// BinaryFloatingPoint (the common case), the result is simply this value
262+
/// unmodified. For zeros, the result has the representation (+0, +0, +0, +0).
263+
/// For infinite values, the result has the representation (+inf, +0, +0, +0).
264+
///
265+
/// If the RealType admits non-canonical representations, the x, y, z and r
266+
/// components are canonicalized in the result.
267+
///
268+
/// This is mainly useful for interoperation with other languages, where
269+
/// you may want to reduce each equivalence class to a single representative
270+
/// before passing across language boundaries, but it may also be useful
271+
/// for some serialization tasks. It's also a useful implementation detail for
272+
/// some primitive operations.
273+
@_transparent
274+
public var canonicalized: Self {
275+
guard !isZero else { return .zero }
276+
guard isFinite else { return .infinity }
277+
return self.multiplied(by: 1)
278+
}
257279
}
258280

259281
// MARK: - Additional Initializers

0 commit comments

Comments
 (0)