Browse Source

Math: added sincos().

Mainly a convenience function in case you want to compute sin and cos of
the same, potentially longer expression, and you don't want to have
repeated code or temporary variables. On some architectures might use
faster instruction that computes both values in one shot.
pull/118/head
Vladimír Vondruš 11 years ago
parent
commit
e7cae67e9f
  1. 27
      src/Magnum/Math/Functions.h
  2. 6
      src/Magnum/Math/Test/FunctionsTest.cpp

27
src/Magnum/Math/Functions.h

@ -104,7 +104,11 @@ template<class Integral> std::pair<Integral, Integral> div(Integral x, Integral
/* The functions accept Unit instead of Rad to make them working with operator
products (e.g. 2*35.0_degf, which is of type Unit) */
/** @brief Sine */
/**
@brief Sine
@see @ref sincos()
*/
#ifdef DOXYGEN_GENERATING_OUTPUT
template<class T> inline T sin(Rad<T> angle);
#else
@ -112,7 +116,11 @@ template<class T> inline T sin(Unit<Rad, T> angle) { return std::sin(T(angle));
template<class T> inline T sin(Unit<Deg, T> angle) { return sin(Rad<T>(angle)); }
#endif
/** @brief Cosine */
/**
@brief Cosine
@see @ref sincos()
*/
#ifdef DOXYGEN_GENERATING_OUTPUT
template<class T> inline T cos(Rad<T> angle);
#else
@ -120,6 +128,21 @@ template<class T> inline T cos(Unit<Rad, T> angle) { return std::cos(T(angle));
template<class T> inline T cos(Unit<Deg, T> angle) { return cos(Rad<T>(angle)); }
#endif
/**
@brief Sine and cosine
On some architectures might be faster than doing both computations separately.
@see @ref sin(), @ref cos()
*/
#ifdef DOXYGEN_GENERATING_OUTPUT
template<class T> inline std::pair<T, T> sincos(Rad<T> angle);
#else
template<class T> inline std::pair<T, T> sincos(Unit<Rad, T> angle) {
return {std::sin(T(angle)) ,std::cos(T(angle))};
}
template<class T> inline std::pair<T, T> sincos(Unit<Deg, T> angle) { return sincos(Rad<T>(angle)); }
#endif
/** @brief Tangent */
#ifdef DOXYGEN_GENERATING_OUTPUT
template<class T> inline T tan(Rad<T> angle);

6
src/Magnum/Math/Test/FunctionsTest.cpp

@ -453,6 +453,9 @@ void FunctionsTest::trigonometric() {
CORRADE_COMPARE(Math::cos(Rad(Constants::pi()/3)), 0.5f);
CORRADE_COMPARE_AS(Math::acos(0.5f), Deg(60.0f), Deg);
CORRADE_COMPARE(Math::sincos(Deg(30.0f)), std::make_pair(0.5f, 0.8660254037844386f));
CORRADE_COMPARE(Math::sincos(Rad(Constants::pi()/6)), std::make_pair(0.5f, 0.8660254037844386f));
CORRADE_COMPARE(Math::tan(Deg(45.0f)), 1.0f);
CORRADE_COMPARE(Math::tan(Rad(Constants::pi()/4)), 1.0f);
CORRADE_COMPARE_AS(Math::atan(1.0f), Deg(45.0f), Deg);
@ -469,6 +472,9 @@ void FunctionsTest::trigonometricWithBase() {
CORRADE_COMPARE(Math::cos(2*Deg(30.0f)), 0.5f);
CORRADE_COMPARE(Math::cos(2*Rad(Constants::pi()/6)), 0.5f);
CORRADE_COMPARE(Math::sincos(2*Deg(15.0f)), std::make_pair(0.5f, 0.8660254037844386f));
CORRADE_COMPARE(Math::sincos(2*Rad(Constants::pi()/12)), std::make_pair(0.5f, 0.8660254037844386f));
CORRADE_COMPARE(Math::tan(2*Deg(22.5f)), 1.0f);
CORRADE_COMPARE(Math::tan(2*Rad(Constants::pi()/8)), 1.0f);
}

Loading…
Cancel
Save