forked from TheAlgorithms/C-Plus-Plus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat : Implemented Babylonian Method (TheAlgorithms#1837)
* feat : Implemented Babylonian Method Babylonian method is used to calculate square roots . * Update numerical_methods/babylonian_method.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * updating DIRECTORY.md * clang-format and clang-tidy fixes for 9596ac7 Co-authored-by: David Leal <halfpacho@gmail.com> Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
- Loading branch information
1 parent
5bfc774
commit b98dcdf
Showing
2 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/** | ||
* @file | ||
* @brief [A babylonian method | ||
* (BM)](https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method) | ||
* is an algorithm that computes the square root. | ||
* @details | ||
* This algorithm has an application in use case scenario where a user wants | ||
* find accurate square roots of large numbers | ||
* @author [Ameya Chawla](https://github.com/ameyachawlaggsipu) | ||
*/ | ||
|
||
#include <cassert> /// for assert | ||
#include <iostream> /// for IO operations | ||
|
||
#include "math.h" | ||
|
||
/** | ||
* @namespace numerical_methods | ||
* @brief Numerical algorithms/methods | ||
*/ | ||
|
||
namespace numerical_methods { | ||
|
||
/** | ||
* @brief Babylonian methods is an iterative function which returns | ||
* square root of radicand | ||
* @param radicand is the radicand | ||
* @returns x1 the square root of radicand | ||
*/ | ||
|
||
double babylonian_method(double radicand) { | ||
int i = 1; /// To find initial root or rough approximation | ||
|
||
while (i * i <= radicand) { | ||
i++; | ||
} | ||
|
||
i--; /// Real Initial value will be i-1 as loop stops on +1 value | ||
|
||
double x0 = i; /// Storing previous value for comparison | ||
double x1 = | ||
(radicand / x0 + x0) / 2; /// Storing calculated value for comparison | ||
double temp = NAN; /// Temp variable to x0 and x1 | ||
|
||
while (std::max(x0, x1) - std::min(x0, x1) < 0.0001) { | ||
temp = (radicand / x1 + x1) / 2; /// Newly calculated root | ||
x0 = x1; | ||
x1 = temp; | ||
} | ||
|
||
return x1; /// Returning final root | ||
} | ||
|
||
} // namespace numerical_methods | ||
|
||
/** | ||
* @brief Self-test implementations | ||
* @details | ||
* Declaring two test cases and checking for the error | ||
* in predicted and true value is less than 0.0001. | ||
* @returns void | ||
*/ | ||
static void test() { | ||
/* descriptions of the following test */ | ||
|
||
auto testcase1 = 125348; /// Testcase 1 | ||
auto testcase2 = 752080; /// Testcase 2 | ||
|
||
auto real_output1 = 354.045194855; /// Real Output 1 | ||
auto real_output2 = 867.225460881; /// Real Output 2 | ||
|
||
auto test_result1 = numerical_methods::babylonian_method(testcase1); | ||
/// Test result for testcase 1 | ||
auto test_result2 = numerical_methods::babylonian_method(testcase2); | ||
/// Test result for testcase 2 | ||
|
||
assert(std::max(test_result1, real_output1) - | ||
std::min(test_result1, real_output1) < | ||
0.0001); | ||
/// Testing for test Case 1 | ||
assert(std::max(test_result2, real_output2) - | ||
std::min(test_result2, real_output2) < | ||
0.0001); | ||
/// Testing for test Case 2 | ||
|
||
std::cout << "All tests have successfully passed!\n"; | ||
} | ||
|
||
/** | ||
* @brief Main function | ||
* @param argc commandline argument count (ignored) | ||
* @param argv commandline array of arguments (ignored) | ||
* calls automated test function to test the working of fast fourier transform. | ||
* @returns 0 on exit | ||
*/ | ||
|
||
int main(int argc, char const *argv[]) { | ||
test(); // run self-test implementations | ||
// with 2 defined test cases | ||
return 0; | ||
} |