Properly use typed math constants instead of impl. specific hacks

PI is not in the stdlib, neither is 1/pi, pi*2 and so on. Instead
of relying on implementations providing these, use properly typed
math constants.

Main benefits:
- portable and
- returns constexpr, for compile-time computation

References:
- http://www.boost.org/doc/libs/1_60_0/libs/math/doc/html/math_toolkit/constants_intro.html
- http://www.boost.org/doc/libs/1_60_0/libs/math/doc/html/math_toolkit/constants.html
This commit is contained in:
Daniel J. Hofmann
2016-01-28 15:21:02 +01:00
parent 64b36807d3
commit ef171f3acd
6 changed files with 42 additions and 26 deletions
+14 -9
View File
@@ -6,6 +6,8 @@
#include <limits>
#include <boost/math/constants/constants.hpp>
namespace osrm
{
namespace util
@@ -356,19 +358,22 @@ constexpr unsigned short atan_table[4096] = {
0xffe0, 0xffea, 0xfff4, 0xffff};
// max value is pi/4
constexpr double SCALING_FACTOR = 4. / M_PI * 0xFFFF;
const constexpr double SCALING_FACTOR = 4. / boost::math::constants::pi<double>() * 0xFFFF;
inline double atan2_lookup(double y, double x)
{
using namespace boost::math::constants;
if (std::abs(x) < std::numeric_limits<double>::epsilon())
{
if (y >= 0.)
{
return M_PI / 2.;
return half_pi<double>();
}
else
{
return -M_PI / 2.;
return -half_pi<double>();
}
}
@@ -399,25 +404,25 @@ inline double atan2_lookup(double y, double x)
case 0:
break;
case 1:
angle = M_PI - angle;
angle = pi<double>() - angle;
break;
case 2:
angle = -angle;
break;
case 3:
angle = -M_PI + angle;
angle = -pi<double>() + angle;
break;
case 4:
angle = M_PI / 2.0 - angle;
angle = half_pi<double>() - angle;
break;
case 5:
angle = M_PI / 2.0 + angle;
angle = half_pi<double>() + angle;
break;
case 6:
angle = -M_PI / 2.0 + angle;
angle = -half_pi<double>() + angle;
break;
case 7:
angle = -M_PI / 2.0 - angle;
angle = -half_pi<double>() - angle;
break;
}
return angle;
-8
View File
@@ -6,14 +6,6 @@
#include <limits>
#include <cstddef>
// Necessary workaround for Windows as VS doesn't implement C99
#ifdef _MSC_VER
#define WIN32_LEAN_AND_MEAN
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#endif
// OpenStreetMap node ids are higher than 2^32
OSRM_STRONG_TYPEDEF(uint64_t, OSMNodeID)
OSRM_STRONG_TYPEDEF(uint32_t, OSMWayID)