Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package week1
- import java.lang.Math._
- object session {
- def sqrt(x: Double) = {
- def sqrtItr(guess: Double, x:Double): Double =
- if (isGoodEnough(guess)) guess
- else (sqrtItr(improve(guess),x))
- def isGoodEnough(guess: Double) =
- abs(guess * guess - x) / x < 0.0001
- def improve(guess: Double) =
- (guess + x / guess) / 2
- sqrtItr(1.0, x)
- } //> sqrt: (x: Double)Double
- sqrt(1e-6) //> res0: Double = 0.0010000001533016628
- sqrt(1e60) //> res1: Double = 1.0000000031080746E30
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement