3
// Copyright (C) 2001, Chris Laurel <claurel@shatters.net>
5
// This program is free software; you can redistribute it and/or
6
// modify it under the terms of the GNU General Public License
7
// as published by the Free Software Foundation; either version 2
8
// of the License, or (at your option) any later version.
10
// 2008-06-28 - reformatted to comply with Stellarium's coding
19
// Solve a function using the bisection method. Returns a pair
20
// with the solution as the first element and the error as the second.
21
template<class T, class F> std::pair<T, T> solveBisection(F f,
28
for (int i = 0; i < maxIter; i++)
30
x = (lower + upper) * (T) 0.5;
31
if (upper - lower < 2 * err)
41
return std::make_pair(x, (upper - lower) / 2);
45
// Solve using iteration; terminate when error is below err or the maximum
46
// number of iterations is reached.
47
template<class T, class F> std::pair<T, T> solveIteration(F f,
55
for (int i = 0; i < maxIter; i++)
59
if (abs(x2 - x) < err)
60
return std::make_pair(x2, x2 - x);
63
return std::make_pair(x2, x2 - x);
67
// Solve using iteration method and a fixed number of steps.
68
template<class T, class F> std::pair<T, T> solveIteration_fixed(F f,
75
for (int i = 0; i < maxIter; i++)
81
return std::make_pair(x2, x2 - x);