From 0a2d163bb15f70d96a2efc5925f59d9a881d0163 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Wed, 21 Jan 2015 23:39:23 +1100 Subject: [PATCH] maths: seperate float and double PI --- lerp.cpp | 2 +- maths.hpp | 17 +++++++++++++---- test/maths/maths.cpp | 10 +++++++--- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/lerp.cpp b/lerp.cpp index f930e5b8..e6ccf019 100644 --- a/lerp.cpp +++ b/lerp.cpp @@ -45,7 +45,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)) * 0.5; + double t = (1.0 - cos (weight * PI_d)) * 0.5; return a * (1.0 - t) + b * t; } diff --git a/maths.hpp b/maths.hpp index 2823d2aa..33fb4296 100644 --- a/maths.hpp +++ b/maths.hpp @@ -164,24 +164,33 @@ exactly_zero (T a) //----------------------------------------------------------------------------- -constexpr double PI = 3.141592653589793238462643; +// angles, trig +constexpr double PI_d = 3.141592653589793238462643; +constexpr float PI_f = 3.141592653589793238462643f; //----------------------------------------------------------------------------- constexpr double to_degrees (double radians) { - return radians * 180 / PI; + return radians * 180 / PI_d; } + +constexpr float +to_degrees (float radians) { + return radians * 180 / PI_f; +} + + //----------------------------------------------------------------------------- constexpr float to_radians (float degrees) { - return degrees / 180 * static_cast (PI); + return degrees / 180 * static_cast (PI_f); } constexpr double to_radians (double degrees) { - return degrees / 180 * PI; + return degrees / 180 * PI_d; } diff --git a/test/maths/maths.cpp b/test/maths/maths.cpp index 69eb3c67..eddf45e0 100644 --- a/test/maths/maths.cpp +++ b/test/maths/maths.cpp @@ -10,6 +10,9 @@ using std::numeric_limits; int main (int, char **) { + std::cerr.precision (15); + std::cout.precision (15); + CHECK_HARD (!almost_equal (-2.0, 0.0)); CHECK_HARD (!almost_equal (-2.f, 0.f)); CHECK_HARD ( almost_equal ( 0.0, 0.0)); @@ -39,9 +42,10 @@ main (int, char **) { CHECK_EQ (sign ( numeric_limits::infinity ()), 1); CHECK_EQ (sign (-numeric_limits::infinity ()), -1); - CHECK_EQ (to_degrees (PI), 180); - CHECK_EQ (to_radians (180.f), PI); - CHECK_EQ (to_radians (180.0), PI); + 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 (log2 (8u), 3); CHECK_EQ (log2 (1u), 0);