8000 Update sqrt_double.cpp · SelfCodeLearning/C-Plus-Plus@878dc0b · GitHub
[go: up one dir, main page]

8000 Skip to content

Commit 878dc0b

Browse files
author
DarkWarrior703
authored
Update sqrt_double.cpp
1 parent 2b917ee commit 878dc0b

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

math/sqrt_double.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
#include <iostream>
22
#include <cassert>
33

4-
/* Calculate the square root of any number in O(logn) time, with precision fixed */
4+
/* Calculate the square root of any
5+
number in O(logn) time,
6+
with precision fixed */
57

6-
double Sqrt(double x){
8+
double Sqrt(double x)
9+
{
710
double l = 0, r = x;
8-
//Epsilon is the precision. A great precision is between 1e-7 and 1e-12.
11+
/* Epsilon is the precision.
12+
A great precision is
13+
between 1e-7 and 1e-12.
914
double epsilon = 1e-12;
10-
while(l <= r){
15+
*/
16+
while( l <= r )
17+
{
1118
double mid = (l + r) / 2;
12-
if(mid * mid > x){
19+
if( mid * mid > x )
20+
{
1321
r = mid;
14-
} else {
15-
if(x - mid * mid < epsilon){
22+
}
23+
else
24+
{
25+
if( x - mid * mid < epsilon )
26+
{
1627
return mid;
1728
}
1829
l = mid;

0 commit comments

Comments
 (0)
0