WPILibC++ 2023.4.3-108-ge5452e3
pow.hpp
Go to the documentation of this file.
1/*################################################################################
2 ##
3 ## Copyright (C) 2016-2022 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
28namespace internal
29{
30
31template<typename T>
32constexpr
33T
34pow_dbl(const T base, const T exp_term)
35noexcept
36{
37 return exp(exp_term*log(base));
38}
39
40template<typename T1, typename T2, typename TC = common_t<T1,T2>,
41 typename std::enable_if<!std::is_integral<T2>::value>::type* = nullptr>
42constexpr
43TC
44pow_check(const T1 base, const T2 exp_term)
45noexcept
46{
47 return( base < T1(0) ? \
48 GCLIM<TC>::quiet_NaN() :
49 //
50 pow_dbl(static_cast<TC>(base),static_cast<TC>(exp_term)) );
51}
52
53template<typename T1, typename T2, typename TC = common_t<T1,T2>,
54 typename std::enable_if<std::is_integral<T2>::value>::type* = nullptr>
55constexpr
56TC
57pow_check(const T1 base, const T2 exp_term)
58noexcept
59{
60 return pow_integral(base,exp_term);
61}
62
63}
64
65/**
66 * Compile-time power function
67 *
68 * @param base a real-valued input.
69 * @param exp_term a real-valued input.
70 * @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$
71 */
72
73template<typename T1, typename T2>
74constexpr
75common_t<T1,T2>
76pow(const T1 base, const T2 exp_term)
77noexcept
78{
79 return internal::pow_check(base,exp_term);
80}
81
82#endif
const Eigen::CwiseBinaryOp< Eigen::internal::scalar_pow_op< typename Derived::Scalar, typename ExponentDerived::Scalar >, const Derived, const ExponentDerived > pow(const Eigen::ArrayBase< Derived > &x, const Eigen::ArrayBase< ExponentDerived > &exponents)
Definition: GlobalFunctions.h:143
dimensionless::scalar_t log(const ScalarUnit x) noexcept
Compute natural logarithm.
Definition: math.h:349
Definition: Eigen_Colamd.h:50
constexpr T pow_dbl(const T base, const T exp_term) noexcept
Definition: pow.hpp:34
constexpr T1 pow_integral(const T1 base, const T2 exp_term) noexcept
Definition: pow_integral.hpp:120
constexpr TC pow_check(const T1 base, const T2 exp_term) noexcept
Definition: pow.hpp:44