| 1 |
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */ |
| 2 |
|
| 3 |
#ifndef MPT_BASE_NUMBRES_HPP |
| 4 |
#define MPT_BASE_NUMBRES_HPP |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include "mpt/base/detect_compiler.hpp" |
| 9 |
#include "mpt/base/detect_quirks.hpp" |
| 10 |
#include "mpt/base/namespace.hpp" |
| 11 |
|
| 12 |
#if MPT_CXX_AT_LEAST(20) && !defined(MPT_LIBCXX_QUIRK_NO_NUMBERS) |
| 13 |
#include <numbers> |
| 14 |
#else |
| 15 |
#include <type_traits> |
| 16 |
#include <cmath> |
| 17 |
#include <math.h> |
| 18 |
#endif |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
namespace mpt { |
| 23 |
inline namespace MPT_INLINE_NS { |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
namespace numbers { |
| 28 |
|
| 29 |
#if MPT_CXX_AT_LEAST(20) && !defined(MPT_LIBCXX_QUIRK_NO_NUMBERS) |
| 30 |
|
| 31 |
template <typename T> |
| 32 |
inline constexpr T e_v = std::numbers::e_v<T>; |
| 33 |
template <typename T> |
| 34 |
inline constexpr T log2e_v = std::numbers::log2e_v<T>; |
| 35 |
template <typename T> |
| 36 |
inline constexpr T log10e_v = std::numbers::log10e_v<T>; |
| 37 |
template <typename T> |
| 38 |
inline constexpr T pi_v = std::numbers::pi_v<T>; |
| 39 |
template <typename T> |
| 40 |
inline constexpr T inv_pi_v = std::numbers::inv_pi_v<T>; |
| 41 |
template <typename T> |
| 42 |
inline constexpr T inv_sqrtpi_v = std::numbers::inv_sqrtpi_v<T>; |
| 43 |
template <typename T> |
| 44 |
inline constexpr T ln2_v = std::numbers::ln2_v<T>; |
| 45 |
template <typename T> |
| 46 |
inline constexpr T ln10_v = std::numbers::ln10_v<T>; |
| 47 |
template <typename T> |
| 48 |
inline constexpr T sqrt2_v = std::numbers::sqrt2_v<T>; |
| 49 |
template <typename T> |
| 50 |
inline constexpr T sqrt3_v = std::numbers::sqrt3_v<T>; |
| 51 |
template <typename T> |
| 52 |
inline constexpr T inv_sqrt3_v = std::numbers::inv_sqrt3_v<T>; |
| 53 |
template <typename T> |
| 54 |
inline constexpr T egamma_v = std::numbers::egamma_v<T>; |
| 55 |
template <typename T> |
| 56 |
inline constexpr T phi_v = std::numbers::phi_v<T>; |
| 57 |
|
| 58 |
inline constexpr double e = e_v<double>; |
| 59 |
inline constexpr double log2e = log2e_v<double>; |
| 60 |
inline constexpr double log10e = log10e_v<double>; |
| 61 |
inline constexpr double pi = pi_v<double>; |
| 62 |
inline constexpr double inv_pi = inv_pi_v<double>; |
| 63 |
inline constexpr double inv_sqrtpi = inv_sqrtpi_v<double>; |
| 64 |
inline constexpr double ln2 = ln2_v<double>; |
| 65 |
inline constexpr double ln10 = ln10_v<double>; |
| 66 |
inline constexpr double sqrt2 = sqrt2_v<double>; |
| 67 |
inline constexpr double sqrt3 = sqrt3_v<double>; |
| 68 |
inline constexpr double inv_sqrt3 = inv_sqrt3_v<double>; |
| 69 |
inline constexpr double egamma = egamma_v<double>; |
| 70 |
inline constexpr double phi = phi_v<double>; |
| 71 |
|
| 72 |
#else |
| 73 |
|
| 74 |
#ifdef M_E |
| 75 |
template <typename T, typename std::enable_if<std::is_floating_point<T>::value, bool>::type = true> |
| 76 |
inline constexpr T e_v = static_cast<T>(M_E); |
| 77 |
#else |
| 78 |
template <typename T, typename std::enable_if<std::is_floating_point<T>::value, bool>::type = true> |
| 79 |
inline constexpr T e_v = static_cast<T>(2.71828182845904523536); |
| 80 |
#endif |
| 81 |
|
| 82 |
#ifdef M_LOG2E |
| 83 |
template <typename T, typename std::enable_if<std::is_floating_point<T>::value, bool>::type = true> |
| 84 |
inline constexpr T log2e_v = static_cast<T>(M_LOG2E); |
| 85 |
#else |
| 86 |
template <typename T, typename std::enable_if<std::is_floating_point<T>::value, bool>::type = true> |
| 87 |
inline constexpr T log2e_v = static_cast<T>(1.44269504088896340736); |
| 88 |
#endif |
| 89 |
|
| 90 |
#ifdef M_LOG10E |
| 91 |
template <typename T, typename std::enable_if<std::is_floating_point<T>::value, bool>::type = true> |
| 92 |
inline constexpr T log10e_v = static_cast<T>(M_LOG10E); |
| 93 |
#else |
| 94 |
template <typename T, typename std::enable_if<std::is_floating_point<T>::value, bool>::type = true> |
| 95 |
inline constexpr T log10e_v = static_cast<T>(0.434294481903251827651); |
| 96 |
#endif |
| 97 |
|
| 98 |
#ifdef M_PI |
| 99 |
template <typename T, typename std::enable_if<std::is_floating_point<T>::value, bool>::type = true> |
| 100 |
inline constexpr T pi_v = static_cast<T>(M_PI); |
| 101 |
#else |
| 102 |
template <typename T, typename std::enable_if<std::is_floating_point<T>::value, bool>::type = true> |
| 103 |
inline constexpr T pi_v = static_cast<T>(3.14159265358979323846); |
| 104 |
#endif |
| 105 |
|
| 106 |
#ifdef M_1_PI |
| 107 |
template <typename T, typename std::enable_if<std::is_floating_point<T>::value, bool>::type = true> |
| 108 |
inline constexpr T inv_pi_v = static_cast<T>(M_1_PI); |
| 109 |
#else |
| 110 |
template <typename T, typename std::enable_if<std::is_floating_point<T>::value, bool>::type = true> |
| 111 |
inline constexpr T inv_pi_v = static_cast<T>(0.318309886183790671538); |
| 112 |
#endif |
| 113 |
|
| 114 |
template <typename T, typename std::enable_if<std::is_floating_point<T>::value, bool>::type = true> |
| 115 |
inline constexpr T inv_sqrtpi_v = static_cast<T>(0.564189583547756286948079451560772586); |
| 116 |
|
| 117 |
#ifdef M_LN2 |
| 118 |
template <typename T, typename std::enable_if<std::is_floating_point<T>::value, bool>::type = true> |
| 119 |
inline constexpr T ln2_v = static_cast<T>(M_LN2); |
| 120 |
#else |
| 121 |
template <typename T, typename std::enable_if<std::is_floating_point<T>::value, bool>::type = true> |
| 122 |
inline constexpr T ln2_v = static_cast<T>(0.693147180559945309417); |
| 123 |
#endif |
| 124 |
|
| 125 |
#ifdef M_LN10 |
| 126 |
template <typename T, typename std::enable_if<std::is_floating_point<T>::value, bool>::type = true> |
| 127 |
inline constexpr T ln10_v = static_cast<T>(M_LN10); |
| 128 |
#else |
| 129 |
template <typename T, typename std::enable_if<std::is_floating_point<T>::value, bool>::type = true> |
| 130 |
inline constexpr T ln10_v = static_cast<T>(2.30258509299404568402); |
| 131 |
#endif |
| 132 |
|
| 133 |
#ifdef M_SQRT2 |
| 134 |
template <typename T, typename std::enable_if<std::is_floating_point<T>::value, bool>::type = true> |
| 135 |
inline constexpr T sqrt2_v = static_cast<T>(M_SQRT2); |
| 136 |
#else |
| 137 |
template <typename T, typename std::enable_if<std::is_floating_point<T>::value, bool>::type = true> |
| 138 |
inline constexpr T sqrt2_v = static_cast<T>(1.41421356237309504880); |
| 139 |
#endif |
| 140 |
|
| 141 |
template <typename T, typename std::enable_if<std::is_floating_point<T>::value, bool>::type = true> |
| 142 |
inline constexpr T sqrt3_v = static_cast<T>(1.732050807568877293527446341505872367); |
| 143 |
|
| 144 |
template <typename T, typename std::enable_if<std::is_floating_point<T>::value, bool>::type = true> |
| 145 |
inline constexpr T inv_sqrt3_v = static_cast<T>(0.577350269189625764509148780501957456); |
| 146 |
|
| 147 |
template <typename T, typename std::enable_if<std::is_floating_point<T>::value, bool>::type = true> |
| 148 |
inline constexpr T egamma_v = static_cast<T>(0.577215664901532860606512090082402431); |
| 149 |
|
| 150 |
template <typename T, typename std::enable_if<std::is_floating_point<T>::value, bool>::type = true> |
| 151 |
inline constexpr T phi_v = static_cast<T>(1.618033988749894848204586834365638118); |
| 152 |
|
| 153 |
inline constexpr double e = e_v<double>; |
| 154 |
inline constexpr double log2e = log2e_v<double>; |
| 155 |
inline constexpr double log10e = log10e_v<double>; |
| 156 |
inline constexpr double pi = pi_v<double>; |
| 157 |
inline constexpr double inv_pi = inv_pi_v<double>; |
| 158 |
inline constexpr double inv_sqrtpi = inv_sqrtpi_v<double>; |
| 159 |
inline constexpr double ln2 = ln2_v<double>; |
| 160 |
inline constexpr double ln10 = ln10_v<double>; |
| 161 |
inline constexpr double sqrt2 = sqrt2_v<double>; |
| 162 |
inline constexpr double sqrt3 = sqrt3_v<double>; |
| 163 |
inline constexpr double inv_sqrt3 = inv_sqrt3_v<double>; |
| 164 |
inline constexpr double egamma = egamma_v<double>; |
| 165 |
inline constexpr double phi = phi_v<double>; |
| 166 |
|
| 167 |
#endif |
| 168 |
|
| 169 |
} // namespace numbers |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
} // namespace MPT_INLINE_NS |
| 174 |
} // namespace mpt |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
#endif // MPT_BASE_NUMBRES_HPP |