A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://TheAlgorithms.github.io/C-Plus-Plus/da/d24/sqrt__double_8cpp_source.html below:

TheAlgorithms/C++: math/sqrt_double.cpp Source File

1 10#include <cassert> 11#include <iostream> 12 16double Sqrt

(

double

a) {

17 if

(a > 0 && a < 1) {

18 return

1 /

Sqrt

(1 / a);

19

}

20 double

l = 0, r = a;

21 /* Epsilon is the precision. 22 A great precision is 23 between 1e-7 and 1e-12. 24 double epsilon = 1e-12; 25 */ 26 double

epsilon = 1e-12;

27 while

(l <= r) {

28 double

mid = (l + r) / 2;

29 if

(mid * mid > a) {

30

r = mid;

31

}

else

{

32 if

(a - mid * mid < epsilon) {

33 return

mid;

34

}

35

l = mid;

36

}

37

}

38 return

-1;

39

}

40 42int main

() {

43 double

n{};

44

std::cin >> n;

45

assert(n >= 0);

46 // Change this line for a better precision 47

std::cout.precision(12);

48

std::cout << std::fixed <<

Sqrt

(n);

49

}

Sqrt

double Sqrt(double a)

Definition sqrt_double.cpp:16 main

int main()

Definition sqrt_double.cpp:42

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4