<cmath>
support
Decimal contains overloads for all functions from <cmath>
, and they have the same handling as built-in floating point types.
They are also all constexpr with C++14 unlike the built-in floating point types which require either C++23 or 26.
Additionally, all functions are marked noexcept
.
Basic Operations
Function |
Description |
Absolute Value |
|
Floating Point Absolute Value (Identical to |
|
Floating Point Modulo Operation |
|
Signed Floating Point Modulo |
|
Signed remainder and at least three bits of division result (quotient) |
|
Fused Multiply - Add |
|
Returns maximum value |
|
Returns minimum value |
|
Returns unsigned difference of two values |
|
Generates NANs |
#include <boost/decimal/cmath.hpp>
namespace boost {
namespace decimal {
template <typename DecimalType>
constexpr DecimalType abs(DecimalType x) noexcept;
template <typename DecimalType>
constexpr DecimalType fabs(DecimalType x) noexcept;
template <typename DecimalType>
constexpr DecimalType abs(DecimalType x) noexcept;
template <typename DecimalType>
constexpr DecimalType fmod(DecimalType x, DecimalType y) noexcept;
template <typename DecimalType>
constexpr DecimalType remainder(DecimalType x, DecimalType y) noexcept;
template <typename DecimalType>
constexpr DecimalType remquo(DecimalType x, DecimalType y, int* quo) noexcept;
template <typename DecimalType>
constexpr DecimalType fma(DecimalType x, DecimalType y, DecimalType z) noexcept;
template <typename DecimalType>
constexpr DecimalType fmax(DecimalType x, DecimalType y, int* quo) noexcept;
template <typename DecimalType>
constexpr DecimalType fmin(DecimalType x, DecimalType y, int* quo) noexcept;
template <typename DecimalType>
constexpr DecimalType fdim(DecimalType x, DecimalType y, int* quo) noexcept;
constexpr decimal32_t nand32(const char* arg) noexcept;
constexpr decimal64_t nand64(const char* arg) noexcept;
constexpr decimal128_t nand128(const char* arg) noexcept;
} // namespace decimal
} // namespace boost
Exponential Functions
Function |
Description |
Returns |
|
Returns |
|
Returns ( |
|
Returns the natural logarithm of the value (base |
|
Returns the log base 10 of the value |
|
Returns the log base 2 of the value |
|
Returns ln(1 + x) |
#include <boost/decimal/cmath.hpp>
namespace boost {
namespace decimal {
template <typename DecimalType>
constexpr DecimalType exp(DecimalType x) noexcept;
template <typename DecimalType>
constexpr DecimalType exp2(DecimalType x) noexcept;
template <typename DecimalType>
constexpr DecimalType expm1(DecimalType x) noexcept;
template <typename DecimalType>
constexpr DecimalType log(DecimalType x) noexcept;
template <typename DecimalType>
constexpr DecimalType log10(DecimalType x) noexcept;
template <typename DecimalType>
constexpr DecimalType log2(DecimalType x) noexcept;
template <typename DecimalType>
constexpr DecimalType log1p(DecimalType x) noexcept;
} // namespace decimal
} // namespace boost
Power Functions
Function |
Description |
Returns x^y |
|
Returns the square root of the value |
|
Returns the cube root of the value |
|
Returns sqrt(x^2 + y^2) or sqrt(x^2 + y^2 + z^2) depending on number of arguments |
#include <boost/decimal/cmath.hpp>
namespace boost {
namespace decimal {
template <typename DecimalType>
constexpr DecimalType pow(DecimalType x, DecimalType y) noexcept;
template <typename DecimalType>
constexpr DecimalType sqrt(DecimalType x) noexcept;
template <typename DecimalType>
constexpr DecimalType cbrt(DecimalType x) noexcept;
template <typename DecimalType>
constexpr DecimalType hypot(DecimalType x, DecimalType y) noexcept;
template <typename DecimalType>
constexpr DecimalType hypot(DecimalType x, DecimalType y, DecimalType z) noexcept;
} // namespace decimal
} // namespace boost
Trigonometric Functions
Function |
Description |
Computes sin(x) |
|
Computes cos(x) |
|
Computes tan(x) |
|
Computes arcsin(x) |
|
Computes arccos(x) |
|
Computes arctan(x) |
|
Computes arctan(x) using signs as quadrants |
#include <boost/decimal/cmath.hpp>
namespace boost {
namespace decimal {
template <typename DecimalType>
constexpr DecimalType sin(DecimalType x) noexcept;
template <typename DecimalType>
constexpr DecimalType cos(DecimalType x) noexcept;
template <typename DecimalType>
constexpr DecimalType tan(DecimalType x) noexcept;
template <typename DecimalType>
constexpr DecimalType asin(DecimalType x) noexcept;
template <typename DecimalType>
constexpr DecimalType acos(DecimalType x) noexcept;
template <typename DecimalType>
constexpr DecimalType atan(DecimalType x) noexcept;
template <typename DecimalType>
constexpr DecimalType atan2(DecimalType x, DecimalType y) noexcept;
} // namespace decimal
} // namespace boost
Hyperbolic Functions
Function |
Description |
Computes hyperbolic sin sinh(x) |
|
Computes hyperbolic cos cosh(x) |
|
Computes hyperbolic tan tanh(x) |
|
Computes hyperbolic arcsin asinh(x) |
|
Computes hyperbolic arccos acosh(x) |
|
Computes hyperbolic arctan atanh(x) |
#include <boost/decimal/cmath.hpp>
namespace boost {
namespace decimal {
template <typename DecimalType>
constexpr DecimalType sinh(DecimalType x) noexcept;
template <typename DecimalType>
constexpr DecimalType cosh(DecimalType x) noexcept;
template <typename DecimalType>
constexpr DecimalType tanh(DecimalType x) noexcept;
template <typename DecimalType>
constexpr DecimalType asinh(DecimalType x) noexcept;
template <typename DecimalType>
constexpr DecimalType acosh(DecimalType x) noexcept;
template <typename DecimalType>
constexpr DecimalType atanh(DecimalType x) noexcept;
} // namespace decimal
} // namespace boost
Error and Gamma Functions
Function |
Description |
Computes the error function erf(x) |
|
Computes the complementary error function erfc(x) |
|
Computes the gamma function gamma(x) |
|
Computes the natural log of the gamma function ln(gamma(x)) |
#include <boost/decimal/cmath.hpp>
namespace boost {
namespace decimal {
template <typename DecimalType>
constexpr DecimalType erf(DecimalType x) noexcept;
template <typename DecimalType>
constexpr DecimalType erfc(DecimalType x) noexcept;
template <typename DecimalType>
constexpr DecimalType tgamma(DecimalType x) noexcept;
template <typename DecimalType>
constexpr DecimalType lgamma(DecimalType x) noexcept;
} // namespace decimal
} // namespace boost
Nearest integer floating point operations
Function |
Description |
Computes the nearest integer not less than the given value |
|
Computes the nearest integer not greater than the given value |
|
Computes the nearest integer not greater in magnitude than the given value |
|
Computes the nearest integer represented in same decimal format rounding away from zero |
|
Computes the nearest |
|
Computes the nearest |
|
Computest the nearest integer represented in same decimal format subject to current fenv rounding mode |
|
Computest the nearest |
|
Computest the nearest |
#include <boost/decimal/cmath.hpp>
namespace boost {
namespace decimal {
template <typename DecimalType>
constexpr DecimalType ceil(DecimalType x) noexcept;
template <typename DecimalType>
constexpr DecimalType floor(DecimalType x) noexcept;
template <typename DecimalType>
constexpr DecimalType trunc(DecimalType x) noexcept;
template <typename DecimalType>
constexpr DecimalType round(DecimalType x) noexcept;
template <typename DecimalType>
constexpr long lround(DecimalType x) noexcept;
template <typename DecimalType>
constexpr long long llround(DecimalType x) noexcept;
template <typename DecimalType>
constexpr DecimalType nearbyint(DecimalType x) noexcept;
template <typename DecimalType>
constexpr DecimalType rint(DecimalType x) noexcept;
template <typename DecimalType>
constexpr long lrint(DecimalType x) noexcept;
template <typename DecimalType>
constexpr long long llrint(DecimalType x) noexcept;
} // namespace decimal
} // namespace boost
Floating point manipulation functions
Function |
Description |
Returns significand and base 2 exponent |
|
Multiplies a number by 2^x |
|
Decomposes a number into integer and fractional parts |
|
Multiplies a decimal number by |
|
Multiplies a decimal number by |
|
Extracts |
|
Extracts Decimal Type exponent of a number |
|
Next representable decimal type value |
|
Next representable decimal type value towards a |
|
Copies the sign of a decimal value |
#include <boost/decimal/cmath.hpp>
namespace boost {
namespace decimal {
template <typename DecimalType>
constexpr DecimalType frexp(DecimalType x, int* exp) noexcept;
template <typename DecimalType>
constexpr DecimalType ldexp(DecimalType x, int exp) noexcept;
template <typename DecimalType>
constexpr DecimalType modf(DecimalType x, DecimalType* iptr) noexcept;
template <typename DecimalType>
constexpr DecimalType scalbn(DecimalType x, int exp) noexcept;
template <typename DecimalType>
constexpr DecimalType scalbln(DecimalType x, long exp) noexcept;
template <typename DecimalType>
constexpr int ilogb(DecimalType x) noexcept;
template <typename DecimalType>
constexpr DecimalType logb(DecimalType x) noexcept;
template <typename DecimalType>
constexpr DecimalType nextafter(DecimalType from, DecimalType to) noexcept;
template <typename DecimalType>
constexpr DecimalType nexttoward(DecimalType from, long double to) noexcept;
template <typename DecimalType>
constexpr DecimalType copysign(DecimalType mag, DecimalType sgn) noexcept;
} // namespace decimal
} // namespace boost
Classification and comparison
Function |
Description |
Categorizes a value |
|
Checks if a given value is finite |
|
Checks if a given value is infinite |
|
Checks if a given value is a NAN |
|
Checks if a given value is normal |
|
Checks the sign of a value |
|
Checks if the LHS (Left-hand side or first parameter) is greater than the RHS (Right-hand side or second parameter) |
|
Checks if the LHS is greater than or equal to the RHS |
|
Checks if the LHS is less than the RHS |
|
Checks if the LHS is less than or equal to the RHS |
|
Checks if the LHS and RHS are unordered (i.e. if one or both are NAN) |
#include <boost/decimal/cmath.hpp>
namespace boost {
namespace decimal {
template <typename DecimalType>
constexpr int fpclassify(DecimalType x) noexcept;
template <typename DecimalType>
constexpr bool isfinite(DecimalType x) noexcept;
template <typename DecimalType>
constexpr bool isinf(DecimalType x) noexcept;
template <typename DecimalType>
constexpr bool isnan(DecimalType x) noexcept;
template <typename DecimalType>
constexpr bool isnormal(DecimalType x) noexcept;
template <typename DecimalType>
constexpr bool signbit(DecimalType x) noexcept;
template <typename DecimalType>
constexpr bool isgreater(DecimalType x, DecimalType y) noexcept;
template <typename DecimalType>
constexpr bool isgreaterequal(DecimalType x, DecimalType y) noexcept;
template <typename DecimalType>
constexpr bool isless(DecimalType x, DecimalType y) noexcept;
template <typename DecimalType>
constexpr bool islessgreater(DecimalType x, DecimalType y) noexcept;
template <typename DecimalType>
constexpr bool isunordered(DecimalType x, DecimalType y) noexcept;
} // namespace decimal
} // namespace boost
C++17 Mathematical Special Functions
This section does not currently contain all functions specified by C++17, but it does contain the ones that are more commonly useful like the Beta Function. |
The following functions have been implemented for decimal types:
Function |
Description |
Associated Laguerre Polynomials |
|
Associated Legendre Polynomials |
|
Calculates the beta function |
|
Calculates the complete elliptic integrals of the first kind |
|
Calculates the complete elliptic integrals of the second kind |
|
Calculates the incomplete elliptic integrals of the first kind |
|
Calculates the incomplete elliptic integrals of the second kind |
|
Hermite Polynomials |
|
Laguerre Polynomials |
|
Legendre Polynomials |
|
Computes the Riemann Zeta function |
#include <boost/decimal/cmath.hpp>
namespace boost {
namespace decimal {
template <typename DecimalType>
constexpr DecimalType assoc_laguerre(unsigned int n, unsigned int m, DecimalType x) noexcept;
template <typename DecimalType>
constexpr DecimalType assoc_legendre(unsigned int n, unsigned int m, DecimalType x) noexcept;
template <typename DecimalType>
constexpr DecimalType beta(DecimalType x, DecimalType y) noexcept;
template <typename DecimalType>
constexpr auto comp_ellint_1(DecimalType k) noexcept;
template <typename DecimalType>
constexpr auto comp_ellint_2(DecimalType k) noexcept;
template <typename DecimalType>
constexpr auto ellint_1(DecimalType k, DecimalType phi) noexcept;
template <typename DecimalType>
constexpr auto ellint_2(DecimalType k, DecimalType phi) noexcept;
template <typename DecimalType>
constexpr DecimalType hermite(unsigned int n, DecimalType x) noexcept;
template <typename DecimalType>
constexpr DecimalType laguerre(unsigned int n, DecimalType x) noexcept;
template <typename DecimalType>
constexpr DecimalType legendre(unsigned int n, DecimalType x) noexcept;
template <typename DecimalType, typename IntegralType>
constexpr DecimalType riemann_zeta(IntegralType n) noexcept;
} //namespace decimal
} //namespace boost
Non-standard Functions
The following are convenience functions, or are prescribed in IEEE 754-2019 as required for decimal floating point types.
issignaling
template <typename Decimal>
constexpr bool issignaling(Decimal x) noexcept;
Effects: If x is an sNaN returns true, otherwise returns false.
samequantum
#include <boost/decimal/cmath.hpp>
namespace boost {
namespace decimal {
template <typename Decimal>
constexpr bool samequantum(Decimal x, Decimal y) noexcept;
constexpr bool samequantumd32(decimal32_t x, decimal32_t y) noexcept;
constexpr bool samequantumd64(decimal64_t x, decimal64_t y) noexcept;
constexpr bool samequantumd128(decimal128_t x, decimal128_t y) noexcept;
} // namespace decimal
} // namepsace boost
Effects: Determines if the quantum (unbiased) exponents of x and y are the same.
If both x and y are NaN, or infinity, they have the same quantum exponents.
If exactly one operand is infinity or exactly one operand is NaN, they do not have the same quantum exponents.
quantexp
#include <boost/decimal/cmath.hpp>
namespace boost {
namespace decimal {
template <typename Decimal>
constexpr int quantexp(Decimal x) noexcept;
constexpr int quantexp32(decimal32_t x) noexcept;
constexpr int quantexp64(decimal64_t x) noexcept;
constexpr int quantexp128(decimal128_t x) noexcept;
} // namespace decimal
} // namepsace boost
Effects: if x is finite, returns its quantum exponent.
Otherwise, INT_MIN
is returned.
quantized
#include <boost/decimal/cmath.hpp>
namespace boost {
namespace decimal {
template <typename Decimal>
constexpr Decimal quantized(Decimal x, Decimal y) noexcept;
constexpr decimal32_t quantized32(decimal32_t x, decimal32_t y) noexcept;
constexpr decimal64_t quantized64(decimal64_t x, decimal64_t y) noexcept;
constexpr decimal128_t quantized128(decimal128_t x, decimal128_t y) noexcept;
} // namespace decimal
} // namepsace boost
Returns: a number that is equal in value (except for any rounding) and sign to x, and which has an exponent set to be equal to the exponent of y.
If the exponent is being increased, the value is correctly rounded according to the current rounding mode;
If the result does not have the same value as x, the "inexact" floating-point exception is raised.
If the exponent is being decreased and the significand of the result has more digits than the type would allow, the "invalid" floating-point exception is raised and the result is sNaN.
If one or both operands are NaN the result is sNaN.
Otherwise, if only one operand is infinity, the "invalid" floating-point exception is raised and the result is sNaN.
If both operands are infinity, the result is infinity, with the same sign as x.
The quantize functions do not signal underflow.
frexp10
#include <boost/decimal/cmath.hpp>
namespace boost {
namespace decimal {
template <typename Decimal>
constexpr auto frexp10(Decimal num, int* expptr) noexcept;
} // namespace decimal
} // namepsace boost
This function is very similar to frexp, but returns the significand and an integral power of 10 since the FLT_RADIX
of this type is 10.
The significand is normalized to the number of digits of precision the type has (e.g. for decimal32_t it is [1'000'000, 9'999'999]).
normalize
#include <boost/decimal/cmath.hpp>
namespace boost {
namespace decimal {
template <typename Decimal>
constexpr Decimal normalize(Decimal val) noexcept;
} // namespace decimal
} // namepsace boost
Similar to the frexp10
function above, but rather than returning the normalized significand, and exponent of the Decimal number, it returns a normalized number.
This removes the effects of cohorts on the IEEE 754 compliant types.
This function has NO effect on fast types since they are always normalized internally.
rescale
#include <boost/decimal/cmath.hpp>
namespace boost {
namespace decimal {
template <typename Decimal>
constexpr Decimal rescale(Decimal val, int precision = 0);
} // namespace decimal
} // namespace boost
The function returns the decimal type with number of fractional digits equal to the value of precision.
rescale
is similar to trunc, and with the default precision argument of 0 it is identical.