Browse Source

Math: added pow(), log() and exp().

Just wrappers around std::pow(), std::log() and std::exp() to have the
complete toolbox consistent.
pull/122/merge
Vladimír Vondruš 11 years ago
parent
commit
9ebd6ad46d
  1. 24
      src/Magnum/Math/Functions.h
  2. 18
      src/Magnum/Math/Test/FunctionsTest.cpp

24
src/Magnum/Math/Functions.h

@ -66,6 +66,14 @@ template<UnsignedInt exponent, class T> constexpr T pow(T base) {
return Implementation::Pow<exponent>::pow(base); return Implementation::Pow<exponent>::pow(base);
} }
/**
@brief Power
Returns power of @p base to the @p exponent.
@see @ref pow(T), @ref exp()
*/
template<class T> T pow(T base, T exponent) { return std::pow(base, exponent); }
/** /**
@brief Integral logarithm @brief Integral logarithm
@ -82,6 +90,22 @@ Returns integral logarithm of given number with base `2`.
*/ */
UnsignedInt MAGNUM_EXPORT log2(UnsignedInt number); UnsignedInt MAGNUM_EXPORT log2(UnsignedInt number);
/**
@brief Natural logarithm
Returns natural (base @f$ e @f$) logarithm of given number.
@see @ref Constants::e(), @ref log(UnsignedInt, UnsignedInt), @ref log2()
*/
template<class T> T log(T number) { return std::log(number); }
/**
@brief Natural exponential
Returns @f$ e^x @f$.
@see @ref Constants::e(), @ref pow(T, T)
*/
template<class T> T exp(T exponent) { return std::exp(exponent); }
/** /**
@brief Integer division with remainder @brief Integer division with remainder

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

@ -65,8 +65,11 @@ struct FunctionsTest: Corrade::TestSuite::Tester {
void normalizeTypeDeduction(); void normalizeTypeDeduction();
void powIntegral(); void powIntegral();
void pow();
void logIntegral(); void logIntegral();
void log2(); void log2();
void log();
void exp();
void div(); void div();
void trigonometric(); void trigonometric();
void trigonometricWithBase(); void trigonometricWithBase();
@ -113,8 +116,11 @@ FunctionsTest::FunctionsTest() {
&FunctionsTest::normalizeTypeDeduction, &FunctionsTest::normalizeTypeDeduction,
&FunctionsTest::powIntegral, &FunctionsTest::powIntegral,
&FunctionsTest::pow,
&FunctionsTest::logIntegral, &FunctionsTest::logIntegral,
&FunctionsTest::log2, &FunctionsTest::log2,
&FunctionsTest::log,
&FunctionsTest::exp,
&FunctionsTest::div, &FunctionsTest::div,
&FunctionsTest::trigonometric, &FunctionsTest::trigonometric,
&FunctionsTest::trigonometricWithBase}); &FunctionsTest::trigonometricWithBase});
@ -441,6 +447,10 @@ void FunctionsTest::powIntegral() {
CORRADE_COMPARE(a, 125); CORRADE_COMPARE(a, 125);
} }
void FunctionsTest::pow() {
CORRADE_COMPARE(Math::pow(2.0f, 0.5f), 1.414213562f);
}
void FunctionsTest::logIntegral() { void FunctionsTest::logIntegral() {
CORRADE_COMPARE(Math::log(2, 256), 8ul); CORRADE_COMPARE(Math::log(2, 256), 8ul);
CORRADE_COMPARE(Math::log(256, 2), 0ul); CORRADE_COMPARE(Math::log(256, 2), 0ul);
@ -450,6 +460,14 @@ void FunctionsTest::log2() {
CORRADE_COMPARE(Math::log2(2153), 11); CORRADE_COMPARE(Math::log2(2153), 11);
} }
void FunctionsTest::log() {
CORRADE_COMPARE(Math::log(2.0f), 0.693147f);
}
void FunctionsTest::exp() {
CORRADE_COMPARE(Math::exp(0.693147f), 2.0f);
}
void FunctionsTest::div() { void FunctionsTest::div() {
Int quotient, remainder; Int quotient, remainder;
std::tie(quotient, remainder) = Math::div(57, 6); std::tie(quotient, remainder) = Math::div(57, 6);

Loading…
Cancel
Save