Browse Source

Added Math::pow() and Math::log() functions.

Math::pow() is templated on exponent, so it should be as fast as
possible (without recursion).
vectorfields
Vladimír Vondruš 15 years ago
parent
commit
e536c892c4
  1. 2
      src/CMakeLists.txt
  2. 4
      src/Math/CMakeLists.txt
  3. 27
      src/Math/Math.cpp
  4. 22
      src/Math/Math.h
  5. 1
      src/Math/Test/CMakeLists.txt
  6. 10
      src/Math/Test/MathTest.cpp
  7. 2
      src/Math/Test/MathTest.h

2
src/CMakeLists.txt

@ -20,7 +20,7 @@ set(Magnum_SRCS
)
add_library(Magnum SHARED ${Magnum_SRCS})
target_link_libraries(Magnum CorradePluginManager CorradeUtility ${OPENGL_gl_LIBRARY} ${GLEW_LIBRARY})
target_link_libraries(Magnum CorradePluginManager CorradeUtility MagnumMath ${OPENGL_gl_LIBRARY} ${GLEW_LIBRARY})
install(TARGETS Magnum DESTINATION ${MAGNUM_LIBRARY_INSTALL_DIR})

4
src/Math/CMakeLists.txt

@ -1,5 +1,9 @@
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
add_library(MagnumMath STATIC
Math.cpp
)
if(BUILD_TESTS)
enable_testing()
add_subdirectory(Test)

27
src/Math/Math.cpp

@ -0,0 +1,27 @@
/*
Copyright © 2010, 2011 Vladimír Vondruš <mosra@centrum.cz>
This file is part of Magnum.
Magnum is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License version 3
only, as published by the Free Software Foundation.
Magnum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License version 3 for more details.
*/
#include "Math.h"
namespace Magnum { namespace Math {
size_t log(size_t base, size_t number) {
size_t log = 0;
while(number /= base)
++log;
return log;
}
}}

22
src/Math/Math.h

@ -15,6 +15,8 @@
GNU Lesser General Public License version 3 for more details.
*/
#include <cstddef>
/** @file
* @brief Math constants and utilities
*/
@ -27,6 +29,26 @@ namespace Magnum { namespace Math {
/** @brief Maximal tolerance when comparing floats */
#define EPSILON 1.0e-6
/**
* @brief Integral power
*
* Returns integral power of base to the exponent.
*/
template<size_t exponent> inline constexpr size_t pow(size_t base) {
return base*pow<exponent-1>(base);
}
#ifndef DOXYGEN_GENERATING_OUTPUT
template<> inline constexpr size_t pow<0>(size_t base) { return 1; }
#endif
/**
* @brief Integral logarithm
*
* Returns integral logarithm of given number with given base.
*/
size_t log(size_t base, size_t number);
/**
* @brief Angle in degrees
*

1
src/Math/Test/CMakeLists.txt

@ -15,4 +15,5 @@ corrade_add_test(Matrix4Test Matrix4Test.h Matrix4Test.cpp)
target_link_libraries(Matrix4Test ${CORRADE_UTILITY_LIBRARY})
corrade_add_test(MathTest MathTest.h MathTest.cpp)
target_link_libraries(MathTest MagnumMath)
corrade_add_test(GeometryUtilsTest GeometryUtilsTest.h GeometryUtilsTest.cpp)

10
src/Math/Test/MathTest.cpp

@ -32,4 +32,14 @@ void MathTest::degrad() {
QVERIFY(deg(90) == PI/2);
}
void MathTest::pow() {
QCOMPARE(Math::pow<10>(2), 1024ul);
QCOMPARE(Math::pow<0>(3), 1ul);
}
void MathTest::log() {
QCOMPARE(Math::log(2, 256), 8ul);
QCOMPARE(Math::log(256, 2), 0ul);
}
}}}

2
src/Math/Test/MathTest.h

@ -24,6 +24,8 @@ class MathTest: public QObject {
private slots:
void degrad();
void pow();
void log();
};
}}}

Loading…
Cancel
Save