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
)
8 def sqrt(a
: Double
, x
: Double
= 10): Double
= {
9 val xNext
= improve(x
, a
)
10 if (isGoodEnough(x
, xNext
)) xNext
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))