numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
pow_pos_int.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 "num_collect/constants/one.h" // IWYU pragma: keep
23#include "num_collect/constants/two.h" // IWYU pragma: keep
24#include "num_collect/constants/zero.h" // IWYU pragma: keep
25
27
39template <typename T, typename I>
40constexpr auto pow_pos_int(T base, I exp) -> T {
41 if (exp == zero<I>) {
42 return one<T>;
43 }
44 if (exp == one<I>) {
45 return base;
46 }
47 if (exp == two<I>) {
48 return base * base;
49 }
50 I exp1 = exp / two<I>;
51 I exp2 = exp - exp1;
52 return pow_pos_int(base, exp1) * pow_pos_int(base, exp2);
53}
54
55} // namespace num_collect::constants::impl
Namespace of internal implementations.
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
constexpr T two
Value 2.
Definition two.h:30
constexpr auto exp(T x) -> T
Calculate exponential function .
Definition exp.h:46
constexpr T zero
Value 0.
Definition zero.h:30
constexpr T one
Value 1.
Definition one.h:30
Definition of one.
Definition of two.
Definition of zero.