Numerics library
A guide to Clause 29: where complex numbers, random numbers, valarray, <cmath>, mathematical constants, linear algebra and std::simd live, the recurring kinds of rule consequences in this clause, and the state of libc++ in our tests.
Key points
- Clause 29 collects types and functions built around mathematical objects: complex numbers, random numbers,
valarray,<cmath>, mathematical constants,<linalg>and<simd>.<numeric>algorithms such asaccumulate,reduceandgcdare in the algorithms clause, not here. - When the result of a
<complex>function is mathematically undefined or unrepresentable, the behavior is UB;<cmath>uses C's error reporting mechanism instead, which is not UB. - The algorithms of random number distributions are implementation-defined, so the same seed can give different sequences on different standard libraries; the algorithms of engines are fully specified.
<random>expresses requirements through template parameter names: parameters namedIntType,RealTypeand so on accept only types in specified sets, otherwise the program is ill-formed.- libc++ in our tests does not yet have
<linalg>,<simd>orphilox_engine, and theconstexprextensions to<cmath>are not implemented.
Map of the clause ([numerics.general])#
¶1–¶2 This clause describes components for seminumerical operations, that is, types and functions about mathematical objects themselves, not general algorithms over sequences. Table 125 lists the subclauses:
| Subclause | Contents | Header | Notes on this site |
|---|---|---|---|
| [numeric.requirements] | requirements on the element types of complex and valarray |
— | [numeric.requirements] |
| [cfenv] | the floating-point environment: exception flags, rounding modes | <cfenv> |
[cfenv] |
| [complex.numbers] | complex<T>, transcendental functions, literal suffixes |
<complex> |
[complex.numbers] |
| [rand] | engines, engine adaptors, distributions, random_device, seed_seq |
<random> |
[rand] |
| [numarray] | valarray and the four slice proxies |
<valarray> |
[numarray] |
| [c.math] | <cmath> functions, integer abs, lerp, mathematical special functions |
<cmath>, <cstdlib> |
[c.math] |
| [numbers] | mathematical constants such as std::numbers::pi |
<numbers> |
[numbers] |
| [linalg] | BLAS-style algorithms based on mdspan C++26 |
<linalg> |
[linalg] |
| [simd] | data-parallel types basic_vec, basic_mask C++26 |
<simd> |
[simd] |
After the table there is one more subclause, [numerics.c]: C23's <stdckdint.h>, with the overflow-checked ckd_add, ckd_sub and ckd_mul.
The introduction in ¶2 still mentions generalized numeric algorithms, but accumulate, reduce, inclusive_scan, gcd, midpoint, saturation arithmetic and so on long ago moved to [numeric.ops] and [numeric.sat] in the algorithms clause. Look for them there.
Some facilities are often mistakenly thought to be in this clause:
| Facility | Actual location |
|---|---|
numeric_limits, <cfloat>, <climits> |
the language support library, [support.limits] |
<stdfloat> aliases such as float16_t |
[support.arith.types]; the types themselves are in [basic.extended.fp] |
<bit> (popcount, bit_cast and so on) |
the utilities library, [bit] |
to_chars, from_chars |
text processing, [charconv] |
shuffle, sample, ranges::generate_random |
the algorithms clause, [alg.random.shuffle], [alg.random.sample], [alg.rand.generate] |
mdspan itself |
the containers clause, [views.multidim] |
Recurring kinds of rule consequences in this clause#
The numerics library borrows heavily from mathematical notation and often specifies a consequence directly for mathematical failure. The following patterns recur across the pages.
Results undefined or unrepresentable#
auto z = std::log(std::complex<double>(0, 0));
// <complex>: ln 0 is mathematically undefined; UB per ¶3
double d = std::log(0.0);
// <cmath>: pole error, returns -HUGE_VAL
// and reports via math_errhandling; not UB[complex.numbers.general] ¶3 in [complex.numbers] says that if the result of a complex function is mathematically undefined or outside the representable range of its type, the behavior is undefined. This is stricter than the core language. Per [expr.pre]/4, a built-in arithmetic expression is UB only when the mathematical result is neither representable nor an infinity or NaN the type has, so for IEEE floating-point types only division by zero remains UB among the built-in operations. <complex> has no such exemption. libc++ in our tests actually follows the C Annex G conventions and returns (-inf, 0) for the example above, but the standard does not guarantee it.
<cmath> keeps C's math_errhandling mechanism for reporting domain and range errors (see [c.math]), which is not UB. But integer std::abs(INT_MIN) is still UB, because that is how C specifies abs.
Implementation-defined algorithms#
std::mt19937 eng(42);
std::uniform_int_distribution<int> d(1, 6);
int x = d(eng); // different standard libraries may give different x[rand.dist.general] ¶3 in [rand.dist] says that how each distribution produces the target distribution from uniform random bits is implementation-defined. So with the same engine and the same seed, uniform_int_distribution can give different results on libc++, libstdc++ and the MSVC STL.
Engines are the opposite: the algorithm of mt19937 is fully specified, and [rand.predef] even specifies the value that the 10000th invocation of a default-constructed engine must produce. To reproduce a random sequence across platforms, you can rely only on the engine's output and must write the transformation from bits to the distribution yourself. Which engine default_random_engine is, and where random_device gets its entropy, are also implementation-defined.
Template parameter names carry requirements#
[rand.req.genl] ¶1 says that template parameters in <random> named RealType, IntType, UIntType, URBG, Sseq and Engine automatically carry corresponding requirements. For the first three, an argument outside the allowed set of types makes the program ill-formed. For example, IntType accepts only the standard integer types, extended integer types with width between that of char and long long, and other integer types the implementation chooses to include. char and bool are not standard integer types and can be used only if the implementation includes them; libc++ in our tests rejects uniform_int_distribution<char> with a static_assert.
[linalg] uses the same technique ([linalg.algs.reqs]): parameters named Triangle, DiagonalStorage and ExecutionPolicy each have constraints. When reading these function signatures, the parameter names themselves are part of the requirements.
Mandates, Constraints and Preconditions#
As in other library clauses, when Mandates are not met the program is ill-formed and must be diagnosed; when Constraints are not met the overload drops out of overload resolution with no diagnostic; when Preconditions are not met the behavior is UB ([structure.specifications]). A few spots in this clause deserve attention:
- instantiating a primary template like
std::numbers::pi_v<int>makes the program ill-formed ([math.constants] ¶3); - instantiating
complex<int>has unspecified effects: it is not guaranteed to work, and no diagnostic is required ([numeric.requirements]); - the bounds check of
valarray::operator[]is a hardened precondition C++26; under a hardened implementation, out-of-bounds access is checked and terminates (hardened precondition); <linalg>uses Mandates to check that static extents are compatible and Preconditions to constrain dynamic extents.
Implementation status (Clang 23 + libc++)#
| Facility | Status |
|---|---|
basic <complex> functionality, literals |
implemented; C++26 structured binding support also implemented |
C++23/26 constexpr extensions to <complex> and <cmath> |
not implemented: std::floor and others are rejected in constant expressions |
the classic engines and distributions of <random> |
implemented; default_random_engine is minstd_rand |
philox_engine, ranges::generate_random C++26 |
not implemented |
the new specification of generate_canonical |
not adopted; still the old algorithm, which can return 1.0 (see [rand.util]) |
<valarray> |
implemented, using expression templates |
| mathematical special functions | only hermite; no riemann_zeta, cyl_bessel_j and so on |
<linalg>, <simd> C++26 |
neither header exists |
<stdckdint.h> |
provided by Clang's own C header; the ckd_* are macros, not the function templates the standard requires |
Contents
- 29.1General[numerics.general]
- 29.2Numeric type requirements[numeric.requirements]done
- 29.3The floating-point environment[cfenv]done
- 29.4Complex numbers[complex.numbers]done
- 29.4.1General[complex.numbers.general]
- 29.4.2Header <complex> synopsis[complex.syn]
- 29.4.3Class template complex[complex]
- 29.4.4Member functions[complex.members]
- 29.4.5Member operators[complex.member.ops]
- 29.4.6Non-member operations[complex.ops]
- 29.4.7Value operations[complex.value.ops]
- 29.4.8Transcendentals[complex.transcendentals]
- 29.4.9Tuple interface[complex.tuple]
- 29.4.10Additional overloads[cmplx.over]
- 29.4.11Suffixes for complex number literals[complex.literals]
- 29.5Random number generation[rand]done
- 29.5.1General[rand.general]
- 29.5.2Header <random> synopsis[rand.synopsis]
- 29.5.3Requirements[rand.req]done
- 29.5.3.1General requirements[rand.req.genl]
- 29.5.3.2Seed sequence requirements[rand.req.seedseq]
- 29.5.3.3Uniform random bit generator requirements[rand.req.urng]
- 29.5.3.4Random number engine requirements[rand.req.eng]
- 29.5.3.5Random number engine adaptor requirements[rand.req.adapt]
- 29.5.3.6Random number distribution requirements[rand.req.dist]
- 29.5.4Random number engine class templates[rand.eng]done
- 29.5.5Random number engine adaptor class templates[rand.adapt]
- 29.5.6Engines and engine adaptors with predefined parameters[rand.predef]
- 29.5.7Class random_device[rand.device]done
- 29.5.8Utilities[rand.util]done
- 29.5.9Random number distribution class templates[rand.dist]done
- 29.5.9.1General[rand.dist.general]
- 29.5.9.2Uniform distributions[rand.dist.uni]
- 29.5.9.3Bernoulli distributions[rand.dist.bern]
- 29.5.9.4Poisson distributions[rand.dist.pois]
- 29.5.9.4.1Class template poisson_distribution[rand.dist.pois.poisson]
- 29.5.9.4.2Class template exponential_distribution[rand.dist.pois.exp]
- 29.5.9.4.3Class template gamma_distribution[rand.dist.pois.gamma]
- 29.5.9.4.4Class template weibull_distribution[rand.dist.pois.weibull]
- 29.5.9.4.5Class template extreme_value_distribution[rand.dist.pois.extreme]
- 29.5.9.5Normal distributions[rand.dist.norm]
- 29.5.9.5.1Class template normal_distribution[rand.dist.norm.normal]
- 29.5.9.5.2Class template lognormal_distribution[rand.dist.norm.lognormal]
- 29.5.9.5.3Class template chi_squared_distribution[rand.dist.norm.chisq]
- 29.5.9.5.4Class template cauchy_distribution[rand.dist.norm.cauchy]
- 29.5.9.5.5Class template fisher_f_distribution[rand.dist.norm.f]
- 29.5.9.5.6Class template student_t_distribution[rand.dist.norm.t]
- 29.5.9.6Sampling distributions[rand.dist.samp]
- 29.5.10Low-quality random number generation[c.math.rand]
- 29.6Numeric arrays[numarray]done
- 29.6.1Header <valarray> synopsis[valarray.syn]
- 29.6.2Class template valarray[template.valarray]
- 29.6.2.1Overview[template.valarray.overview]
- 29.6.2.2Constructors[valarray.cons]
- 29.6.2.3Assignment[valarray.assign]
- 29.6.2.4Element access[valarray.access]
- 29.6.2.5Subset operations[valarray.sub]
- 29.6.2.6Unary operators[valarray.unary]
- 29.6.2.7Compound assignment[valarray.cassign]
- 29.6.2.8Range access[valarray.range]
- 29.6.2.9Member functions[valarray.members]
- 29.6.3valarray non-member operations[valarray.nonmembers]
- 29.6.4Class slice[class.slice]
- 29.6.5Class template slice_array[template.slice.array]
- 29.6.6The gslice class[class.gslice]
- 29.6.7Class template gslice_array[template.gslice.array]
- 29.6.8Class template mask_array[template.mask.array]
- 29.6.9Class template indirect_array[template.indirect.array]
- 29.7Mathematical functions for floating-point types[c.math]done
- 29.7.1Header <cmath> synopsis[cmath.syn]
- 29.7.2Absolute values[c.math.abs]
- 29.7.3Three-dimensional hypotenuse[c.math.hypot3]
- 29.7.4Linear interpolation[c.math.lerp]
- 29.7.5Classification / comparison functions[c.math.fpclass]
- 29.7.6Mathematical special functions[sf.cmath]
- 29.7.6.1General[sf.cmath.general]
- 29.7.6.2Associated Laguerre polynomials[sf.cmath.assoc.laguerre]
- 29.7.6.3Associated Legendre functions[sf.cmath.assoc.legendre]
- 29.7.6.4Beta function[sf.cmath.beta]
- 29.7.6.5Complete elliptic integral of the first kind[sf.cmath.comp.ellint.1]
- 29.7.6.6Complete elliptic integral of the second kind[sf.cmath.comp.ellint.2]
- 29.7.6.7Complete elliptic integral of the third kind[sf.cmath.comp.ellint.3]
- 29.7.6.8Regular modified cylindrical Bessel functions[sf.cmath.cyl.bessel.i]
- 29.7.6.9Cylindrical Bessel functions of the first kind[sf.cmath.cyl.bessel.j]
- 29.7.6.10Irregular modified cylindrical Bessel functions[sf.cmath.cyl.bessel.k]
- 29.7.6.11Cylindrical Neumann functions[sf.cmath.cyl.neumann]
- 29.7.6.12Incomplete elliptic integral of the first kind[sf.cmath.ellint.1]
- 29.7.6.13Incomplete elliptic integral of the second kind[sf.cmath.ellint.2]
- 29.7.6.14Incomplete elliptic integral of the third kind[sf.cmath.ellint.3]
- 29.7.6.15Exponential integral[sf.cmath.expint]
- 29.7.6.16Hermite polynomials[sf.cmath.hermite]
- 29.7.6.17Laguerre polynomials[sf.cmath.laguerre]
- 29.7.6.18Legendre polynomials[sf.cmath.legendre]
- 29.7.6.19Riemann zeta function[sf.cmath.riemann.zeta]
- 29.7.6.20Spherical Bessel functions of the first kind[sf.cmath.sph.bessel]
- 29.7.6.21Spherical associated Legendre functions[sf.cmath.sph.legendre]
- 29.7.6.22Spherical Neumann functions[sf.cmath.sph.neumann]
- 29.8Numbers[numbers]done
- 29.9Basic linear algebra algorithms[linalg]draft
- 29.9.1Overview[linalg.overview]
- 29.9.2Header <linalg> synopsis[linalg.syn]
- 29.9.3General[linalg.general]
- 29.9.4Requirements[linalg.reqs]
- 29.9.5Tag classes[linalg.tags]
- 29.9.6Layouts for packed matrix types[linalg.layout.packed]
- 29.9.7Exposition-only helpers[linalg.helpers]
- 29.9.7.1abs-if-needed[linalg.helpers.abs]
- 29.9.7.2conj-if-needed[linalg.helpers.conj]
- 29.9.7.3real-if-needed[linalg.helpers.real]
- 29.9.7.4imag-if-needed[linalg.helpers.imag]
- 29.9.7.5Argument concepts[linalg.helpers.concepts]
- 29.9.7.6Mandates[linalg.helpers.mandates]
- 29.9.7.7Preconditions[linalg.helpers.precond]
- 29.9.8Scaled in-place transformation[linalg.scaled]
- 29.9.9Conjugated in-place transformation[linalg.conj]
- 29.9.10Transpose in-place transformation[linalg.transp]
- 29.9.11Conjugate transpose in-place transform[linalg.conjtransposed]
- 29.9.12Algorithm requirements based on template parameter name[linalg.algs.reqs]
- 29.9.13BLAS 1 algorithms[linalg.algs.blas1]
- 29.9.13.1Complexity[linalg.algs.blas1.complexity]
- 29.9.13.2Givens rotations[linalg.algs.blas1.givens]
- 29.9.13.3Swap matrix or vector elements[linalg.algs.blas1.swap]
- 29.9.13.4Multiply the elements of an object in place by a scalar[linalg.algs.blas1.scal]
- 29.9.13.5Copy elements of one matrix or vector into another[linalg.algs.blas1.copy]
- 29.9.13.6Add vectors or matrices elementwise[linalg.algs.blas1.add]
- 29.9.13.7Dot product of two vectors[linalg.algs.blas1.dot]
- 29.9.13.8Euclidean norm of a vector[linalg.algs.blas1.nrm2]
- 29.9.13.9Sum of absolute values of vector elements[linalg.algs.blas1.asum]
- 29.9.13.10Index of maximum absolute value of vector elements[linalg.algs.blas1.iamax]
- 29.9.13.11Frobenius norm of a matrix[linalg.algs.blas1.matfrobnorm]
- 29.9.13.12One norm of a matrix[linalg.algs.blas1.matonenorm]
- 29.9.13.13Infinity norm of a matrix[linalg.algs.blas1.matinfnorm]
- 29.9.14BLAS 2 algorithms[linalg.algs.blas2]
- 29.9.14.1General matrix-vector product[linalg.algs.blas2.gemv]
- 29.9.14.2Symmetric matrix-vector product[linalg.algs.blas2.symv]
- 29.9.14.3Hermitian matrix-vector product[linalg.algs.blas2.hemv]
- 29.9.14.4Triangular matrix-vector product[linalg.algs.blas2.trmv]
- 29.9.14.5Solve a triangular linear system[linalg.algs.blas2.trsv]
- 29.9.14.6Rank-1 (outer product) update of a matrix[linalg.algs.blas2.rank1]
- 29.9.14.7Symmetric or Hermitian Rank-1 (outer product) update of a matrix[linalg.algs.blas2.symherrank1]
- 29.9.14.8Symmetric and Hermitian rank-2 matrix updates[linalg.algs.blas2.rank2]
- 29.9.15BLAS 3 algorithms[linalg.algs.blas3]
- 29.9.15.1General matrix-matrix product[linalg.algs.blas3.gemm]
- 29.9.15.2Symmetric, Hermitian, and triangular matrix-matrix product[linalg.algs.blas3.xxmm]
- 29.9.15.3In-place triangular matrix-matrix product[linalg.algs.blas3.trmm]
- 29.9.15.4Rank-k update of a symmetric or Hermitian matrix[linalg.algs.blas3.rankk]
- 29.9.15.5Rank-2k update of a symmetric or Hermitian matrix[linalg.algs.blas3.rank2k]
- 29.9.15.6Solve multiple triangular linear systems[linalg.algs.blas3.trsm]
- 29.9.15.7Solve multiple triangular linear systems in-place[linalg.algs.blas3.inplacetrsm]
- 29.10Data-parallel types[simd]draft
- 29.10.1General[simd.general]
- 29.10.2Exposition-only types, variables, and concepts[simd.expos]
- 29.10.3Header <simd> synopsis[simd.syn]
- 29.10.4Type traits[simd.traits]
- 29.10.5Load and store flags[simd.flags]
- 29.10.6Class template simd-iterator[simd.iterator]
- 29.10.7Class template basic_vec[simd.class]
- 29.10.8basic_vec non-member operations[simd.nonmembers]
- 29.10.8.1Binary operators[simd.binary]
- 29.10.8.2Compound assignment[simd.cassign]
- 29.10.8.3Comparison operators[simd.comparison]
- 29.10.8.4Exposition-only conditional operators[simd.cond]
- 29.10.8.5Reductions[simd.reductions]
- 29.10.8.6Load and store functions[simd.loadstore]
- 29.10.8.7Static permute[simd.permute.static]
- 29.10.8.8Dynamic permute[simd.permute.dynamic]
- 29.10.8.9Mask permute[simd.permute.mask]
- 29.10.8.10Memory permute[simd.permute.memory]
- 29.10.8.11Creation[simd.creation]
- 29.10.8.12Algorithms[simd.alg]
- 29.10.8.13Mathematical functions[simd.math]
- 29.10.8.14Bit manipulation[simd.bit]
- 29.10.8.15Complex math[simd.complex.math]
- 29.10.9Class template basic_mask[simd.mask.class]
- 29.10.10basic_mask non-member operations[simd.mask.nonmembers]
- 29.11C compatibility[numerics.c]done