From 0c3f9a38f45125e265afe9155e47af8889b3204e Mon Sep 17 00:00:00 2001 From: Marshall Clow Date: Tue, 2 Dec 2014 14:38:25 -0800 Subject: [PATCH] Add new algorithm boost::power, which raises a number to an integer power --- include/boost/algorithm/algorithm.hpp | 50 +++++++++++++++++++++++++++ test/Jamfile.v2 | 6 ++-- test/power_test.cpp | 36 +++++++++++++++++++ 3 files changed, 88 insertions(+), 4 deletions(-) create mode 100644 include/boost/algorithm/algorithm.hpp create mode 100644 test/power_test.cpp diff --git a/include/boost/algorithm/algorithm.hpp b/include/boost/algorithm/algorithm.hpp new file mode 100644 index 0000000..3304831 --- /dev/null +++ b/include/boost/algorithm/algorithm.hpp @@ -0,0 +1,50 @@ +/* + Copyright (c) Marshall Clow 2014. + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + + Revision history: + 2 Dec 2014 mtc First version; power + +*/ + +/// \file algorithm.hpp +/// \brief Misc Algorithms +/// \author Marshall Clow +/// + +#ifndef BOOST_ALGORITHM_HPP +#define BOOST_ALGORITHM_HPP + +#include // for boost::disable_if + +namespace boost { namespace algorithm { + +/// \fn power ( T x, Integer n ) +/// \return the value "x" raised to the power "n" +/// +/// \param x The value to be exponentiated +/// \param n The exponent +/// +// \remark Taken from Knuth, The Art of Computer Programming, Volume 2: +// Seminumerical Algorithms, Section 4.6.3 +template +T power (T x, Integer n) { + T y = 1; // Should be "T y{1};" + if (n == 0) return y; + while (true) { + if (n % 2 == 1) { + y = x * y; + if (n == 1) + return y; + } + n = n / 2; + x = x * x; + } + return y; + } + +}} + +#endif // BOOST_ALGORITHM_HPP diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index bb4dff6..727cada 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -27,8 +27,9 @@ alias unit_test_framework [ compile-fail search_fail2.cpp : : : : ] [ compile-fail search_fail3.cpp : : : : ] -# Clamp tests +# Misc tests [ run clamp_test.cpp unit_test_framework : : : : clamp_test ] + [ run power_test.cpp unit_test_framework : : : : power_test ] # Cxx11 tests [ run all_of_test.cpp unit_test_framework : : : : all_of_test ] @@ -58,9 +59,6 @@ alias unit_test_framework [ run hex_test4.cpp unit_test_framework : : : : hex_test4 ] [ compile-fail hex_fail1.cpp ] -# Wrapper tests - [ run wrapper_test1.cpp unit_test_framework : : : : wrapper_test1 ] - # Gather tests [ run gather_test1.cpp unit_test_framework : : : : gather_test1 ] [ compile-fail gather_fail1.cpp ] diff --git a/test/power_test.cpp b/test/power_test.cpp new file mode 100644 index 0000000..b4d8cf9 --- /dev/null +++ b/test/power_test.cpp @@ -0,0 +1,36 @@ +/* + Copyright (c) Marshall Clow 2014. + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + + For more information, see http://www.boost.org +*/ + +#include + +#include +#include + +#define BOOST_TEST_MAIN +#include + +#include +#include +#include + +namespace ba = boost::algorithm; + +BOOST_AUTO_TEST_CASE( test_main ) +{ + BOOST_CHECK ( ba::power(0, 0) == 1); + BOOST_CHECK ( ba::power(5, 0) == 1); + BOOST_CHECK ( ba::power(1, 1) == 1); + BOOST_CHECK ( ba::power(1, 4) == 1); + BOOST_CHECK ( ba::power(3, 2) == 9); + BOOST_CHECK ( ba::power(2, 3) == 8); + BOOST_CHECK ( ba::power(3, 3) == 27); + BOOST_CHECK ( ba::power(2, 30) == 0x40000000); + BOOST_CHECK ( ba::power(5L, 10) == 3125*3125); + BOOST_CHECK ( ba::power(18, 3) == 18*18*18); +}