mirror of
https://github.com/boostorg/charconv.git
synced 2026-02-17 01:32:13 +00:00
Add functions for powers and factors
This commit is contained in:
43
include/boost/charconv/detail/power_factor.hpp
Normal file
43
include/boost/charconv/detail/power_factor.hpp
Normal file
@@ -0,0 +1,43 @@
|
||||
// Copyright 2020-2023 Junekey Jeon
|
||||
// Copyright 2023 Matt Borland
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// https://www.boost.org/LICENSE_1_0.txt
|
||||
|
||||
#ifndef BOOST_CHARCONV_DETAIL_POWER_FACTOR_HPP
|
||||
#define BOOST_CHARCONV_DETAIL_POWER_FACTOR_HPP
|
||||
|
||||
#include <boost/charconv/detail/config.hpp>
|
||||
#include <cstdint>
|
||||
|
||||
namespace boost { namespace charconv { namespace detail {
|
||||
|
||||
template <int k, typename Integer>
|
||||
BOOST_CHARCONV_CXX14_CONSTEXPR Integer compute_power(Integer a) noexcept
|
||||
{
|
||||
Integer p = 1;
|
||||
for (Integer i = 0; i < k; ++i)
|
||||
{
|
||||
p *= a;
|
||||
}
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
template <unsigned a, typename Unsigned_Integer>
|
||||
BOOST_CHARCONV_CXX14_CONSTEXPR int count_factors(Unsigned_Integer n) noexcept
|
||||
{
|
||||
static_assert(a > 1);
|
||||
|
||||
int c = 0;
|
||||
while (n % a == 0)
|
||||
{
|
||||
n /= a;
|
||||
++c;
|
||||
}
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
}}} // Namespaces
|
||||
|
||||
#endif // BOOST_CHARCONV_DETAIL_POWER_FACTOR_HPP
|
||||
Reference in New Issue
Block a user