TaskStats: remove unused attribute "Time"
[xcsoar.git] / test / src / test_normalise.cpp
blob2ff3ec311414b8d7d7af5e59ca54f8737e1f97a0
1 /* Copyright_License {
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.
22 #include "Math/FastMath.h"
23 #include "TestUtil.hpp"
25 #include <stdio.h>
26 #include <stdlib.h>
28 static
29 void test_normalise_err(const int x, const int y)
31 int x0=x;
32 int y0=y;
33 i_normalise(x0, y0);
34 int mag0 = iround((fixed)sqrt(x0*x0+y0*y0));
35 int error0 = abs((1<<NORMALISE_BITS)-mag0);
37 int x1=x;
38 int y1=y;
39 i_normalise_fast(x1, y1);
40 int mag1 = ihypot(x1, y1);
41 int error1 = abs((1<<NORMALISE_BITS)-mag1);
43 bool err_ok = error1<= error0+1;
44 if (!err_ok) {
45 printf("# %d %d %d %d %d %d %d %d %d %d\n",
46 x, y, x0, y0, mag0, error0, x1, y1, mag1, error1);
49 char label[80];
50 sprintf(label,"fast integer normalise error x=%d y=%d", x, y);
51 ok(err_ok, label, 0);
54 int slow_norm3(const int k, const int x, const int y, const int z);
56 int slow_norm3(const int k, const int x, const int y, const int z) {
57 const long mag = (long)x*x+(long)y*y+(long)x*x;
58 return (1<<NORMALISE_BITS)*k/isqrt4(mag);
61 #ifdef FIXED_MATH
63 static
64 void
65 test_fixed_err()
67 bool err_ok=true;
68 for (fixed x=fixed(0.0001); x< fixed(100000.0); x*= fixed(1.5)) {
69 fixed y0 = fixed(1)/sqrt(x);
70 fixed y1 = rsqrt(x);
71 fixed err = fabs(y1 / y0 - fixed(1));
72 bool this_ok = err< fixed(0.0001);
73 err_ok &= this_ok;
74 if (!this_ok) {
75 printf("%g %g %g %g\n", (double)x, (double)y0, (double)y1, (double)err);
78 ok(err_ok, "fixed rsqrt error", 0);
81 #endif
83 int main(int argc, char** argv) {
85 #ifdef FIXED_MATH
86 plan_tests(5);
87 #else
88 plan_tests(4);
89 #endif
91 test_normalise_err(100, 50);
92 test_normalise_err(-100, 50);
93 test_normalise_err(100, -50);
94 test_normalise_err(-100, -50);
96 #ifdef FIXED_MATH
97 test_fixed_err();
98 #endif
100 return exit_status();