From 2bd853e8fde52bd8a0287397354b43a78bb9012e Mon Sep 17 00:00:00 2001 From: Ameya Chawla <88154798+ameyachawlaggsipu@users.noreply.github.com> Date: Thu, 4 Nov 2021 14:55:32 +0530 Subject: [PATCH] feat : Implemented Babylonian Method Babylonian method is used to calculate square roots . --- numerical_methods/babylonian_method.cpp | 98 +++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 numerical_methods/babylonian_method.cpp diff --git a/numerical_methods/babylonian_method.cpp b/numerical_methods/babylonian_method.cpp new file mode 100644 index 000000000..d3ac4d05c --- /dev/null +++ b/numerical_methods/babylonian_method.cpp @@ -0,0 +1,98 @@ +/** + * @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 /// for IO operations +#include /// for assert + +/** + * @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; /// 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; +}