WPILibC++ 2024.3.2
pow.hpp
Go to the documentation of this file.
1/*################################################################################
2 ##
3 ## Copyright (C) 2016-2023 Keith O'Hara
4 ##
5 ## This file is part of the GCE-Math C++ library.
6 ##
7 ## Licensed under the Apache License, Version 2.0 (the "License");
8 ## you may not use this file except in compliance with the License.
9 ## You may obtain a copy of the License at
10 ##
11 ## http://www.apache.org/licenses/LICENSE-2.0
12 ##
13 ## Unless required by applicable law or agreed to in writing, software
14 ## distributed under the License is distributed on an "AS IS" BASIS,
15 ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 ## See the License for the specific language governing permissions and
17 ## limitations under the License.
18 ##
19 ################################################################################*/
20
21/*
22 * compile-time power function
23 */
24
25#ifndef _gcem_pow_HPP
26#define _gcem_pow_HPP
27
28#include <cmath>
29#include <type_traits>
30
31namespace gcem
32{
33
34namespace internal
35{
36
37template<typename T>
38constexpr
39T
40pow_dbl(const T base, const T exp_term)
41noexcept
42{
43 return exp(exp_term*log(base));
44}
45
46template<typename T1, typename T2, typename TC = common_t<T1,T2>,
47 typename std::enable_if<!std::is_integral<T2>::value>::type* = nullptr>
48constexpr
49TC
50pow_check(const T1 base, const T2 exp_term)
51noexcept
52{
53 return( base < T1(0) ? \
55 //
56 pow_dbl(static_cast<TC>(base),static_cast<TC>(exp_term)) );
57}
58
59template<typename T1, typename T2, typename TC = common_t<T1,T2>,
60 typename std::enable_if<std::is_integral<T2>::value>::type* = nullptr>
61constexpr
62TC
63pow_check(const T1 base, const T2 exp_term)
64noexcept
65{
66 return pow_integral(base,exp_term);
67}
68
69}
70
71/**
72 * Compile-time power function
73 *
74 * @param base a real-valued input.
75 * @param exp_term a real-valued input.
76 * @return Computes \c base raised to the power \c exp_term. In the case where \c exp_term is integral-valued, recursion by squaring is used, otherwise \f$ \text{base}^{\text{exp\_term}} = e^{\text{exp\_term} \log(\text{base})} \f$
77 */
78
79template<typename T1, typename T2>
80constexpr
81common_t<T1,T2>
82pow(const T1 base, const T2 exp_term)
83noexcept
84{
86 return internal::pow_check(base,exp_term);
87 } else {
88 return std::pow(base, exp_term);
89 }
90}
91
92}
93
94#endif
constexpr FMT_INLINE auto is_constant_evaluated(bool default_value=false) noexcept -> bool
Definition: core.h:304
constexpr T pow_dbl(const T base, const T exp_term) noexcept
Definition: pow.hpp:40
constexpr T1 pow_integral(const T1 base, const T2 exp_term) noexcept
Definition: pow_integral.hpp:123
constexpr TC pow_check(const T1 base, const T2 exp_term) noexcept
Definition: pow.hpp:50
Definition: is_even.hpp:29
constexpr return_t< T > log(const T x) noexcept
Compile-time natural logarithm function.
Definition: log.hpp:186
constexpr common_t< T1, T2 > pow(const T1 base, const T2 exp_term) noexcept
Compile-time power function.
Definition: pow.hpp:82
std::numeric_limits< T > GCLIM
Definition: gcem_options.hpp:74