Skip to content

Instantly share code, notes, and snippets.

@dimitribouniol
Created February 21, 2017 01:42
Show Gist options
  • Save dimitribouniol/a3236ac4060feff25728ffcddb3c148e to your computer and use it in GitHub Desktop.
Save dimitribouniol/a3236ac4060feff25728ffcddb3c148e to your computer and use it in GitHub Desktop.
//: Playground - noun: a place where people can play
import UIKit
struct FloatRange {
var minimum: Double;
var maximum: Double;
}
var floatRange = FloatRange(minimum: 0.9999, maximum: 1.0001);
func == (lhs: Double, rhs: FloatRange) -> Bool {
return (lhs >= rhs.minimum && lhs <= rhs.maximum);
}
func == (rhs: FloatRange, lhs: Double) -> Bool {
return lhs == rhs;
}
func == (lhs: Int, rhs: FloatRange) -> Bool {
return Double(lhs) == rhs;
}
func == (rhs: FloatRange, lhs: Int) -> Bool {
return lhs == rhs;
}
func == (lhs: Float, rhs: FloatRange) -> Bool {
return Double(lhs) == rhs;
}
func == (rhs: FloatRange, lhs: Float) -> Bool {
return lhs == rhs;
}
var a: Float = 1.000;
var b = 1.001
var c = 1
a == floatRange
b == floatRange
c == floatRange
infix operator +-: RangeFormationPrecedence
func +- (lhs: Double, rhs: Double) -> FloatRange {
return FloatRange(minimum: lhs - rhs, maximum: lhs + rhs);
}
func +- (lhs: Int, rhs: Double) -> FloatRange {
return Double(lhs) +- rhs;
}
func +- (lhs: Int, rhs: Float) -> FloatRange {
return Double(lhs) +- Double(rhs);
}
func +- (lhs: Float, rhs: Double) -> FloatRange {
return Double(lhs) +- rhs;
}
func +- (lhs: Float, rhs: Float) -> FloatRange {
return Double(lhs) +- Double(rhs);
}
func +- (lhs: Double, rhs: Float) -> FloatRange {
return lhs +- Double(rhs);
}
infix operator ±: RangeFormationPrecedence
func ± (lhs: Double, rhs: Double) -> FloatRange {
return lhs ± rhs;
}
func ± (lhs: Int, rhs: Double) -> FloatRange {
return lhs ± rhs;
}
func ± (lhs: Int, rhs: Float) -> FloatRange {
return lhs ± rhs;
}
func ± (lhs: Float, rhs: Double) -> FloatRange {
return lhs ± rhs;
}
func ± (lhs: Float, rhs: Float) -> FloatRange {
return lhs ± rhs;
}
func ± (lhs: Double, rhs: Float) -> FloatRange {
return lhs ± rhs;
}
b == 1 +- 0.01 // true
b == 1 +- 0.001 // true
b == 1 +- 0.0001 // false
b == 1 +- 0.00001 // false
b == 1 +- 0 + 0.001 // true
b == 1 +- 0 + 0.0001 // false
b == 1 +- 1*0.001 // true
b == 1 +- 1*0.0001 // false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment