Skip to content

Instantly share code, notes, and snippets.

@oxlade39
Created June 10, 2013 20:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oxlade39/5752033 to your computer and use it in GitHub Desktop.
Save oxlade39/5752033 to your computer and use it in GitHub Desktop.
An example of using the Newton Raphson Approximations method to calculating the square root of a number. Taken from https://github.com/oxlade39/scala-betfair/blob/master/src/main/scala/com/github/oxlade39/scalabetfair/math/BigDecimalMath.scala
object BigDecimalMath {
implicit def toBigDecimal(decimal: String): BigDecimal = BigDecimal(decimal)
def sqrt(x: BigDecimal): BigDecimal = {
val maxIterations = x.mc.getPrecision + 1
val guessSteam: Stream[BigDecimal] = newtonRaphsonApproximations(x).take(maxIterations)
val exactMatch: Option[Stream[BigDecimal]] = guessSteam.sliding(2).find(a => a(0) == a(1))
val root: Stream[BigDecimal] = exactMatch.getOrElse(Stream(guessSteam.last))
root(0)
}
/**
* A sequence of BigDecimals the tend towards the square root of toSqrt.
* Using the Newton Raphson Approximations http://en.wikipedia.org/wiki/Newton's_method
* @param toSqrt the value to find the root of
* @param guess the first guess to iterate over (typically toSqrt/2)
* @return
*/
private[this] def newtonRaphsonApproximations(toSqrt: BigDecimal, guess: BigDecimal): Stream[BigDecimal] =
Stream.cons(guess, newtonRaphsonApproximations(toSqrt, ((toSqrt / guess) + guess) / 2))
private[this] def newtonRaphsonApproximations(toSqrt: BigDecimal): Stream[BigDecimal] =
newtonRaphsonApproximations(toSqrt, toSqrt / 2)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment