maths: templatise to_{radians,degrees}

This commit is contained in:
Danny Robson 2015-02-04 15:44:03 +11:00
parent bd22ec3172
commit 2fcc16646c
2 changed files with 33 additions and 7 deletions

View File

@ -167,20 +167,29 @@ exactly_zero [[gnu::pure]] (T a)
//-----------------------------------------------------------------------------
// angles, trig
template <typename T>
struct constants { };
constexpr double PI_d = 3.141592653589793238462643;
constexpr float PI_f = 3.141592653589793238462643f;
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 <typename T>
constexpr T
to_degrees [[gnu::pure]] (T radians)
{
return radians * 180 / constants<T>::PI;
}
constexpr float
to_degrees [[gnu::pure]] (float radians) {
return radians * 180 / PI_f;
template <typename T>
constexpr T
to_radians [[gnu::pure]] (T degrees)
{
return degrees / 180 * constants<T>::PI;
}

View File

@ -43,3 +43,20 @@ pow (T x, unsigned y)
{
return y == 0 ? 1 : x * pow (x, y - 1);
}
//-----------------------------------------------------------------------------
template <>
struct constants<float>
{
static constexpr float PI = PI_f;
static constexpr float E = E_f;
};
template <>
struct constants<double>
{
static constexpr double PI = PI_d;
static constexpr double E = E_d;
};