diff --git a/d0/dfc/sqrt__double_8cpp__incl.map b/d0/dfc/sqrt__double_8cpp__incl.map index 00d627f43..3bb5edd30 100644 --- a/d0/dfc/sqrt__double_8cpp__incl.map +++ b/d0/dfc/sqrt__double_8cpp__incl.map @@ -1,7 +1,9 @@ - + - - - + + + + + diff --git a/d0/dfc/sqrt__double_8cpp__incl.md5 b/d0/dfc/sqrt__double_8cpp__incl.md5 index a27858191..7b8811b85 100644 --- a/d0/dfc/sqrt__double_8cpp__incl.md5 +++ b/d0/dfc/sqrt__double_8cpp__incl.md5 @@ -1 +1 @@ -2e9781dbd92e22712294cedbf23fa54f \ No newline at end of file +d07d188ceba69a1a30dd6f8f17fad441 \ No newline at end of file diff --git a/d0/dfc/sqrt__double_8cpp__incl.svg b/d0/dfc/sqrt__double_8cpp__incl.svg index d45de32e8..45fe98653 100644 --- a/d0/dfc/sqrt__double_8cpp__incl.svg +++ b/d0/dfc/sqrt__double_8cpp__incl.svg @@ -4,8 +4,8 @@ - + @@ -23,8 +23,8 @@ Node1 - -math/sqrt_double.cpp + +math/sqrt_double.cpp @@ -41,8 +41,8 @@ Node1->Node2 - - + + @@ -50,8 +50,8 @@ Node3 - -iostream + +cmath @@ -59,8 +59,26 @@ Node1->Node3 - - + + + + + + + +Node4 + + +iostream + + + + + +Node1->Node4 + + + diff --git a/d0/dfc/sqrt__double_8cpp__incl_org.svg b/d0/dfc/sqrt__double_8cpp__incl_org.svg index 067dcd59f..a64a493f9 100644 --- a/d0/dfc/sqrt__double_8cpp__incl_org.svg +++ b/d0/dfc/sqrt__double_8cpp__incl_org.svg @@ -4,16 +4,16 @@ - + math/sqrt_double.cpp Node1 - -math/sqrt_double.cpp + +math/sqrt_double.cpp @@ -30,8 +30,8 @@ Node1->Node2 - - + + @@ -39,8 +39,8 @@ Node3 - -iostream + +cmath @@ -48,8 +48,26 @@ Node1->Node3 - - + + + + + + + +Node4 + + +iostream + + + + + +Node1->Node4 + + + diff --git a/da/d24/sqrt__double_8cpp.html b/da/d24/sqrt__double_8cpp.html index 4421a043b..ad25aff0d 100644 --- a/da/d24/sqrt__double_8cpp.html +++ b/da/d24/sqrt__double_8cpp.html @@ -142,23 +142,34 @@ document.addEventListener('DOMContentLoaded',() => { initNavTree('da/d24/sqrt__d N)\) time, with precision fixed using bisection method of root-finding. More...

#include <cassert>
+#include <cmath>
#include <iostream>
Include dependency graph for sqrt_double.cpp:
-
+

Go to the source code of this file.

+ + + +

+Namespaces

namespace  math
 for assert
- + + + + +

Functions

double Sqrt (double a)
double math::Sqrt (double a)
 Bisection method implemented for the function \(x^2-a=0\) whose roots are \(\pm\sqrt{a}\) and only the positive root is returned.
static void test ()
 Self-test implementations.
int main ()
 Main function.

Detailed Description

Calculate the square root of any positive real number in \(O(\log N)\) time, with precision fixed using bisection method of root-finding.

+
Author
DarkWarrior703
See also
Can be implemented using faster and better algorithms like newton_raphson_method.cpp and false_position.cpp

Definition in file sqrt_double.cpp.

@@ -177,63 +188,56 @@ N)\) time, with precision fixed using -

main function

-

Definition at line 42 of file sqrt_double.cpp.

-
42 {
-
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}
-
double Sqrt(double a)
+

Main function.

+
Returns
0 on exit
+ +

Definition at line 69 of file sqrt_double.cpp.

+
69 {
+
70 test();
+
71 return 0;
+
72}
+
void test()
- -

◆ Sqrt()

+ +

◆ test()

+ + + + + +
- + - +
double Sqrt void test (double a))
+
+static
-

Bisection method implemented for the function \(x^2-a=0\) whose roots are \(\pm\sqrt{a}\) and only the positive root is returned.

