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_v

\(e\)

Base of the natural logarithm

log2e_v

\(\log_{2}{e}\)

Base-2 logarithm of e

log10e_v

\(\log_{10}{e}\)

Base-10 logarithm of e

pi_v

\(\pi\)

Ratio of a circle’s circumference to its diameter

inv_pi_v

\(\frac{1}{\pi}\)

Inverse of pi

inv_sqrtpi_v

\(\frac{1}{\sqrt{\pi}}\)

Inverse of the square root of pi

ln2_v

\(\ln{2}\)

Natural logarithm of 2

ln10_v

\(\ln{10}\)

Natural logarithm of 10

sqrt2_v

\(\sqrt{2}\)

Square root of 2

sqrt3_v

\(\sqrt{3}\)

Square root of 3

inv_sqrt3_v

\(\frac{1}{\sqrt{3}}\)

Inverse of the square root of 3

egamma_v

\(\gamma\)

Euler-Mascheroni constant

phi_v

\(\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::pi is equivalent to Kokkos::numbers::pi_v<double>

  • Kokkos::numbers::e is equivalent to Kokkos::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);
}

See also