From 2fcc16646c513fdc4d542264c61524fbd46db6e7 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Wed, 4 Feb 2015 15:44:03 +1100 Subject: [PATCH] maths: templatise to_{radians,degrees} --- maths.hpp | 23 ++++++++++++++++------- maths.ipp | 17 +++++++++++++++++ 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/maths.hpp b/maths.hpp index b0b511e6..40389da6 100644 --- a/maths.hpp +++ b/maths.hpp @@ -167,20 +167,29 @@ exactly_zero [[gnu::pure]] (T a) //----------------------------------------------------------------------------- // angles, trig + +template +struct constants { }; + constexpr double PI_d = 3.141592653589793238462643; constexpr float PI_f = 3.141592653589793238462643f; -constexpr float E_f = 2.71828182845904523536028747135266250f; +constexpr float E_f = 2.71828182845904523536028747135266250f; +constexpr double E_d = 2.71828182845904523536028747135266250; -constexpr double -to_degrees [[gnu::pure]] (double radians) { - return radians * 180 / PI_d; +template +constexpr T +to_degrees [[gnu::pure]] (T radians) +{ + return radians * 180 / constants::PI; } -constexpr float -to_degrees [[gnu::pure]] (float radians) { - return radians * 180 / PI_f; +template +constexpr T +to_radians [[gnu::pure]] (T degrees) +{ + return degrees / 180 * constants::PI; } diff --git a/maths.ipp b/maths.ipp index 186d81a4..482aaacc 100644 --- a/maths.ipp +++ b/maths.ipp @@ -43,3 +43,20 @@ pow (T x, unsigned y) { return y == 0 ? 1 : x * pow (x, y - 1); } + + +//----------------------------------------------------------------------------- +template <> +struct constants +{ + static constexpr float PI = PI_f; + static constexpr float E = E_f; +}; + + +template <> +struct constants +{ + static constexpr double PI = PI_d; + static constexpr double E = E_d; +};