Intervals

47 views
Skip to first unread message

Cletus Flynn

unread,
Sep 21, 2016, 8:36:36 PM9/21/16
to scala-user
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. 
  }
}

Vlad Patryshev

unread,
Sep 21, 2016, 9:32:33 PM9/21/16
to Cletus Flynn, scala-user
I do not see what's so special about 0; and definitely I would not recommend comparing real numbers by equality. You can safely assume that the theory of real numbers is not equational.

Also, if you want a placeholder, better define it as ???

Also, how about intervals where the x1 > x2? Is it a "negative interval"? What are "multiplication" and "division" for?

Thanks,
-Vlad

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Cletus Flynn

unread,
Sep 21, 2016, 10:41:08 PM9/21/16
to scala-user, clet...@gmail.com
Hi Vlad,

  There are other supporting files I did not disclose, but sent you in an email.  This is an interval calculator assignment I have been working since last week.(Actually Friday, since my return from DC).  I'm still puzzled on how to implement multiplication. For this week I'll take my goose egg and move on to the next assignment as this one turns into a pumpkin at 23:55 EST. It only gets more difficult. My lack OOP doesn't help, but I will figure it out. 

Thanks for responding...
Cletus

Thanks,
-Vlad

To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages