mirror of
https://github.com/ETLCPP/etl.git
synced 2026-04-30 19:09:10 +08:00
Added pow<> template
Compile time calculation of powers.
This commit is contained in:
parent
487671a024
commit
ed9e56ef6d
30
math.h
30
math.h
@ -87,7 +87,7 @@ namespace etl
|
||||
/// Calculates base 2 logs.
|
||||
//***************************************************************************
|
||||
template <const size_t N>
|
||||
struct Log2
|
||||
struct log2
|
||||
{
|
||||
enum value_type
|
||||
{
|
||||
@ -100,13 +100,39 @@ namespace etl
|
||||
/// Calculates base 10 logs.
|
||||
//***************************************************************************
|
||||
template <const size_t N>
|
||||
struct Log10
|
||||
struct log10
|
||||
{
|
||||
enum value_type
|
||||
{
|
||||
value = log<N, 10>::value
|
||||
};
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
///\ingroup exp
|
||||
/// Calculates powers.
|
||||
//***************************************************************************
|
||||
template <const size_t N, const size_t POWER>
|
||||
struct pow
|
||||
{
|
||||
enum value_type
|
||||
{
|
||||
value = N * pow<N, POWER - 1>::value
|
||||
};
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
///\ingroup exp
|
||||
/// Specialisation for POWER == 0
|
||||
//***************************************************************************
|
||||
template <const size_t N>
|
||||
struct pow<N, 0>
|
||||
{
|
||||
enum value_type
|
||||
{
|
||||
value = 1
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
164
test/test_math.cpp
Normal file
164
test/test_math.cpp
Normal file
@ -0,0 +1,164 @@
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
|
||||
Copyright(c) 2014 jwellbelove
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files(the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions :
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
******************************************************************************/
|
||||
|
||||
#include <UnitTest++/UnitTest++.h>
|
||||
|
||||
#include "../math.h"
|
||||
|
||||
namespace
|
||||
{
|
||||
SUITE(TestMath)
|
||||
{
|
||||
//*************************************************************************
|
||||
TEST(TestLog0_BASE)
|
||||
{
|
||||
int actual;
|
||||
|
||||
actual = etl::log<0, 2>::value;
|
||||
CHECK_EQUAL(0, actual);
|
||||
|
||||
actual = etl::log<0, 10>::value;
|
||||
CHECK_EQUAL(0, actual);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(TestLog1_BASE)
|
||||
{
|
||||
int actual;
|
||||
|
||||
actual = etl::log<1, 2>::value;
|
||||
CHECK_EQUAL(0, actual);
|
||||
|
||||
actual = etl::log<1, 10>::value;
|
||||
CHECK_EQUAL(0, actual);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(TestLog10_BASE)
|
||||
{
|
||||
int actual;
|
||||
|
||||
actual = etl::log<10, 2>::value;
|
||||
CHECK_EQUAL(3, actual);
|
||||
|
||||
actual = etl::log<10, 10>::value;
|
||||
CHECK_EQUAL(1, actual);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(TestLog100_BASE)
|
||||
{
|
||||
int actual;
|
||||
|
||||
actual = etl::log<100, 2>::value;
|
||||
CHECK_EQUAL(6, actual);
|
||||
|
||||
actual = etl::log<100, 10>::value;
|
||||
CHECK_EQUAL(2, actual);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(TestLog2)
|
||||
{
|
||||
int actual;
|
||||
|
||||
actual = etl::log2<0>::value;
|
||||
CHECK_EQUAL(0, actual);
|
||||
|
||||
actual = etl::log2<1>::value;
|
||||
CHECK_EQUAL(0, actual);
|
||||
|
||||
actual = etl::log2<10>::value;
|
||||
CHECK_EQUAL(3, actual);
|
||||
|
||||
actual = etl::log2<100>::value;
|
||||
CHECK_EQUAL(6, actual);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(TestLog10)
|
||||
{
|
||||
int actual;
|
||||
|
||||
actual = etl::log10<0>::value;
|
||||
CHECK_EQUAL(0, actual);
|
||||
|
||||
actual = etl::log10<1>::value;
|
||||
CHECK_EQUAL(0, actual);
|
||||
|
||||
actual = etl::log10<10>::value;
|
||||
CHECK_EQUAL(1, actual);
|
||||
|
||||
actual = etl::log10<100>::value;
|
||||
CHECK_EQUAL(2, actual);
|
||||
|
||||
actual = etl::log10<100>::value;
|
||||
CHECK_EQUAL(2, actual);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(TestPow)
|
||||
{
|
||||
int actual;
|
||||
|
||||
// 2^1
|
||||
actual = etl::pow<2, 1>::value;
|
||||
CHECK_EQUAL(2, actual);
|
||||
|
||||
// 3^2
|
||||
actual = etl::pow<3, 2>::value;
|
||||
CHECK_EQUAL(9, actual);
|
||||
|
||||
// 4^3
|
||||
actual = etl::pow<4, 3>::value;
|
||||
CHECK_EQUAL(64, actual);
|
||||
|
||||
// 5^4
|
||||
actual = etl::pow<5, 4>::value;
|
||||
CHECK_EQUAL(625, actual);
|
||||
|
||||
// 6^5
|
||||
actual = etl::pow<6, 5>::value;
|
||||
CHECK_EQUAL(7776, actual);
|
||||
|
||||
// 7^6
|
||||
actual = etl::pow<7, 6>::value;
|
||||
CHECK_EQUAL(117649, actual);
|
||||
|
||||
// 8^7
|
||||
actual = etl::pow<8, 7>::value;
|
||||
CHECK_EQUAL(2097152, actual);
|
||||
|
||||
// 9^8
|
||||
actual = etl::pow<9, 8>::value;
|
||||
CHECK_EQUAL(43046721, actual);
|
||||
|
||||
// 10^9
|
||||
actual = etl::pow<10, 9>::value;
|
||||
CHECK_EQUAL(1000000000, actual);
|
||||
}
|
||||
};
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user