mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
Update sqrt_double.cpp
This commit is contained in:
parent
2b917ee43a
commit
878dc0bf6c
@ -1,18 +1,29 @@
|
||||
#include <iostream>
|
||||
#include <cassert>
|
||||
|
||||
/* Calculate the square root of any number in O(logn) time, with precision fixed */
|
||||
/* Calculate the square root of any
|
||||
number in O(logn) time,
|
||||
with precision fixed */
|
||||
|
||||
double Sqrt(double x){
|
||||
double Sqrt(double x)
|
||||
{
|
||||
double l = 0, r = x;
|
||||
//Epsilon is the precision. A great precision is between 1e-7 and 1e-12.
|
||||
/* Epsilon is the precision.
|
||||
A great precision is
|
||||
between 1e-7 and 1e-12.
|
||||
double epsilon = 1e-12;
|
||||
while(l <= r){
|
||||
*/
|
||||
while( l <= r )
|
||||
{
|
||||
double mid = (l + r) / 2;
|
||||
if(mid * mid > x){
|
||||
if( mid * mid > x )
|
||||
{
|
||||
r = mid;
|
||||
} else {
|
||||
if(x - mid * mid < epsilon){
|
||||
}
|
||||
else
|
||||
{
|
||||
if( x - mid * mid < epsilon )
|
||||
{
|
||||
return mid;
|
||||
}
|
||||
l = mid;
|
||||
|
Loading…
Reference in New Issue
Block a user