llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Small issue in constructor of `std::discrete_distribution`

Open zhqrbitee opened this issue 4 years ago • 1 comments

Hi I happens to find a potential small issue (or at least something I don't understand) when I read through libcxx/include/__random/discrete_distribution.h:

template<class _IntType>
template<class _UnaryOperation>
discrete_distribution<_IntType>::param_type::param_type(size_t __nw,
                                                        double __xmin,
                                                        double __xmax,
                                                        _UnaryOperation __fw)
{
    if (__nw > 1)
    {
        __p_.reserve(__nw - 1);
        double __d = (__xmax - __xmin) / __nw;
        double __d2 = __d / 2;
        for (size_t __k = 0; __k < __nw; ++__k)
            __p_.push_back(__fw(__xmin + __k * __d + __d2));
        __init();
    }
}

Here we first call __p_.reserve(__nw - 1) to reserve size of __nw - 1 for __p_, but later we loop for __nw times to push values into __p_.
It doesn't effect the functionality, but shouldn't we just reserve __nw instead of __nw - 1?

zhqrbitee avatar Nov 05 '22 19:11 zhqrbitee

Ideally it should reserve __nw, and if the __nw is 0 then then it should be assigned to 1.

Long5hot avatar Apr 28 '23 19:04 Long5hot