From b924df9dd4bcf78b44aa3144a2516f06ed729fae Mon Sep 17 00:00:00 2001 From: VildanWW Date: Sat, 28 Nov 2026 08:34:55 +0500 Subject: [PATCH] docs: conform sqrt_double.cpp to guidelines (#2456) (#3291) * docs: conform binary_exponent.cpp to guidelines (#2456) * clang-format and clang-tidy fixes for ea599349 --------- Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- math/sqrt_double.cpp | 47 +++++++++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/math/sqrt_double.cpp b/math/sqrt_double.cpp index c4beec9d8..87f7ff777 100644 --- a/math/sqrt_double.cpp +++ b/math/sqrt_double.cpp @@ -3,15 +3,24 @@ * @brief Calculate the square root of any positive real number in \f$O(\log * N)\f$ time, with precision fixed using [bisection * method](https://en.wikipedia.org/wiki/Bisection_method) of root-finding. - * + * @author DarkWarrior703 * @see Can be implemented using faster and better algorithms like * newton_raphson_method.cpp and false_position.cpp */ -#include -#include +#include /// For assert +#include /// For std::abs, std::sqrt +#include /// For IO operations -/** Bisection method implemented for the function \f$x^2-a=0\f$ +/** + * @namespace math + * @brief Mathematical algorithms + */ +namespace math { +/** + * @brief Bisection method implemented for the function \f$x^2-a=0\f$ * whose roots are \f$\pm\sqrt{a}\f$ and only the positive root is returned. + * @param a number to find the square root of + * @returns positive square root of `a`, or `-1` if `a < 0` */ double Sqrt(double a) { if (a > 0 && a < 1) { @@ -37,13 +46,27 @@ double Sqrt(double a) { } return -1; } +} // namespace math -/** main function */ -int main() { - double n{}; - std::cin >> n; - assert(n >= 0); - // Change this line for a better precision - std::cout.precision(12); - std::cout << std::fixed << Sqrt(n); +/** + * @brief Self-test implementations + * @returns void + */ +static void test() { + const double eps = 1e-9; + + assert(std::abs(std::sqrt(123) - math::Sqrt(123)) < eps); + assert(std::abs(std::sqrt(25) - math::Sqrt(25)) < eps); + assert(std::abs(std::sqrt(0.25) - math::Sqrt(0.25)) < eps); + + std::cout << "All tests have successfully passed!\n"; +} + +/** + * @brief Main function + * @returns 0 on exit + */ +int main() { + test(); + return 0; }