-

Definition at line 16 of file sqrt_double.cpp.

-
16 {
-
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}
-
double l(double x)
Another test function.
+

Self-test implementations.

+
Returns
void
+ +

Definition at line 55 of file sqrt_double.cpp.

+
55 {
+
56 const double eps = 1e-9;
+
57
+
58 assert(std::abs(std::sqrt(123) - math::Sqrt(123)) < eps);
+
59 assert(std::abs(std::sqrt(25) - math::Sqrt(25)) < eps);
+
60 assert(std::abs(std::sqrt(0.25) - math::Sqrt(0.25)) < eps);
+
61
+
62 std::cout << "All tests have successfully passed!\n";
+
63}
+
double Sqrt(double a)
Bisection method implemented for the function whose roots are and only the positive root is returne...
diff --git a/da/d24/sqrt__double_8cpp.js b/da/d24/sqrt__double_8cpp.js index b11b60048..1b041ffb4 100644 --- a/da/d24/sqrt__double_8cpp.js +++ b/da/d24/sqrt__double_8cpp.js @@ -1,5 +1,6 @@ var sqrt__double_8cpp = [ [ "main", "da/d24/sqrt__double_8cpp.html#ae66f6b31b5ad750f1fe042a706a4e3d4", null ], - [ "Sqrt", "da/d24/sqrt__double_8cpp.html#ae662282ad0740d2063ac404ca3bd74fc", null ] + [ "math::Sqrt", "dd/d47/namespacemath.html#a35d4cc5ee702f942f648c160ba00e831", null ], + [ "test", "da/d24/sqrt__double_8cpp.html#aa8dca7b867074164d5f45b0f3851269d", null ] ]; \ No newline at end of file diff --git a/da/d24/sqrt__double_8cpp_source.html b/da/d24/sqrt__double_8cpp_source.html index c4369655a..de1875b80 100644 --- a/da/d24/sqrt__double_8cpp_source.html +++ b/da/d24/sqrt__double_8cpp_source.html @@ -122,48 +122,61 @@ document.addEventListener('DOMContentLoaded',() => { initNavTree('da/d24/sqrt__d
Go to the documentation of this 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}
+
10#include <cassert>
+
11#include <cmath>
+
12#include <iostream>
+
13
+
18namespace math {
+
+
25double Sqrt(double a) {
+
26 if (a > 0 && a < 1) {
+
27 return 1 / Sqrt(1 / a);
+
28 }
+
29 double l = 0, r = a;
+
30 /* Epsilon is the precision.
+
31 A great precision is
+
32 between 1e-7 and 1e-12.
+
33 double epsilon = 1e-12;
+
34 */
+
35 double epsilon = 1e-12;
+
36 while (l <= r) {
+
37 double mid = (l + r) / 2;
+
38 if (mid * mid > a) {
+
39 r = mid;
+
40 } else {
+
41 if (a - mid * mid < epsilon) {
+
42 return mid;
+
43 }
+
44 l = mid;
+
45 }
+
46 }
+
47 return -1;
+
48}
-
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}
+
49} // namespace math
+
50
+
+
55static void test() {
+
56 const double eps = 1e-9;
+
57
+
58 assert(std::abs(std::sqrt(123) - math::Sqrt(123)) < eps);
+
59 assert(std::abs(std::sqrt(25) - math::Sqrt(25)) < eps);
+
60 assert(std::abs(std::sqrt(0.25) - math::Sqrt(0.25)) < eps);
+
61
+
62 std::cout << "All tests have successfully passed!\n";
+
63}
+
64
+
+
69int main() {
+
70 test();
+
71 return 0;
+
72}
+
+
void test()
int main()
Main function.
-
double Sqrt(double a)
+
for assert
+
double Sqrt(double a)
Bisection method implemented for the function whose roots are and only the positive root is returne...
diff --git a/dd/d47/namespacemath.html b/dd/d47/namespacemath.html index 608afff9b..335dbfaf7 100644 --- a/dd/d47/namespacemath.html +++ b/dd/d47/namespacemath.html @@ -238,6 +238,8 @@ Functions  This function finds whether a number is power of 2 or not.
std::array< std::complex< long double >, 2 > quadraticEquation (long double a, long double b, long double c)  Quadratic equation calculator.
+double Sqrt (double a) + Bisection method implemented for the function \(x^2-a=0\) whose roots are \(\pm\sqrt{a}\) and only the positive root is returned.
uint64_t binomialCoeffSum (uint64_t n) template<typename T> T cube_volume (T length) @@ -263,6 +265,7 @@ Functions

Detailed Description

for assert

+

For IO operations.

std::cout

for IO implementations

Evaluate recurrence relation using matrix exponentiation.

@@ -320,6 +323,8 @@ Algorithm

Mathematical algorithms

for assert for IO operations

Mathematical algorithms

+

For assert For std::abs, std::sqrt

+

Mathematical algorithms

for assert for std::pow for std::uint32_t

Mathematical algorithms

Typedef Documentation

@@ -1941,6 +1946,60 @@ template<typename T> + +

◆ Sqrt()

+ +
+
+ + + + + + + +
double math::Sqrt (double a)
+
+ +

Bisection method implemented for the function \(x^2-a=0\) whose roots are \(\pm\sqrt{a}\) and only the positive root is returned.

+
Parameters
+ + +
anumber to find the square root of
+
+
+
Returns
positive square root of a, or -1 if a < 0
+ +

Definition at line 25 of file sqrt_double.cpp.

+
25 {
+
26 if (a > 0 && a < 1) {
+
27 return 1 / Sqrt(1 / a);
+
28 }
+
29 double l = 0, r = a;
+
30 /* Epsilon is the precision.
+
31 A great precision is
+
32 between 1e-7 and 1e-12.
+
33 double epsilon = 1e-12;
+
34 */
+
35 double epsilon = 1e-12;
+
36 while (l <= r) {
+
37 double mid = (l + r) / 2;
+
38 if (mid * mid > a) {
+
39 r = mid;
+
40 } else {
+
41 if (a - mid * mid < epsilon) {
+
42 return mid;
+
43 }
+
44 l = mid;
+
45 }
+
46 }
+
47 return -1;
+
48}
+
double l(double x)
Another test function.
+
double Sqrt(double a)
Bisection method implemented for the function whose roots are and only the positive root is returne...
+
+
+

◆ square_area()

diff --git a/dd/d47/namespacemath.js b/dd/d47/namespacemath.js index 8569c9f7d..33745efc9 100644 --- a/dd/d47/namespacemath.js +++ b/dd/d47/namespacemath.js @@ -39,6 +39,7 @@ var namespacemath = [ "sieve", "dd/d47/namespacemath.html#a91366864111e1fac29722ca45e02ea8f", null ], [ "sphere_surface_area", "dd/d47/namespacemath.html#ab7f29862d30df351c317eedd60a0c656", null ], [ "sphere_volume", "dd/d47/namespacemath.html#a34d66a77c19ce9b8b3a3d14352b34551", null ], + [ "Sqrt", "dd/d47/namespacemath.html#a35d4cc5ee702f942f648c160ba00e831", null ], [ "square_area", "dd/d47/namespacemath.html#a971ce57e368f2f631cf1f4ff3f864049", null ], [ "square_perimeter", "dd/d47/namespacemath.html#a9236348755183644f1225e162d01ab14", null ], [ "sum_of_divisor", "dd/d47/namespacemath.html#af05567415a9ea36c254b54e3d5a2152a", null ], diff --git a/doxygen_crawl.html b/doxygen_crawl.html index 879818181..bc051e966 100644 --- a/doxygen_crawl.html +++ b/doxygen_crawl.html @@ -2126,7 +2126,7 @@ - + @@ -2831,6 +2831,7 @@ + diff --git a/globals_func_s.html b/globals_func_s.html index 50489f5b5..fd6444d11 100644 --- a/globals_func_s.html +++ b/globals_func_s.html @@ -157,7 +157,6 @@ document.addEventListener('DOMContentLoaded',() => { initNavTree('globals_func_s
  • SLEEP() : memory_game.cpp
  • sortcol() : fcfs_scheduling.cpp, non_preemptive_sjf_scheduling.cpp
  • spiralPrint() : spiral_print.cpp
  • -
  • Sqrt() : sqrt_double.cpp
  • Standard_InvSqrt() : inv_sqrt.cpp
  • stoogeSort() : stooge_sort.cpp
  • struzik_search() : exponential_search.cpp
  • diff --git a/globals_func_t.html b/globals_func_t.html index 51e5883f9..af7af800e 100644 --- a/globals_func_t.html +++ b/globals_func_t.html @@ -140,7 +140,7 @@ document.addEventListener('DOMContentLoaded',() => { initNavTree('globals_func_t

    - t -