[Math] Make vectors AlmostEquatable

This commit is contained in:
Eryn Wells 2015-11-12 23:19:52 -08:00
parent cd8bd144f6
commit 77b0bf7014
2 changed files with 30 additions and 3 deletions

View file

@ -20,11 +20,11 @@ public protocol EquatableWithinEpsilon: Strideable {
static var Epsilon: Self.Stride { get }
}
extension Float: EquatableWithinEpsilon {
extension Float: AlmostEquatable, EquatableWithinEpsilon {
public static let Epsilon: Float.Stride = 1e-8
}
extension Double: EquatableWithinEpsilon {
extension Double: AlmostEquatable, EquatableWithinEpsilon {
public static let Epsilon: Double.Stride = 1e-16
}

View file

@ -9,7 +9,7 @@
import Foundation
/** A vector. */
public protocol Vector {
public protocol Vector: Equatable, AlmostEquatable {
init()
/** Number of elements in the vector. */
@ -246,4 +246,31 @@ public func ×=(inout lhs: Vector3, rhs: Vector3) {
infix operator × { associativity left precedence 151 }
public func ×(lhs: Vector3, rhs: Vector3) -> Vector3 {
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
}