GCC Newspaper
JULY 29, 2026
Date
/
Architectures
Components
Topics
News & Policy
libstdc++

libstdc++: Piecewise distributions now use result_type for generation.

Piecewise distributions in libstdc++ now generate values matching the template's result_type instead of always using double.

This commit updates libstdc++‘s piecewise distributions to generate values consistent with their template parameter _RealType, rather than always defaulting to double. This change addresses issues with precision and LWG4052 regarding input type requirements. Internal computations now also use _RealType for better precision, though double is retained for storage to maintain ABI compatibility unless _GLIBCXX_USE_RESULT_TYPE_FOR_PIECEWISE_DENSITIES is defined. New helper functions __generate_one reduce code duplication.

In Details

The piecewise_linear_distribution and piecewise_constant_distribution in libstdc++ previously had a fixed implementation detail where generated values were always computed as double before being cast to the distribution's _RealType. This commit corrects this by ensuring the generation process respects _RealType from the outset. It also applies this to input conversions from iterators or functors, aligning with LWG4052. Internal calculations now use a _CalcType (which defaults to _RealType) instead of double, potentially reducing precision for float instantiations but preserv…

For Context
piecewise distributions
Classes in C++'s <random> header that define probability distributions over a finite number of intervals. piecewise_linear_distribution and piecewise_constant_distribution are specific types that use linear or constant density functions, respectively, within each interval.
_RealType
A template parameter for distribution classes in libstdc++, representing the underlying floating-point type (e.g., float, double, long double) that the distribution's values are ultimately expected to be of.
LWG4052
A specific issue reported to the C++ Library Working Group (LWG) regarding bogus requirements for piecewise_linear_distribution, which this commit aims to resolve.
ABI compatibility
Application Binary Interface compatibility. Refers to the requirement that code compiled with one version of a library or compiler should still be linkable and interoperable with code compiled by other versions, often concerning data layout and function call conventions.
Filed Under: libstdc++randomnumeric