algorithm-dev

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub today2098/algorithm-dev

:heavy_check_mark: algorithm/Math/Algebra/square_root.hpp

Verified with

Code

#ifndef ALGORITHM_SQUARE_ROOT_HPP
#define ALGORITHM_SQUARE_ROOT_HPP 1

#include <cassert>
#include <cmath>

namespace algorithm {

// 平方根(ニュートン法).
constexpr double sqrt(double x, double eps = 1e-10) {
    assert(x >= 0.0);
    double res = 1.0;
    while(true) {
        double tmp = (x / res + res) / 2;
        if(std::abs(tmp - res) < eps) break;
        res = tmp;
    }
    return res;
}

}  // namespace algorithm

#endif
#line 1 "algorithm/Math/Algebra/square_root.hpp"



#include <cassert>
#include <cmath>

namespace algorithm {

// 平方根(ニュートン法).
constexpr double sqrt(double x, double eps = 1e-10) {
    assert(x >= 0.0);
    double res = 1.0;
    while(true) {
        double tmp = (x / res + res) / 2;
        if(std::abs(tmp - res) < eps) break;
        res = tmp;
    }
    return res;
}

}  // namespace algorithm
Back to top page