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"
29 void test_normalise_err(const int x
, const int y
)
34 int mag0
= iround((fixed
)sqrt(x0
*x0
+y0
*y0
));
35 int error0
= abs((1<<NORMALISE_BITS
)-mag0
);
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;
45 printf("# %d %d %d %d %d %d %d %d %d %d\n",
46 x
, y
, x0
, y0
, mag0
, error0
, x1
, y1
, mag1
, error1
);
50 sprintf(label
,"fast integer normalise error x=%d y=%d", x
, y
);
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
);
68 for (fixed x
=fixed(0.0001); x
< fixed(100000.0); x
*= fixed(1.5)) {
69 fixed y0
= fixed(1)/sqrt(x
);
71 fixed err
= fabs(y1
/ y0
- fixed(1));
72 bool this_ok
= err
< fixed(0.0001);
75 printf("%g %g %g %g\n", (double)x
, (double)y0
, (double)y1
, (double)err
);
78 ok(err_ok
, "fixed rsqrt error", 0);
83 int main(int argc
, char** argv
) {
91 test_normalise_err(100, 50);
92 test_normalise_err(-100, 50);
93 test_normalise_err(100, -50);
94 test_normalise_err(-100, -50);
100 return exit_status();