competitive-programming-library

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

View the Project on GitHub kmyk/competitive-programming-library

:warning: a binary search on floating point numbers / 二分探索 (浮動小数点数)
(utils/binary_search_float.hpp)

Depends on

Code

#pragma once
#include <cassert>
#include "utils/macros.hpp"

/**
 * @brief a binary search on floating point numbers / 二分探索 (浮動小数点数)
 */
template <int Iteration = 64, typename UnaryPredicate>
double binsearch_float(double l, double r, UnaryPredicate p) {
    assert (l <= r);
    REP (i, Iteration) {
        double m = (l + r) / 2;
        (p(m) ? r : l) = m;
    }
    return r;
}
#line 2 "utils/binary_search_float.hpp"
#include <cassert>
#line 2 "utils/macros.hpp"
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++ (i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++ (i))
#define REP_R(i, n) for (int i = (int)(n) - 1; (i) >= 0; -- (i))
#define REP3R(i, m, n) for (int i = (int)(n) - 1; (i) >= (int)(m); -- (i))
#define ALL(x) std::begin(x), std::end(x)
#line 4 "utils/binary_search_float.hpp"

/**
 * @brief a binary search on floating point numbers / 二分探索 (浮動小数点数)
 */
template <int Iteration = 64, typename UnaryPredicate>
double binsearch_float(double l, double r, UnaryPredicate p) {
    assert (l <= r);
    REP (i, Iteration) {
        double m = (l + r) / 2;
        (p(m) ? r : l) = m;
    }
    return r;
}
Back to top page