/* *****************************************************************************
* %{QMAKE_PROJECT_NAME}
* Copyright (c) %YEAR% killerbee
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
* ****************************************************************************/
#ifndef TWO_CANDLES_PROBLEM_H
#define TWO_CANDLES_PROBLEM_H
#include
#include
inline auto rng = kblib::seeded();
template
inline auto uniform_01 = std::uniform_real_distribution{0, 1};
template
struct minmax_result {
T min;
T max;
friend auto operator+(minmax_result lhs, minmax_result rhs)
-> minmax_result {
return {lhs.min + rhs.min, lhs.max + rhs.max};
}
friend auto operator/(minmax_result lhs, T d) -> minmax_result {
return {lhs.min / d, lhs.max / d};
}
};
template
auto get_avg_positions_ab(int iterations_lb2) -> minmax_result {
if (iterations_lb2 == 0) {
return {};
} else if (iterations_lb2 == 1) {
auto a = uniform_01(rng);
auto b = uniform_01(rng);
return {std::min(a, b), std::max(a, b)};
} else {
return (get_avg_positions_ab(iterations_lb2 - 1)
+ get_avg_positions_ab(iterations_lb2 - 1))
/ 2;
}
}
#endif // TWO_CANDLES_PROBLEM_H