[Bug libstdc++/87527] New: uniform_real_distribution can't generate the full range of finite floating point numbers
fergus.henderson at gmail dot com
gcc-bugzilla@gcc.gnu.org
Fri Oct 5 12:33:00 GMT 2018
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87527
Bug ID: 87527
Summary: uniform_real_distribution can't generate the full
range of finite floating point numbers
Product: gcc
Version: unknown
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: libstdc++
Assignee: unassigned at gcc dot gnu.org
Reporter: fergus.henderson at gmail dot com
Target Milestone: ---
Created attachment 44795
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=44795&action=edit
Example program demonstrating the problem.
std::uniform_real_distribution doesn't work if you try to use it to cover the
whole range of available finite floating point numbers:
...
double low = std::numeric_limits<double>::lowest();
double high = std::numeric_limits<double>::max();
std::uniform_real_distribution<> distribution(low, high);
... distribution(generator) ...
In that case, rather than returning a random finite float, it always returns
"inf".
The code in include/bits/random.h line 1862
https://github.com/gcc-mirror/gcc/blob/140696c847da5f27f6b8b6f321c426c932dd1592/libstdc%2B%2B-v3/include/bits/random.h#L1862
computes
__p.b() - __p.a()
which in this case ends up being
std::numeric_limits<double>::max() - std::numeric_limits<double>::lowest()
and that expression overflows to "inf", which then ends up propagating to the
return value from std::uniform_real_distribution<>::operator().
See the attached example program uniform_real.cc for a test case.
g++ -Wall uniform_real.cc
./a.out
Expected behaviour: program runs successfully with no output.
Observed behaviour:
a.out: uniform_real.cc:13: int main(): Assertion `x >= low && x < high' failed.
Observed with: gcc version 7.3.0 (Debian 7.3.0-5)
and libstdc++.so.6.0.25
More information about the Gcc-bugs
mailing list