From 076144886a58b7ff5aafe1a7c846321702069b32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Thu, 1 Jan 2015 14:18:57 +0100 Subject: [PATCH] Math: added half pi and Euler's number to constants. Constants::piHalf() is not longer to write than doing the division manually and it has significantly smaller mental overhead. Also I chose piHalf() instead of halfPi() to make it more discoverable through autocompletion. Float a = Constants::pi()*0.5f; Float a = Constants::piHalf(); Float b = Constants::pi()/(2*countOfSomething); Float b = Constants::piHalf()/countOfSomething; --- doc/transformations.dox | 2 +- src/Magnum/Math/Constants.h | 16 ++++++++++++++-- src/Magnum/Math/Test/ConstantsTest.cpp | 10 ++++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/doc/transformations.dox b/doc/transformations.dox index 02ebbd29b..1594b646f 100644 --- a/doc/transformations.dox +++ b/doc/transformations.dox @@ -90,7 +90,7 @@ and rotation transformation can be created by calling @ref Matrix3::rotation(), @ref Complex::rotation() or @ref DualComplex::rotation(), for example: @code auto a = Matrix3::rotation(23.0_degf); -auto b = Complex::rotation(Rad(Constants::pi()/2)); +auto b = Complex::rotation(Rad(Constants::piHalf())); auto c = DualComplex::rotation(-1.57_radf); @endcode diff --git a/src/Magnum/Math/Constants.h b/src/Magnum/Math/Constants.h index 17b873340..212da2cd4 100644 --- a/src/Magnum/Math/Constants.h +++ b/src/Magnum/Math/Constants.h @@ -46,18 +46,26 @@ template struct Constants { /** * @brief Pi * - * @see @ref tau(), @ref Deg, @ref Rad + * @see @ref piHalf(), @ref tau(), @ref Deg, @ref Rad */ static constexpr T pi(); + /** + * @brief Half pi + * + * @see @ref pi(), @ref tau(), @ref Deg, @ref Rad + */ + static constexpr T piHalf(); + /** * @brief Tau * * Or two pi. - * @see @ref pi(), @ref Deg, @ref Rad + * @see @ref pi(), @ref piHalf(), @ref Deg, @ref Rad */ static constexpr T tau(); + static constexpr T e(); /**< @brief Euler's number */ static constexpr T sqrt2(); /**< @brief Square root of 2 */ static constexpr T sqrt3(); /**< @brief Square root of 3 */ #endif @@ -69,7 +77,9 @@ template<> struct Constants { Constants() = delete; static constexpr Double pi() { return 3.141592653589793; } + static constexpr Double piHalf() { return 1.570796326794897; } static constexpr Double tau() { return 6.283185307179586; } + static constexpr Double e() { return 2.718281828459045; } static constexpr Double sqrt2() { return 1.414213562373095; } static constexpr Double sqrt3() { return 1.732050807568877; } }; @@ -78,7 +88,9 @@ template<> struct Constants { Constants() = delete; static constexpr Float pi() { return 3.141592654f; } + static constexpr Float piHalf() { return 1.570796327f; } static constexpr Float tau() { return 6.283185307f; } + static constexpr Float e() { return 2.718281828f; } static constexpr Float sqrt2() { return 1.414213562f; } static constexpr Float sqrt3() { return 1.732050808f; } }; diff --git a/src/Magnum/Math/Test/ConstantsTest.cpp b/src/Magnum/Math/Test/ConstantsTest.cpp index 813c99aab..2331e477e 100644 --- a/src/Magnum/Math/Test/ConstantsTest.cpp +++ b/src/Magnum/Math/Test/ConstantsTest.cpp @@ -50,8 +50,13 @@ void ConstantsTest::constantsFloat() { CORRADE_COMPARE(Math::pow<2>(b), 3.0f); constexpr Float c = Constants::pi(); + constexpr Float d = Constants::piHalf(); constexpr Float e = Constants::tau(); + CORRADE_COMPARE(0.5f*c, d); CORRADE_COMPARE(2.0f*c, e); + + constexpr Float f = Constants::e(); + CORRADE_COMPARE(std::log(f), 1.0f); } void ConstantsTest::constantsDouble() { @@ -62,8 +67,13 @@ void ConstantsTest::constantsDouble() { CORRADE_COMPARE(Math::pow<2>(b), 3.0); constexpr Double c = Constants::pi(); + constexpr Double d = Constants::piHalf(); constexpr Double e = Constants::tau(); + CORRADE_COMPARE(0.5*c, d); CORRADE_COMPARE(2.0*c, e); + + constexpr Double f = Constants::e(); + CORRADE_COMPARE(std::log(f), 1.0); #else CORRADE_SKIP("Double precision is not supported when targeting OpenGL ES."); #endif