[Math] Make vectors AlmostEquatable
This commit is contained in:
parent
cd8bd144f6
commit
77b0bf7014
2 changed files with 30 additions and 3 deletions
|
@ -20,11 +20,11 @@ public protocol EquatableWithinEpsilon: Strideable {
|
||||||
static var Epsilon: Self.Stride { get }
|
static var Epsilon: Self.Stride { get }
|
||||||
}
|
}
|
||||||
|
|
||||||
extension Float: EquatableWithinEpsilon {
|
extension Float: AlmostEquatable, EquatableWithinEpsilon {
|
||||||
public static let Epsilon: Float.Stride = 1e-8
|
public static let Epsilon: Float.Stride = 1e-8
|
||||||
}
|
}
|
||||||
|
|
||||||
extension Double: EquatableWithinEpsilon {
|
extension Double: AlmostEquatable, EquatableWithinEpsilon {
|
||||||
public static let Epsilon: Double.Stride = 1e-16
|
public static let Epsilon: Double.Stride = 1e-16
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -9,7 +9,7 @@
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
/** A vector. */
|
/** A vector. */
|
||||||
public protocol Vector {
|
public protocol Vector: Equatable, AlmostEquatable {
|
||||||
init()
|
init()
|
||||||
|
|
||||||
/** Number of elements in the vector. */
|
/** Number of elements in the vector. */
|
||||||
|
@ -247,3 +247,30 @@ infix operator × { associativity left precedence 151 }
|
||||||
public func ×(lhs: Vector3, rhs: Vector3) -> Vector3 {
|
public func ×(lhs: Vector3, rhs: Vector3) -> Vector3 {
|
||||||
return lhs.cross(rhs)
|
return lhs.cross(rhs)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MARK: Equality
|
||||||
|
|
||||||
|
public func ==<T: Vector>(lhs: T, rhs: T) -> Bool {
|
||||||
|
for i in 0..<T.count {
|
||||||
|
if lhs[i] != rhs[i] {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
public func ==~<T: Vector>(lhs: T, rhs: T) -> Bool {
|
||||||
|
for i in 0..<T.count {
|
||||||
|
if lhs[i] !==~ rhs[i] {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
public func ==~<T: Vector>(lhs: T?, rhs: T?) -> Bool {
|
||||||
|
if let lhs = lhs, rhs = rhs {
|
||||||
|
return lhs ==~ rhs
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue