diff --git a/geom/ellipse.hpp b/geom/ellipse.hpp index 6e8a8ad4..874cfb63 100644 --- a/geom/ellipse.hpp +++ b/geom/ellipse.hpp @@ -46,7 +46,7 @@ namespace util::geom { { std::uniform_real_distribution dist; - float phi = dist (g) * 2 * PI; + float phi = dist (g) * 2 * pi; float rho = std::sqrt (dist (g)); return util::point<2,T> { diff --git a/maths.cpp b/maths.cpp index cc3493e3..e4f50a9c 100644 --- a/maths.cpp +++ b/maths.cpp @@ -31,11 +31,6 @@ template uint32_t util::log2up (uint32_t); template uint64_t util::log2up (uint64_t); -/////////////////////////////////////////////////////////////////////////////// -template const float util::PI; -template const double util::PI; - - /////////////////////////////////////////////////////////////////////////////// // 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 diff --git a/maths.hpp b/maths.hpp index 340d6851..b23fa55d 100644 --- a/maths.hpp +++ b/maths.hpp @@ -451,13 +451,21 @@ namespace util { /////////////////////////////////////////////////////////////////////////////// // angles, trig + namespace detail { + template + struct pi; + + template <> struct pi { static constexpr float value = 3.141592653589793238462643f; }; + template <> struct pi { static constexpr double value = 3.141592653589793238462643; }; + }; + template - constexpr T PI = T(3.141592653589793238462643); + constexpr auto pi = detail::pi::value; //----------------------------------------------------------------------------- template - constexpr T E = T(2.71828182845904523536028747135266250); + constexpr T E = static_cast (2.71828182845904523536028747135266250); //----------------------------------------------------------------------------- @@ -466,7 +474,7 @@ namespace util { to_degrees (T radians) { static_assert (std::is_floating_point::value, "undefined for integral types"); - return radians * 180 / PI; + return radians * 180 / pi; } @@ -476,7 +484,7 @@ namespace util { to_radians (T degrees) { static_assert (std::is_floating_point::value, "undefined for integral types"); - return degrees / 180 * PI; + return degrees / 180 * pi; } @@ -486,7 +494,7 @@ namespace util { constexpr T sincn (T x) { - return almost_zero (x) ? 1 : std::sin (PI * x) / (PI * x); + return almost_zero (x) ? 1 : std::sin (pi * x) / (pi * x); } @@ -518,7 +526,7 @@ namespace util { using real_t = double; return static_cast ( - std::sqrt (2 * PI * n) * std::pow (n / E, n) + std::sqrt (2 * pi * n) * std::pow (n / E, n) ); } diff --git a/polynomial.cpp b/polynomial.cpp index f6e96e29..6d8ea501 100644 --- a/polynomial.cpp +++ b/polynomial.cpp @@ -118,8 +118,8 @@ namespace util::polynomial { const float t = 2 * std::sqrt (-p); s[0] = t * std::cos (phi); - s[1] = -t * std::cos (phi + PI / 3.f); - s[2] = -t * std::cos (phi - PI / 3.f); + s[1] = -t * std::cos (phi + pi / 3.f); + s[2] = -t * std::cos (phi - pi / 3.f); } else { float u = std::cbrt (std::sqrt (D) + abs (q)); if (q > 0.f) diff --git a/test/maths.cpp b/test/maths.cpp index c2502d8e..84ed3291 100644 --- a/test/maths.cpp +++ b/test/maths.cpp @@ -180,10 +180,10 @@ main (void) tap.expect_eq (util::sign ( numeric_limits::infinity ()), 1., "sign +inf"); tap.expect_eq (util::sign (-numeric_limits::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), 180.0, "to_degrees double"); - tap.expect_eq (util::to_radians (180.f), util::PI, "to_radians float"); - tap.expect_eq (util::to_radians (180.0), util::PI, "to_radians double"); + tap.expect_eq (util::to_degrees (util::pi< float>), 180.f, "to_degrees float"); + tap.expect_eq (util::to_degrees (util::pi), 180.0, "to_degrees double"); + tap.expect_eq (util::to_radians (180.f), util::pi, "to_radians float"); + tap.expect_eq (util::to_radians (180.0), util::pi, "to_radians double"); tap.expect_eq (util::log2 (8u), 3u, "log2 +ve"); tap.expect_eq (util::log2 (1u), 0u, "log2 zero"); diff --git a/test/matrix.cpp b/test/matrix.cpp index dc962a71..80bcd030 100644 --- a/test/matrix.cpp +++ b/test/matrix.cpp @@ -196,7 +196,7 @@ main (void) }; for (auto t: TESTS) { - constexpr auto PI2 = 2 * util::PI; + constexpr auto PI2 = 2 * util::pi; auto matrix = ( util::quaternionf::angle_axis (t.euler[2], { 0, 0, 1 }) * diff --git a/test/vector.cpp b/test/vector.cpp index 2465ff45..8f5fd4bc 100644 --- a/test/vector.cpp +++ b/test/vector.cpp @@ -32,13 +32,13 @@ test_polar (util::TAP::logger &tap) }, { - { 1.f, util::PI / 2.f }, + { 1.f, util::pi / 2.f }, { 0.f, 1.f }, "unit length, rotated" }, { - { 1.f, 2 * util::PI }, + { 1.f, 2 * util::pi }, { 1.f, 0.f }, "full rotation, unit length" } @@ -57,8 +57,8 @@ test_polar (util::TAP::logger &tap) auto in_polar = t.polar; auto to_polar = util::cartesian_to_polar (t.cartesian); - in_polar[1] = std::fmod (in_polar[1], 2 * util::PI); - to_polar[1] = std::fmod (to_polar[1], 2 * util::PI); + in_polar[1] = std::fmod (in_polar[1], 2 * util::pi); + to_polar[1] = std::fmod (to_polar[1], 2 * util::pi); 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 for (auto i: TESTS) { tap.expect_eq (util::to_euler (i.dir), - i.euler * util::PI, + i.euler * util::pi, "to euler, %s", i.name); } @@ -109,7 +109,7 @@ test_euler (util::TAP::logger &tap) void test_spherical (util::TAP::logger &tap) { - constexpr auto q = util::PI / 2.f; + constexpr auto q = util::pi / 2.f; static constexpr struct { util::vector3f spherical; diff --git a/vector.hpp b/vector.hpp index c5307b43..e15730c4 100644 --- a/vector.hpp +++ b/vector.hpp @@ -105,16 +105,16 @@ namespace util { { if (s.x < 0) { s.x = -s.x; - s.y += util::PI; + s.y += util::pi; } if (s.y < 0) { s.y = -s.y; - s.z += util::PI; + s.z += util::pi; } - s.y = std::fmod (s.y, util::PI); - s.z = std::fmod (s.z, util::PI); + s.y = std::fmod (s.y, util::pi); + s.z = std::fmod (s.z, util::pi); return s; }