Skip to content

Commit 333ef5c

Browse files
authored
Merge branch 'master' into Lazeeez
2 parents 91d669a + b98dcdf commit 333ef5c

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

DIRECTORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@
223223
* [Volume](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/volume.cpp)
224224

225225
## Numerical Methods
226+
* [Babylonian Method](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/numerical_methods/babylonian_method.cpp)
226227
* [Bisection Method](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/numerical_methods/bisection_method.cpp)
227228
* [Brent Method Extrema](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/numerical_methods/brent_method_extrema.cpp)
228229
* [Composite Simpson Rule](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/numerical_methods/composite_simpson_rule.cpp)
+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/**
2+
* @file
3+
* @brief [A babylonian method
4+
* (BM)](https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method)
5+
* is an algorithm that computes the square root.
6+
* @details
7+
* This algorithm has an application in use case scenario where a user wants
8+
* find accurate square roots of large numbers
9+
* @author [Ameya Chawla](https://github.com/ameyachawlaggsipu)
10+
*/
11+
12+
#include <cassert> /// for assert
13+
#include <iostream> /// for IO operations
14+
15+
#include "math.h"
16+
17+
/**
18+
* @namespace numerical_methods
19+
* @brief Numerical algorithms/methods
20+
*/
21+
22+
namespace numerical_methods {
23+
24+
/**
25+
* @brief Babylonian methods is an iterative function which returns
26+
* square root of radicand
27+
* @param radicand is the radicand
28+
* @returns x1 the square root of radicand
29+
*/
30+
31+
double babylonian_method(double radicand) {
32+
int i = 1; /// To find initial root or rough approximation
33+
34+
while (i * i <= radicand) {
35+
i++;
36+
}
37+
38+
i--; /// Real Initial value will be i-1 as loop stops on +1 value
39+
40+
double x0 = i; /// Storing previous value for comparison
41+
double x1 =
42+
(radicand / x0 + x0) / 2; /// Storing calculated value for comparison
43+
double temp = NAN; /// Temp variable to x0 and x1
44+
45+
while (std::max(x0, x1) - std::min(x0, x1) < 0.0001) {
46+
temp = (radicand / x1 + x1) / 2; /// Newly calculated root
47+
x0 = x1;
48+
x1 = temp;
49+
}
50+
51+
return x1; /// Returning final root
52+
}
53+
54+
} // namespace numerical_methods
55+
56+
/**
57+
* @brief Self-test implementations
58+
* @details
59+
* Declaring two test cases and checking for the error
60+
* in predicted and true value is less than 0.0001.
61+
* @returns void
62+
*/
63+
static void test() {
64+
/* descriptions of the following test */
65+
66+
auto testcase1 = 125348; /// Testcase 1
67+
auto testcase2 = 752080; /// Testcase 2
68+
69+
auto real_output1 = 354.045194855; /// Real Output 1
70+
auto real_output2 = 867.225460881; /// Real Output 2
71+
72+
auto test_result1 = numerical_methods::babylonian_method(testcase1);
73+
/// Test result for testcase 1
74+
auto test_result2 = numerical_methods::babylonian_method(testcase2);
75+
/// Test result for testcase 2
76+
77+
assert(std::max(test_result1, real_output1) -
78+
std::min(test_result1, real_output1) <
79+
0.0001);
80+
/// Testing for test Case 1
81+
assert(std::max(test_result2, real_output2) -
82+
std::min(test_result2, real_output2) <
83+
0.0001);
84+
/// Testing for test Case 2
85+
86+
std::cout << "All tests have successfully passed!\n";
87+
}
88+
89+
/**
90+
* @brief Main function
91+
* @param argc commandline argument count (ignored)
92+
* @param argv commandline array of arguments (ignored)
93+
* calls automated test function to test the working of fast fourier transform.
94+
* @returns 0 on exit
95+
*/
96+
97+
int main(int argc, char const *argv[]) {
98+
test(); // run self-test implementations
99+
// with 2 defined test cases
100+
return 0;
101+
}

0 commit comments

Comments
 (0)