maths: use true constexpr values for pi

This commit is contained in:
Danny Robson 2018-03-12 23:06:15 +11:00
parent d1c6df8bf1
commit 5bc2cf12d4
8 changed files with 32 additions and 29 deletions

View File

@ -46,7 +46,7 @@ namespace util::geom {
{ {
std::uniform_real_distribution<T> dist; std::uniform_real_distribution<T> dist;
float phi = dist (g) * 2 * PI<T>; float phi = dist (g) * 2 * pi<T>;
float rho = std::sqrt (dist (g)); float rho = std::sqrt (dist (g));
return util::point<2,T> { return util::point<2,T> {

View File

@ -31,11 +31,6 @@ template uint32_t util::log2up (uint32_t);
template uint64_t util::log2up (uint64_t); template uint64_t util::log2up (uint64_t);
///////////////////////////////////////////////////////////////////////////////
template const float util::PI<float>;
template const double util::PI<double>;
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// Simple instantiations. Some functions aren't used internally to the library // Simple instantiations. Some functions aren't used internally to the library
// so it's easier to instantiate early and check for broken code at library // so it's easier to instantiate early and check for broken code at library

View File

@ -451,13 +451,21 @@ namespace util {
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// angles, trig // angles, trig
namespace detail {
template <typename T>
struct pi;
template <> struct pi<float> { static constexpr float value = 3.141592653589793238462643f; };
template <> struct pi<double> { static constexpr double value = 3.141592653589793238462643; };
};
template <typename T> template <typename T>
constexpr T PI = T(3.141592653589793238462643); constexpr auto pi = detail::pi<T>::value;
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <typename T> template <typename T>
constexpr T E = T(2.71828182845904523536028747135266250); constexpr T E = static_cast<T> (2.71828182845904523536028747135266250);
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -466,7 +474,7 @@ namespace util {
to_degrees (T radians) to_degrees (T radians)
{ {
static_assert (std::is_floating_point<T>::value, "undefined for integral types"); static_assert (std::is_floating_point<T>::value, "undefined for integral types");
return radians * 180 / PI<T>; return radians * 180 / pi<T>;
} }
@ -476,7 +484,7 @@ namespace util {
to_radians (T degrees) to_radians (T degrees)
{ {
static_assert (std::is_floating_point<T>::value, "undefined for integral types"); static_assert (std::is_floating_point<T>::value, "undefined for integral types");
return degrees / 180 * PI<T>; return degrees / 180 * pi<T>;
} }
@ -486,7 +494,7 @@ namespace util {
constexpr T constexpr T
sincn (T x) sincn (T x)
{ {
return almost_zero (x) ? 1 : std::sin (PI<T> * x) / (PI<T> * x); return almost_zero (x) ? 1 : std::sin (pi<T> * x) / (pi<T> * x);
} }
@ -518,7 +526,7 @@ namespace util {
using real_t = double; using real_t = double;
return static_cast<uintmax_t> ( return static_cast<uintmax_t> (
std::sqrt (2 * PI<real_t> * n) * std::pow (n / E<real_t>, n) std::sqrt (2 * pi<real_t> * n) * std::pow (n / E<real_t>, n)
); );
} }

View File

@ -118,8 +118,8 @@ namespace util::polynomial {
const float t = 2 * std::sqrt (-p); const float t = 2 * std::sqrt (-p);
s[0] = t * std::cos (phi); s[0] = t * std::cos (phi);
s[1] = -t * std::cos (phi + PI<float> / 3.f); s[1] = -t * std::cos (phi + pi<float> / 3.f);
s[2] = -t * std::cos (phi - PI<float> / 3.f); s[2] = -t * std::cos (phi - pi<float> / 3.f);
} else { } else {
float u = std::cbrt (std::sqrt (D) + abs (q)); float u = std::cbrt (std::sqrt (D) + abs (q));
if (q > 0.f) if (q > 0.f)

View File

@ -180,10 +180,10 @@ main (void)
tap.expect_eq (util::sign ( numeric_limits<double>::infinity ()), 1., "sign +inf"); tap.expect_eq (util::sign ( numeric_limits<double>::infinity ()), 1., "sign +inf");
tap.expect_eq (util::sign (-numeric_limits<double>::infinity ()), -1., "sign -inf"); tap.expect_eq (util::sign (-numeric_limits<double>::infinity ()), -1., "sign -inf");
tap.expect_eq (util::to_degrees (util::PI< float>), 180.f, "to_degrees float"); tap.expect_eq (util::to_degrees (util::pi< float>), 180.f, "to_degrees float");
tap.expect_eq (util::to_degrees (util::PI<double>), 180.0, "to_degrees double"); tap.expect_eq (util::to_degrees (util::pi<double>), 180.0, "to_degrees double");
tap.expect_eq (util::to_radians (180.f), util::PI<float>, "to_radians float"); tap.expect_eq (util::to_radians (180.f), util::pi<float>, "to_radians float");
tap.expect_eq (util::to_radians (180.0), util::PI<double>, "to_radians double"); tap.expect_eq (util::to_radians (180.0), util::pi<double>, "to_radians double");
tap.expect_eq (util::log2 (8u), 3u, "log2 +ve"); tap.expect_eq (util::log2 (8u), 3u, "log2 +ve");
tap.expect_eq (util::log2 (1u), 0u, "log2 zero"); tap.expect_eq (util::log2 (1u), 0u, "log2 zero");

View File

@ -196,7 +196,7 @@ main (void)
}; };
for (auto t: TESTS) { for (auto t: TESTS) {
constexpr auto PI2 = 2 * util::PI<float>; constexpr auto PI2 = 2 * util::pi<float>;
auto matrix = ( auto matrix = (
util::quaternionf::angle_axis (t.euler[2], { 0, 0, 1 }) * util::quaternionf::angle_axis (t.euler[2], { 0, 0, 1 }) *

View File

@ -32,13 +32,13 @@ test_polar (util::TAP::logger &tap)
}, },
{ {
{ 1.f, util::PI<float> / 2.f }, { 1.f, util::pi<float> / 2.f },
{ 0.f, 1.f }, { 0.f, 1.f },
"unit length, rotated" "unit length, rotated"
}, },
{ {
{ 1.f, 2 * util::PI<float> }, { 1.f, 2 * util::pi<float> },
{ 1.f, 0.f }, { 1.f, 0.f },
"full rotation, unit length" "full rotation, unit length"
} }
@ -57,8 +57,8 @@ test_polar (util::TAP::logger &tap)
auto in_polar = t.polar; auto in_polar = t.polar;
auto to_polar = util::cartesian_to_polar (t.cartesian); auto to_polar = util::cartesian_to_polar (t.cartesian);
in_polar[1] = std::fmod (in_polar[1], 2 * util::PI<float>); in_polar[1] = std::fmod (in_polar[1], 2 * util::pi<float>);
to_polar[1] = std::fmod (to_polar[1], 2 * util::PI<float>); to_polar[1] = std::fmod (to_polar[1], 2 * util::pi<float>);
tap.expect_eq (in_polar, to_polar, "%s", t.desc); tap.expect_eq (in_polar, to_polar, "%s", t.desc);
} }
@ -88,7 +88,7 @@ test_euler (util::TAP::logger &tap)
// check that simple axis rotations look correct // check that simple axis rotations look correct
for (auto i: TESTS) { for (auto i: TESTS) {
tap.expect_eq (util::to_euler (i.dir), tap.expect_eq (util::to_euler (i.dir),
i.euler * util::PI<float>, i.euler * util::pi<float>,
"to euler, %s", i.name); "to euler, %s", i.name);
} }
@ -109,7 +109,7 @@ test_euler (util::TAP::logger &tap)
void void
test_spherical (util::TAP::logger &tap) test_spherical (util::TAP::logger &tap)
{ {
constexpr auto q = util::PI<float> / 2.f; constexpr auto q = util::pi<float> / 2.f;
static constexpr struct { static constexpr struct {
util::vector3f spherical; util::vector3f spherical;

View File

@ -105,16 +105,16 @@ namespace util {
{ {
if (s.x < 0) { if (s.x < 0) {
s.x = -s.x; s.x = -s.x;
s.y += util::PI<T>; s.y += util::pi<T>;
} }
if (s.y < 0) { if (s.y < 0) {
s.y = -s.y; s.y = -s.y;
s.z += util::PI<T>; s.z += util::pi<T>;
} }
s.y = std::fmod (s.y, util::PI<T>); s.y = std::fmod (s.y, util::pi<T>);
s.z = std::fmod (s.z, util::PI<T>); s.z = std::fmod (s.z, util::pi<T>);
return s; return s;
} }