Cleaning.
[Scala.git] / Assignment_01 / src / main / scala / Main.scala
1 package org.gburri
2
3 object Main {
4 val epsilon = 0.0001
5 def isGoodEnough(x1: Double, x2: Double) = Math.abs(x1 - x2) < epsilon
6 def improve(x: Double, a: Double) = x - (x * x - a) / (2 * x)
7
8 def sqrt(a: Double, x: Double = 10): Double = {
9 val xNext = improve(x, a)
10 if (isGoodEnough(x, xNext)) xNext
11 else sqrt(a, xNext)
12 }
13
14 def main(args: Array[String]): Unit = {
15 println("Assignment 1 / Question 2 :")
16 printf(" sqrt(9) : %f\n", sqrt(9))
17 printf(" sqrt(612) : %f\n", sqrt(612))
18 }
19 }