numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
exp.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 <limits>
23#include <type_traits>
24
27#include "num_collect/constants/napier.h" // IWYU pragma: keep
28#include "num_collect/constants/one.h" // IWYU pragma: keep
30#include "num_collect/constants/zero.h" // IWYU pragma: keep
31
32namespace num_collect::constants {
33
45template <typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
46constexpr auto exp(T x) -> T {
47 if (x < zero<T>) {
48 return one<T> / exp(-x);
49 }
50 if (x < std::numeric_limits<T>::min()) {
51 return one<T>;
52 }
53
54 constexpr auto thresh_inf =
55 static_cast<T>(std::numeric_limits<T>::max_exponent);
56 if (x >= thresh_inf) {
57 return std::numeric_limits<T>::infinity();
58 }
59
60 int int_part = static_cast<int>(trunc(x));
61 T rem_part = x - static_cast<T>(int_part);
62 T exp_int_part = impl::pow_pos_int(napier<T>, int_part);
63 T exp_rem_part = impl::exp_maclaurin(rem_part);
64 return exp_int_part * exp_rem_part;
65}
66
67} // namespace num_collect::constants
Definition of exp_maclaurin 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
constexpr auto exp_maclaurin(T x) -> T
Calculate exponential function with Maclaurin series.
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 T napier
Value of Napier's constant (or Euler's number), .
Definition napier.h:41
constexpr T one
Value 1.
Definition one.h:30
constexpr auto trunc(T x) -> T
Truncate the decimal part of a number x.
Definition trunc.h:43
Definition of napier.
Definition of one.
Definition of pow_pos_int function.
Definition of trunc function.
Definition of zero.