1 // fast approximations different functions
2 // Copyright (C) 2008 Tim Blechmann
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program; see the file COPYING. If not, write to
16 // the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 // Boston, MA 02111-1307, USA.
19 #include <boost/type_traits.hpp>
20 #include <boost/static_assert.hpp>
21 #include <boost/math/constants/constants.hpp>
26 /* fast atan2 approximation
28 * adapted from pseudocode posted in
29 * http://www.dspguru.com/comp.dsp/tricks/alg/fxdatan2.htm
31 * max error of .072 rads, if abs(x) > 1e-8
34 F
faster_atan2(F y
, F x
)
36 BOOST_STATIC_ASSERT(boost::is_floating_point
<F
>::value
);
38 F coeff_1
= boost::math::constants::pi
<F
>()/F(4);
39 F coeff_2
= F(3)*coeff_1
;
40 F abs_y
= std::abs(y
) + F(1e-10); // kludge to prevent 0/0 condition
45 F r
= (x
- abs_y
) / (x
+ abs_y
);
46 angle
= coeff_1
- coeff_1
* r
;
50 F r
= (x
+ abs_y
) / (abs_y
- x
);
51 angle
= coeff_2
- coeff_1
* r
;
54 return(-angle
); // negate if in quad III or IV
59 /* fast atan2 approximation
61 * adapted from pseudocode posted in
62 * http://www.dspguru.com/comp.dsp/tricks/alg/fxdatan2.htm
64 * max error of .011 rads, if abs(x) > 1e-8
67 F
fast_atan2(F y
, F x
)
69 BOOST_STATIC_ASSERT(boost::is_floating_point
<F
>::value
);
71 F coeff_1
= boost::math::constants::pi
<F
>()/F(4);
72 F coeff_2
= F(3)*coeff_1
;
73 F abs_y
= std::abs(y
) + F(1e-10); // kludge to prevent 0/0 condition
78 F r
= (x
- abs_y
) / (x
+ abs_y
);
79 angle
= F(0.1963) * r
*r
*r
- F(0.9817) * r
+ coeff_1
;
83 F r
= (x
+ abs_y
) / (abs_y
- x
);
84 angle
= F(0.1963) * r
*r
*r
- F(0.9817) * r
+ coeff_2
;
87 return(-angle
); // negate if in quad III or IV
92 } /* namespace nova */