numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
pow.h
Go to the documentation of this file.
1/*
2 * Copyright 2021 MusicScience37 (Kenta Kabashima)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
20#pragma once
21
22#include <type_traits>
23
27#include "num_collect/constants/one.h" // IWYU pragma: keep
28#include "num_collect/constants/zero.h" // IWYU pragma: keep
29
30namespace num_collect::constants {
31
45template <typename B, typename E,
46 std::enable_if_t<std::is_integral_v<E>, void*> = nullptr>
47constexpr auto pow(B base, E exp) -> B {
48 if constexpr (std::is_signed_v<E>) {
49 if (exp < zero<E>) {
50 return one<B> / pow(base, -exp);
51 }
52 }
53 return impl::pow_pos_int(base, exp);
54}
55
68template <typename T,
69 std::enable_if_t<std::is_floating_point_v<T>, void*> = nullptr>
70constexpr auto pow(T base, T exp) -> T {
71 return ::num_collect::constants::exp(exp * log(base));
72}
73
74} // namespace num_collect::constants
Definition of exp function.
Definition of log function.
constexpr auto pow_pos_int(T base, I exp) -> T
Calculate the value of base raised to the power exp.
Definition pow_pos_int.h:40
Namespace of constexpr variables and functions.
Definition cbrt.h:24
constexpr auto exp(T x) -> T
Calculate exponential function .
Definition exp.h:46
constexpr T zero
Value 0.
Definition zero.h:30
constexpr auto log(T x) -> T
Calculate logarithm .
Definition log.h:43
constexpr T one
Value 1.
Definition one.h:30
constexpr auto pow(B base, E exp) -> B
Calculate power .
Definition pow.h:47
Definition of one.
Definition of pow_pos_int function.
Definition of zero.