Quote:
Ans: I understood it. It reduces x by a small value i.e. 0.0001 i.e. step until x is less than epsilon. ans is incremented by ans += step. So when it reaches to the value greater than or equal to x, loop terminates.
Wrong.
This program search for the square root of
x
by trying every possible
ans
in sequence starting at
0
and incrementing with a small value
0.0001
.
On every loop, the square of actual
ans
is compared to
x
until the difference is less than
epsilon
and
ans
is less that
x
.
The method is very naive and have many problems.
- it is slow, very slow, waaay slow.
- for
x
greater than
10000
, you start to find values where
ans
never comes within the
epsilon
tolerance.