I'm using the below code for square root from a cornell course, however the code returns wrong values, The code is
fun squareRoot(x: real): real =
let
val delta = 0.0001
fun goodEnough(guess: real): bool =
if(abs(guess*guess - x) < delta) then true else false
fun improve(guess: real): real =
(guess + x/guess) / 2.0
fun tryGuess(guess: real): real =
if goodEnough(guess) then guess
else tryGuess(improve(guess))
in
tryGuess(1.0)
end
When executing
use "squareroot1.sml";
[opening squareroot1.sml]
val squareRoot = fn : real -> real
val it = () : unit
- squareroot 9.0;
val it = 1.0 : real
-
It always returns 1.0 as the root can you please tell why this is happening?