0

Why does this code work?

double squareRoot(double num) { double low = 1.0; double high = num; for (int i = 0; i < 30; i = i + 1) { double estimate = (high + low) / 2; if (estimate*estimate > num) { double newHigh = estimate; high = newHigh; } else { double newLow = estimate; low = newLow; } } return (high + low) / 2; } int main () { cout << squareRoot(9); //Enter the number that you want to take square root of. }

20th Dec 2016, 7:47 AM
Alp BASKICI
Alp BASKICI - avatar
3 Answers
+ 1
Don't all programmers ask that question : "why does this code work?"
20th Dec 2016, 10:31 AM
Abdelaziz Abubaker
Abdelaziz Abubaker - avatar
0
is it working or not working ! ..?
20th Dec 2016, 7:50 AM
Mock
Mock - avatar
0
It is working but I don't understand why it works. If you plug the number you want to take square root of, it operates sucessfully. So I was wondering if someone could explain the code
21st Dec 2016, 2:24 AM
Alp BASKICI
Alp BASKICI - avatar