Browse Source

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;
pull/87/head
Vladimír Vondruš 12 years ago
parent
commit
076144886a
  1. 2
      doc/transformations.dox
  2. 16
      src/Magnum/Math/Constants.h
  3. 10
      src/Magnum/Math/Test/ConstantsTest.cpp

2
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: @ref Complex::rotation() or @ref DualComplex::rotation(), for example:
@code @code
auto a = Matrix3::rotation(23.0_degf); 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); auto c = DualComplex::rotation(-1.57_radf);
@endcode @endcode

16
src/Magnum/Math/Constants.h

@ -46,18 +46,26 @@ template<class T> struct Constants {
/** /**
* @brief Pi * @brief Pi
* *
* @see @ref tau(), @ref Deg, @ref Rad * @see @ref piHalf(), @ref tau(), @ref Deg, @ref Rad
*/ */
static constexpr T pi(); static constexpr T pi();
/**
* @brief Half pi
*
* @see @ref pi(), @ref tau(), @ref Deg, @ref Rad
*/
static constexpr T piHalf();
/** /**
* @brief Tau * @brief Tau
* *
* Or two pi. * 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 tau();
static constexpr T e(); /**< @brief Euler's number */
static constexpr T sqrt2(); /**< @brief Square root of 2 */ static constexpr T sqrt2(); /**< @brief Square root of 2 */
static constexpr T sqrt3(); /**< @brief Square root of 3 */ static constexpr T sqrt3(); /**< @brief Square root of 3 */
#endif #endif
@ -69,7 +77,9 @@ template<> struct Constants<Double> {
Constants() = delete; Constants() = delete;
static constexpr Double pi() { return 3.141592653589793; } static constexpr Double pi() { return 3.141592653589793; }
static constexpr Double piHalf() { return 1.570796326794897; }
static constexpr Double tau() { return 6.283185307179586; } static constexpr Double tau() { return 6.283185307179586; }
static constexpr Double e() { return 2.718281828459045; }
static constexpr Double sqrt2() { return 1.414213562373095; } static constexpr Double sqrt2() { return 1.414213562373095; }
static constexpr Double sqrt3() { return 1.732050807568877; } static constexpr Double sqrt3() { return 1.732050807568877; }
}; };
@ -78,7 +88,9 @@ template<> struct Constants<Float> {
Constants() = delete; Constants() = delete;
static constexpr Float pi() { return 3.141592654f; } static constexpr Float pi() { return 3.141592654f; }
static constexpr Float piHalf() { return 1.570796327f; }
static constexpr Float tau() { return 6.283185307f; } static constexpr Float tau() { return 6.283185307f; }
static constexpr Float e() { return 2.718281828f; }
static constexpr Float sqrt2() { return 1.414213562f; } static constexpr Float sqrt2() { return 1.414213562f; }
static constexpr Float sqrt3() { return 1.732050808f; } static constexpr Float sqrt3() { return 1.732050808f; }
}; };

10
src/Magnum/Math/Test/ConstantsTest.cpp

@ -50,8 +50,13 @@ void ConstantsTest::constantsFloat() {
CORRADE_COMPARE(Math::pow<2>(b), 3.0f); CORRADE_COMPARE(Math::pow<2>(b), 3.0f);
constexpr Float c = Constants<Float>::pi(); constexpr Float c = Constants<Float>::pi();
constexpr Float d = Constants<Float>::piHalf();
constexpr Float e = Constants<Float>::tau(); constexpr Float e = Constants<Float>::tau();
CORRADE_COMPARE(0.5f*c, d);
CORRADE_COMPARE(2.0f*c, e); CORRADE_COMPARE(2.0f*c, e);
constexpr Float f = Constants<Float>::e();
CORRADE_COMPARE(std::log(f), 1.0f);
} }
void ConstantsTest::constantsDouble() { void ConstantsTest::constantsDouble() {
@ -62,8 +67,13 @@ void ConstantsTest::constantsDouble() {
CORRADE_COMPARE(Math::pow<2>(b), 3.0); CORRADE_COMPARE(Math::pow<2>(b), 3.0);
constexpr Double c = Constants<Double>::pi(); constexpr Double c = Constants<Double>::pi();
constexpr Double d = Constants<Double>::piHalf();
constexpr Double e = Constants<Double>::tau(); constexpr Double e = Constants<Double>::tau();
CORRADE_COMPARE(0.5*c, d);
CORRADE_COMPARE(2.0*c, e); CORRADE_COMPARE(2.0*c, e);
constexpr Double f = Constants<Double>::e();
CORRADE_COMPARE(std::log(f), 1.0);
#else #else
CORRADE_SKIP("Double precision is not supported when targeting OpenGL ES."); CORRADE_SKIP("Double precision is not supported when targeting OpenGL ES.");
#endif #endif

Loading…
Cancel
Save