mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-11-30 07:55:31 +08:00
docs: conform binary_exponent.cpp to guidelines (#2456)
This commit is contained in:
@@ -3,17 +3,26 @@
|
|||||||
* @brief Calculate the square root of any positive real number in \f$O(\log
|
* @brief Calculate the square root of any positive real number in \f$O(\log
|
||||||
* N)\f$ time, with precision fixed using [bisection
|
* N)\f$ time, with precision fixed using [bisection
|
||||||
* method](https://en.wikipedia.org/wiki/Bisection_method) of root-finding.
|
* method](https://en.wikipedia.org/wiki/Bisection_method) of root-finding.
|
||||||
*
|
* @author DarkWarrior703
|
||||||
* @see Can be implemented using faster and better algorithms like
|
* @see Can be implemented using faster and better algorithms like
|
||||||
* newton_raphson_method.cpp and false_position.cpp
|
* newton_raphson_method.cpp and false_position.cpp
|
||||||
*/
|
*/
|
||||||
#include <cassert>
|
#include <cassert> /// For assert
|
||||||
#include <iostream>
|
#include <iostream> /// For IO operations
|
||||||
|
#include <cmath> /// For std::abs, std::sqrt
|
||||||
|
|
||||||
/** 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.
|
* @namespace math
|
||||||
|
* @brief Mathematical algorithms
|
||||||
*/
|
*/
|
||||||
double Sqrt(double a) {
|
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) {
|
if (a > 0 && a < 1) {
|
||||||
return 1 / Sqrt(1 / a);
|
return 1 / Sqrt(1 / a);
|
||||||
}
|
}
|
||||||
@@ -28,7 +37,8 @@ double Sqrt(double a) {
|
|||||||
double mid = (l + r) / 2;
|
double mid = (l + r) / 2;
|
||||||
if (mid * mid > a) {
|
if (mid * mid > a) {
|
||||||
r = mid;
|
r = mid;
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
if (a - mid * mid < epsilon) {
|
if (a - mid * mid < epsilon) {
|
||||||
return mid;
|
return mid;
|
||||||
}
|
}
|
||||||
@@ -36,14 +46,28 @@ double Sqrt(double a) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** main function */
|
/**
|
||||||
int main() {
|
* @brief Self-test implementations
|
||||||
double n{};
|
* @returns void
|
||||||
std::cin >> n;
|
*/
|
||||||
assert(n >= 0);
|
static void test() {
|
||||||
// Change this line for a better precision
|
const double eps = 1e-9;
|
||||||
std::cout.precision(12);
|
|
||||||
std::cout << std::fixed << Sqrt(n);
|
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;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user