maths: use templated PI and E constants

This commit is contained in:
Danny Robson 2015-04-29 17:45:39 +10:00
parent d804f0e4b6
commit c052d7df4b
6 changed files with 18 additions and 37 deletions

View File

@ -42,7 +42,7 @@ lerp::linear (double a, double b, double weight) {
double
lerp::cosine (double a, double b, double weight) {
CHECK (weight >= 0.0 && weight <= 1.0);
double t = (1.0 - cos (weight * PI_d)) * 0.5;
double t = (1.0 - cos (weight * PI<double>)) * 0.5;
return a * (1.0 - t) + b * t;
}

View File

@ -171,19 +171,17 @@ exactly_zero [[gnu::pure]] (T a)
// angles, trig
template <typename T>
struct constants { };
constexpr T PI = T(3.141592653589793238462643);
constexpr double PI_d = 3.141592653589793238462643;
constexpr float PI_f = 3.141592653589793238462643f;
template <typename T>
constexpr T E = T(2.71828182845904523536028747135266250);
constexpr float E_f = 2.71828182845904523536028747135266250f;
constexpr double E_d = 2.71828182845904523536028747135266250;
template <typename T>
constexpr T
to_degrees [[gnu::pure]] (T radians)
{
return radians * 180 / constants<T>::PI;
return radians * 180 / PI<T>;
}
@ -191,7 +189,7 @@ template <typename T>
constexpr T
to_radians [[gnu::pure]] (T degrees)
{
return degrees / 180 * constants<T>::PI;
return degrees / 180 * PI<T>;
}
@ -200,7 +198,7 @@ template <typename T>
constexpr T
sincn [[gnu::pure]] (T x)
{
return almost_zero (x) ? 1 : std::sin (constants<T>::PI * x) / (constants<T>::PI * x);
return almost_zero (x) ? 1 : std::sin (PI<T> * x) / (PI<T> * x);
}
@ -224,7 +222,7 @@ factorial [[gnu::pure]] (unsigned i)
constexpr uintmax_t
stirling [[gnu::pure]] (unsigned n)
{
return static_cast<uintmax_t> (std::sqrt (2 * PI_f * n) * std::pow (n / E_f, n));
return static_cast<uintmax_t> (std::sqrt (2 * PI<float> * n) * std::pow (n / E<float>, n));
}

View File

@ -66,20 +66,3 @@ sign (double v)
{
return std::signbit (v) ? -1. : 1.f;
}
//-----------------------------------------------------------------------------
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;
};

View File

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

View File

@ -114,10 +114,10 @@ main (int, char **) {
CHECK_EQ (sign ( numeric_limits<double>::infinity ()), 1);
CHECK_EQ (sign (-numeric_limits<double>::infinity ()), -1);
CHECK_EQ (to_degrees (PI_d), 180.0);
CHECK_EQ (to_degrees (PI_f), 180.f);
CHECK_EQ (to_radians (180.f), PI_f);
CHECK_EQ (to_radians (180.0), PI_d);
CHECK_EQ (to_degrees (PI<double>), 180.0);
CHECK_EQ (to_degrees (PI<float>), 180.f);
CHECK_EQ (to_radians (180.f), PI<float>);
CHECK_EQ (to_radians (180.0), PI<double>);
CHECK_EQ (log2 (8u), 3);
CHECK_EQ (log2 (1u), 0);

View File

@ -28,13 +28,13 @@ test_polar (void)
},
{
{ 1.f, PI_f / 2.f },
{ 1.f, PI<float> / 2.f },
{ 0.f, 1.f },
"unit length, rotated"
},
{
{ 1.f, 2 * PI_f },
{ 1.f, 2 * PI<float> },
{ 1.f, 0.f },
"full rotation, unit length"
}
@ -53,8 +53,8 @@ test_polar (void)
auto in_polar = t.polar;
auto to_polar = util::cartesian_to_polar (t.cartesian);
in_polar[1] = std::fmod (in_polar[1], 2 * PI_f);
to_polar[1] = std::fmod (to_polar[1], 2 * PI_f);
in_polar[1] = std::fmod (in_polar[1], 2 * PI<float>);
to_polar[1] = std::fmod (to_polar[1], 2 * PI<float>);
CHECK_EQ (in_polar, to_polar);
}