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

libstdc++: Fix weight computation in piecewise_linear_distribution constructor.

Corrects weight calculation for `piecewise_linear_distribution` to match the standard.

The constructor for piecewise_linear_distribution(nw, xmin, xmax, fw) in libstdc++ incorrectly computed weights. Instead of using __fw(_M_int[k]) as required by the standard, it used __fw(_M_int[k] + __delta), effectively accessing __fw(_M_int[k + 1]). This commit corrects the weight computation to align with the C++ standard. A new test case and documentation updates are included.

In Details

The piecewise_linear_distribution constructor param_type::param_type(size_t nw, _RealType xmin, _RealType xmax, const piecewise_linear_distribution<_RealType>& fw) had an off-by-one error in accessing the weights from the fw parameter. This PR corrects it to use __fw._M_int[k] instead of __fw._M_int[k+1] for the k-th weight. A temporary macro _GLIBCXX_USE_OLD_PIECEWISE_DISTRIBUTIONS is provided to revert to the old behavior.

For Context
piecewise_linear_distribution
A C++ standard library distribution that generates random numbers according to a piecewise linear probability density function. It's defined in the <random> header.
libstdc++
The GNU Standard C++ Library, the C++ standard library implementation used by GCC.
Weight computation
In the context of piecewise_linear_distribution, this refers to how the discrete weights are calculated to approximate the piecewise linear probability density function.
Filed Under: libstdc++randomcppbugfix