Mathematical constants¶
Defined in header <Kokkos_MathematicalConstants.hpp>
which is included from <Kokkos_Core.hpp>
Attention
Recommendation: Since Kokkos 5.X requires C++20, users are encouraged to
use the standard library constants (std::numbers::*) directly. The
Kokkos::numbers namespace is maintained for backward compatibility and
is implemented via using-declarations
of the standard library constants.
Usage¶
auto const x = Kokkos::numbers::pi_v<float>;
auto const y = Kokkos::numbers::sqrt2_v<float>;
Provides access to the mathematical constants defined in the C++20
<numbers> header in the Standard Library.
All constants are defined in the Kokkos::numbers:: namespace.
Variable Templates¶
The following are variable templates defined for standard floating-point types
(float, double, long double).
Template name |
Math symbol |
Description |
|---|---|---|
|
\(e\) |
Base of the natural logarithm |
|
\(\log_{2}{e}\) |
Base-2 logarithm of e |
|
\(\log_{10}{e}\) |
Base-10 logarithm of e |
|
\(\pi\) |
Ratio of a circle’s circumference to its diameter |
|
\(\frac{1}{\pi}\) |
Inverse of pi |
|
\(\frac{1}{\sqrt{\pi}}\) |
Inverse of the square root of pi |
|
\(\ln{2}\) |
Natural logarithm of 2 |
|
\(\ln{10}\) |
Natural logarithm of 10 |
|
\(\sqrt{2}\) |
Square root of 2 |
|
\(\sqrt{3}\) |
Square root of 3 |
|
\(\frac{1}{\sqrt{3}}\) |
Inverse of the square root of 3 |
|
\(\gamma\) |
Euler-Mascheroni constant |
|
\(\varphi\) |
Golden ratio constant \(\frac{1+\sqrt{5}}{2}\) |
Convenience Constants (double)¶
For each variable template listed above, Kokkos provides an inline constexpr
double constant without the _v suffix. These are shorthand for the
double specialization.
Kokkos::numbers::piis equivalent toKokkos::numbers::pi_v<double>Kokkos::numbers::eis equivalent toKokkos::numbers::e_v<double>
Notes¶
Important
Portability: Passing mathematical constants by reference or taking their address in device code is not supported by some toolchains and hence not portable. (See known issues)
Note
Quadruple Precision: Support for quadruple precision floating-point
__float128 can be enabled via -DKokkos_ENABLE_LIBQUADMATH=ON.
Example¶
KOKKOS_FUNCTION void example() {
// Preferred C++20 usage
constexpr auto pi_f = std::numbers::pi_v<float>;
// Kokkos namespace usage (backward compatibility)
constexpr auto pi = Kokkos::numbers::pi_v<float>;
auto const x = Kokkos::sin(pi_f / 6);
}