diff --git a/src/Magnum/Math/Functions.h b/src/Magnum/Math/Functions.h index 0f1d0db72..d9b23ef90 100644 --- a/src/Magnum/Math/Functions.h +++ b/src/Magnum/Math/Functions.h @@ -66,6 +66,14 @@ template constexpr T pow(T base) { return Implementation::Pow::pow(base); } +/** +@brief Power + +Returns power of @p base to the @p exponent. +@see @ref pow(T), @ref exp() +*/ +template T pow(T base, T exponent) { return std::pow(base, exponent); } + /** @brief Integral logarithm @@ -82,6 +90,22 @@ Returns integral logarithm of given number with base `2`. */ 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 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 T exp(T exponent) { return std::exp(exponent); } + /** @brief Integer division with remainder diff --git a/src/Magnum/Math/Test/FunctionsTest.cpp b/src/Magnum/Math/Test/FunctionsTest.cpp index adcd2200e..ab7b5e694 100644 --- a/src/Magnum/Math/Test/FunctionsTest.cpp +++ b/src/Magnum/Math/Test/FunctionsTest.cpp @@ -65,8 +65,11 @@ struct FunctionsTest: Corrade::TestSuite::Tester { void normalizeTypeDeduction(); void powIntegral(); + void pow(); void logIntegral(); void log2(); + void log(); + void exp(); void div(); void trigonometric(); void trigonometricWithBase(); @@ -113,8 +116,11 @@ FunctionsTest::FunctionsTest() { &FunctionsTest::normalizeTypeDeduction, &FunctionsTest::powIntegral, + &FunctionsTest::pow, &FunctionsTest::logIntegral, &FunctionsTest::log2, + &FunctionsTest::log, + &FunctionsTest::exp, &FunctionsTest::div, &FunctionsTest::trigonometric, &FunctionsTest::trigonometricWithBase}); @@ -441,6 +447,10 @@ void FunctionsTest::powIntegral() { CORRADE_COMPARE(a, 125); } +void FunctionsTest::pow() { + CORRADE_COMPARE(Math::pow(2.0f, 0.5f), 1.414213562f); +} + void FunctionsTest::logIntegral() { CORRADE_COMPARE(Math::log(2, 256), 8ul); CORRADE_COMPARE(Math::log(256, 2), 0ul); @@ -450,6 +460,14 @@ void FunctionsTest::log2() { 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() { Int quotient, remainder; std::tie(quotient, remainder) = Math::div(57, 6);