lecture2_binary_search_c.sc 748 Bytes
Newer Older
Jim Newton's avatar
Jim Newton committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
import scala.math._

// find a root of the given function within the given range
def binSearch(left:Double,right:Double,f:Double=>Double,epsilon:Double,maxDepth:Int):Option[Double] = {

  def recur(left:Double,right:Double,depth:Int):Option[Double] = {
    val m = (left + right) / 2
    val fm = f(m)
    if (depth >= maxDepth)
      None
    else if (abs(fm) < epsilon)
      Some(m)
    else if (fm < 0)
      recur(m, right,depth+1)
    else
      recur(left, m,depth+1)
  }
  if (f(left) <=0 && f(right) >=0)
    recur(left,right,0)
  else if (f(left) >=0 && f(right) <=0)
    recur(right,left,0)
  else
    None
}

binSearch(-Pi/10, Pi/20, sin, 0.0001,32)
binSearch(-Pi/10, Pi/20, cos, 0.0001,32)
binSearch(-10.0,10.0,x=>10-x*x*x,0.0001,32)