Way Compute Square Root Positive Real Number Say X Make Wild Guess Square Root Number G Ch Q43888223
Here is a way to compute the square root of a positive realnumber say x. We make a wild guess that the square root is somenumber g. Then we check if g2 is close to x. If it is we return gas our answer; if not we refine our guess using the formula
g′ = (g + x/g)/2.0
where g′ is the new guess. We keep repeating until we are closeenough. Write an OCaml program to implement this idea. Since we areworking with floating point numbers we cannot check for equality;we need to check if the numbers are close enough. I have written afunction called close
1Unless you are extending the factorial to non-integer valuesthrough the Gamma function.
for you as well as a function called square. In OCaml floatingpoint operations need special symbols: you need to put a dot afterthe operation. Thus you need to write, for example, 2.0+.3.0. Allthe basic arithmetic operations have dotted versions: +., ∗., /.and must be used with floating point numbers. You cannot writeexpressions like 2 + .3.5 or 2.0 + 3.0. We will not test yourprogram on negative inputs so we are not requiring you to deal withthe possibility that you may get a negative answer. There are ofcourse built-in functions to compute square roots. We have writtenthe tester program to check if you used this; you will get a zeroif you use a built-in function for square roots.
(* Q2 TODO: Write your own tests for the mysqrt function.
You should NOT test cases for n < 0.
*)
let mysqrt_tests = [
(* Your test cases go here. *)
]
(* Q2 TODO: Implement mysqrt. *)
let mysqrt (x:float) = raise NotImplemented
Expert Answer
Answer to Here is a way to compute the square root of a positive real number say x. We make a wild guess that the square root is s…
OR