I'm trying to figure out how to implement interval multiplication in class that has two arguments as double.
Multiplying Intervals
[x1, x2] [y1, y2] = [min(x1 * y1, x1 * y2, x2 * y1, x2 * y2) , max(x1 * y1, x1 * y2, x2 * y1, x2 * y2)]
Unfortunately that is four arguments. I would need a list and take the min and max, but don't know how to implement.
Here is the class I'm working on where I have addition, subtraction, and division.
class Interval(lower: Double, upper: Double) {
require(lower != 0, "No division by Zero")
require(upper != 0, "No division by Zero")
val x1: Double = lower
val x2: Double = upper
override def toString = "[" + x1 + "," + x2 + "]"
def +(that: Interval): Interval =
new Interval (x1 + that.x1 , x2 + that.x2)
def -(that: Interval): Interval =
new Interval (x1 - that.x2, x2 - that.x1)
def *(that: Interval): Interval = ???
def /(that: Interval): Interval =
new Interval (x1 * (1/that.x1) * x1 * (1/that.x2), x2 * (1/that.x1) * x2 * (1/that.x2))
private def doubleSign(n: Double) =
if (n < 0) -1.0 else 1.0
}
object Interval {
def apply(lower: Double, upper: Double) = new Interval(lower, upper)
def parseInterval(raw: String): Interval = {
new Interval(1.0 , 1.0 ) // Place holder so this file will compile for now.
}
}