3 XCSoar Glide Computer - http://www.xcsoar.org/
4 Copyright (C) 2000-2013 The XCSoar Project
5 A detailed list of copyright holders can be found in the file "AUTHORS".
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either version 2
10 of the License, or (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include "Math/ZeroFinder.hpp"
24 #include "TestUtil.hpp"
26 class ZeroFinderTest
: public ZeroFinder
31 ZeroFinderTest(fixed x_min
, fixed x_max
, unsigned _func
= 0) :
32 ZeroFinder(x_min
, x_max
, fixed(0.0001)), func(_func
) {}
34 fixed
f(const fixed x
);
38 ZeroFinderTest::f(const fixed x
)
41 return fixed(2) * x
* x
- fixed(3) * x
- fixed(5);
44 return pow(fixed(2), x
) - fixed(3);
54 int main(int argc
, char **argv
)
58 ZeroFinderTest
zf(fixed(-100), fixed(100), 0);
59 ok1(equals(zf
.find_zero(fixed(-150)), fixed(-1)));
60 ok1(equals(zf
.find_zero(fixed(0)), fixed(-1)));
61 // ok1(equals(zf.find_zero(fixed(140)), fixed(2.5))); ???
62 ok1(equals(zf
.find_zero(fixed(140)), fixed(-1)));
64 ok1(equals(zf
.find_min(fixed(-150)), fixed(0.75)));
65 ok1(equals(zf
.find_min(fixed(0)), fixed(0.75)));
66 ok1(equals(zf
.find_min(fixed(150)), fixed(0.75)));
68 ZeroFinderTest
zf2(fixed(0), fixed(100), 0);
69 ok1(equals(zf2
.find_zero(fixed(-150)), fixed(2.5)));
70 ok1(equals(zf2
.find_zero(fixed(0)), fixed(2.5)));
71 ok1(equals(zf2
.find_zero(fixed(140)), fixed(2.5)));
73 ZeroFinderTest
zf3(fixed(0), fixed(10), 1);
74 ok1(equals(zf3
.find_zero(fixed(-150)), fixed(1.584963)));
75 ok1(equals(zf3
.find_zero(fixed(1)), fixed(1.584963)));
76 ok1(equals(zf3
.find_zero(fixed(140)), fixed(1.584963)));
78 ZeroFinderTest
zf4(fixed(0), fixed_pi
+ fixed(1), 2);
79 ok1(equals(zf4
.find_zero(fixed(-150)), fixed_half_pi
));
80 ok1(equals(zf4
.find_zero(fixed(1)), fixed_half_pi
));
81 ok1(equals(zf4
.find_zero(fixed(140)), fixed_half_pi
));
83 ok1(equals(zf4
.find_min(fixed(-150)), fixed_pi
));
84 ok1(equals(zf4
.find_min(fixed(1)), fixed_pi
));
85 ok1(equals(zf4
.find_min(fixed(140)), fixed_pi
));