mirror of
https://github.com/boostorg/algorithm.git
synced 2025-07-06 09:16:33 +02:00
Added more general power functionality as requested by Sean Parent. Also added enable_if to make sure the exponent is an integral type.
This commit is contained in:
@ -18,32 +18,68 @@
|
|||||||
#define BOOST_ALGORITHM_HPP
|
#define BOOST_ALGORITHM_HPP
|
||||||
|
|
||||||
#include <boost/utility/enable_if.hpp> // for boost::disable_if
|
#include <boost/utility/enable_if.hpp> // for boost::disable_if
|
||||||
|
#include <boost/type_traits/is_integral.hpp>
|
||||||
|
|
||||||
namespace boost { namespace algorithm {
|
namespace boost { namespace algorithm {
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
T identity_operation ( std::multiplies<T> ) { return T(1); }
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
T identity_operation ( std::plus<T> ) { return T(0); }
|
||||||
|
|
||||||
|
|
||||||
/// \fn power ( T x, Integer n )
|
/// \fn power ( T x, Integer n )
|
||||||
/// \return the value "x" raised to the power "n"
|
/// \return the value "x" raised to the power "n"
|
||||||
///
|
///
|
||||||
/// \param x The value to be exponentiated
|
/// \param x The value to be exponentiated
|
||||||
/// \param n The exponent
|
/// \param n The exponent (must be >= 0)
|
||||||
///
|
///
|
||||||
// \remark Taken from Knuth, The Art of Computer Programming, Volume 2:
|
// \remark Taken from Knuth, The Art of Computer Programming, Volume 2:
|
||||||
// Seminumerical Algorithms, Section 4.6.3
|
// Seminumerical Algorithms, Section 4.6.3
|
||||||
template <typename T, typename Integer>
|
template <typename T, typename Integer>
|
||||||
T power (T x, Integer n) {
|
typename boost::enable_if<boost::is_integral<Integer>, T>::type
|
||||||
T y = 1; // Should be "T y{1};"
|
power (T x, Integer n) {
|
||||||
if (n == 0) return y;
|
T y = 1; // Should be "T y{1};"
|
||||||
while (true) {
|
if (n == 0) return y;
|
||||||
if (n % 2 == 1) {
|
while (true) {
|
||||||
y = x * y;
|
if (n % 2 == 1) {
|
||||||
if (n == 1)
|
y = x * y;
|
||||||
return y;
|
if (n == 1)
|
||||||
}
|
return y;
|
||||||
n = n / 2;
|
}
|
||||||
x = x * x;
|
n = n / 2;
|
||||||
}
|
x = x * x;
|
||||||
return y;
|
}
|
||||||
}
|
return y;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// \fn power ( T x, Integer n, Operation op )
|
||||||
|
/// \return the value "x" raised to the power "n"
|
||||||
|
/// using the operaton "op".
|
||||||
|
///
|
||||||
|
/// \param x The value to be exponentiated
|
||||||
|
/// \param n The exponent (must be >= 0)
|
||||||
|
/// \param op The operation used
|
||||||
|
///
|
||||||
|
// \remark Taken from Knuth, The Art of Computer Programming, Volume 2:
|
||||||
|
// Seminumerical Algorithms, Section 4.6.3
|
||||||
|
template <typename T, typename Integer, typename Operation>
|
||||||
|
typename boost::enable_if<boost::is_integral<Integer>, T>::type
|
||||||
|
power (T x, Integer n, Operation op) {
|
||||||
|
T y = identity_operation(op);
|
||||||
|
if (n == 0) return y;
|
||||||
|
while (true) {
|
||||||
|
if (n % 2 == 1) {
|
||||||
|
y = op(x, y);
|
||||||
|
if (n == 1)
|
||||||
|
return y;
|
||||||
|
}
|
||||||
|
n = n / 2;
|
||||||
|
x = op(x, x);
|
||||||
|
}
|
||||||
|
return y;
|
||||||
|
}
|
||||||
|
|
||||||
}}
|
}}
|
||||||
|
|
||||||
|
@ -30,6 +30,7 @@ alias unit_test_framework
|
|||||||
# Misc tests
|
# Misc tests
|
||||||
[ run clamp_test.cpp unit_test_framework : : : : clamp_test ]
|
[ run clamp_test.cpp unit_test_framework : : : : clamp_test ]
|
||||||
[ run power_test.cpp unit_test_framework : : : : power_test ]
|
[ run power_test.cpp unit_test_framework : : : : power_test ]
|
||||||
|
[ compile-fail power_fail1.cpp : : : : ]
|
||||||
|
|
||||||
# Cxx11 tests
|
# Cxx11 tests
|
||||||
[ run all_of_test.cpp unit_test_framework : : : : all_of_test ]
|
[ run all_of_test.cpp unit_test_framework : : : : all_of_test ]
|
||||||
|
24
test/power_fail1.cpp
Normal file
24
test/power_fail1.cpp
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
/*
|
||||||
|
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 <iostream>
|
||||||
|
|
||||||
|
#include <boost/config.hpp>
|
||||||
|
#include <boost/algorithm/algorithm.hpp>
|
||||||
|
|
||||||
|
#define BOOST_TEST_MAIN
|
||||||
|
#include <boost/test/unit_test.hpp>
|
||||||
|
|
||||||
|
namespace ba = boost::algorithm;
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE( test_main )
|
||||||
|
{
|
||||||
|
// Second argument must be an integral value
|
||||||
|
BOOST_CHECK ( ba::power(1, 1.0) == 1);
|
||||||
|
}
|
@ -15,22 +15,21 @@
|
|||||||
#define BOOST_TEST_MAIN
|
#define BOOST_TEST_MAIN
|
||||||
#include <boost/test/unit_test.hpp>
|
#include <boost/test/unit_test.hpp>
|
||||||
|
|
||||||
#include <string>
|
|
||||||
#include <vector>
|
|
||||||
#include <list>
|
|
||||||
|
|
||||||
namespace ba = boost::algorithm;
|
namespace ba = boost::algorithm;
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE( test_main )
|
BOOST_AUTO_TEST_CASE( test_main )
|
||||||
{
|
{
|
||||||
BOOST_CHECK ( ba::power(0, 0) == 1);
|
BOOST_CHECK ( ba::power(0, 0) == 1);
|
||||||
BOOST_CHECK ( ba::power(5, 0) == 1);
|
BOOST_CHECK ( ba::power(5, 0) == 1);
|
||||||
BOOST_CHECK ( ba::power(1, 1) == 1);
|
BOOST_CHECK ( ba::power(1, 1) == 1);
|
||||||
BOOST_CHECK ( ba::power(1, 4) == 1);
|
BOOST_CHECK ( ba::power(1, 4) == 1);
|
||||||
BOOST_CHECK ( ba::power(3, 2) == 9);
|
BOOST_CHECK ( ba::power(3, 2) == 9);
|
||||||
BOOST_CHECK ( ba::power(2, 3) == 8);
|
BOOST_CHECK ( ba::power(2, 3) == 8);
|
||||||
BOOST_CHECK ( ba::power(3, 3) == 27);
|
BOOST_CHECK ( ba::power(3, 3) == 27);
|
||||||
BOOST_CHECK ( ba::power(2, 30) == 0x40000000);
|
BOOST_CHECK ( ba::power(2, 30) == 0x40000000);
|
||||||
BOOST_CHECK ( ba::power(5L, 10) == 3125*3125);
|
BOOST_CHECK ( ba::power(5L, 10) == 3125*3125);
|
||||||
BOOST_CHECK ( ba::power(18, 3) == 18*18*18);
|
BOOST_CHECK ( ba::power(18, 3) == 18*18*18);
|
||||||
|
|
||||||
|
BOOST_CHECK ( ba::power(3,2) == ba::power(3,2, std::multiplies<int>()));
|
||||||
|
BOOST_CHECK ( ba::power(3,2, std::plus<int>()) == 6);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user