Home > Mobile >  Why does std::poisson_distribution hang when passed a very large mean?
Why does std::poisson_distribution hang when passed a very large mean?

Time:12-18

For example, the following code hangs, using my setup with a recent version of g and GNU libraries:

#include <random>
#include <cstdio>

std::default_random_engine rng;

int main(){
    std::poisson_distribution<long> mine(34387423874230847234.0);
    std::printf("%ld\n", mine(rng));
}

Try it online

The description for the min and max functions here seems to suggest that it will clamp the output to the maximum possible value of the type parameter, in this case long. But clearly that's not happening. Is this expected behavior?

Edit: When I link against LLVM libc , the poisson distribution always returns LLONG_MAX, which is more what I'd expect. Is this a GNU libstdc bug?

CodePudding user response:

We just discussed that libstdc normalizes the part of the probability distribution that fits in the output type (so the relative probabilities are correct) whereas libc clamps it (so that the mean is as correct as possible). The former approach has greatly increased expected runtime in the case where the parameter is uselessly large, but I would hesitate to call that a bug.

The min and max member functions describe what can happen, and are accurate: they do not imply either mechanism for making that happen.

  • Related