Created
July 9, 2018 14:50
-
-
Save gpampara/61964db611025427b2bc9c56525c2ed3 to your computer and use it in GitHub Desktop.
UnitInterval
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Operations are allowed on UnitIntervals that violate the closedness | |
of the structure, but these violations are not realized until the | |
underlying value is determined. | |
*/ | |
final class UnitInterval private (private val underlying: Double) { | |
def - (other: UnitInterval) = | |
new UnitInterval(underlying - other.underlying) | |
def + (other: UnitInterval) = | |
new UnitInterval(underlying + other.underlying) | |
def * (other: UnitInterval): UnitInterval = | |
new UnitInterval(underlying * other.underlying) | |
def / (other: UnitInterval): UnitInterval = | |
new UnitInterval(underlying / other.underlying) | |
def negate: UnitInterval = | |
new UnitInterval(1.0 - underlying) | |
def getOption: Option[Double] = | |
if (underlying <= 1.0 && underlying >= 0.0) Some(underlying) | |
else None | |
} | |
object UnitInterval { | |
def zero = new UnitInterval(0.0) | |
def min(a: UnitInterval, b: UnitInterval) = | |
new UnitInterval(math.min(a.underlying, b.underlying)) | |
def floor(a: UnitInterval) = | |
new UnitInterval(math.floor(a.underlying)) | |
def apply(a: Double): Option[UnitInterval] = | |
if (a > 0.0 && a < 1.0) Some(new UnitInterval(a)) | |
else None | |
def b_flat(y: UnitInterval, A: UnitInterval, B: UnitInterval, C: UnitInterval) = | |
A + | |
min(zero, floor(y - B)) * ((A * (B - y)) / B) - | |
min(zero, floor(C - y)) * ((A.negate * (y - C)) / C.negate) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment