<html>
<head>
<base href="https://bugs.llvm.org/">
</head>
<body><table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Bug ID</th>
<td><a class="bz_bug_link
bz_status_NEW "
title="NEW - uniform_real_distribution can't generate the full range of finite floating point numbers"
href="https://bugs.llvm.org/show_bug.cgi?id=39344">39344</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>uniform_real_distribution can't generate the full range of finite floating point numbers
</td>
</tr>
<tr>
<th>Product</th>
<td>libc++
</td>
</tr>
<tr>
<th>Version</th>
<td>unspecified
</td>
</tr>
<tr>
<th>Hardware</th>
<td>All
</td>
</tr>
<tr>
<th>OS</th>
<td>All
</td>
</tr>
<tr>
<th>Status</th>
<td>NEW
</td>
</tr>
<tr>
<th>Severity</th>
<td>enhancement
</td>
</tr>
<tr>
<th>Priority</th>
<td>P
</td>
</tr>
<tr>
<th>Component</th>
<td>All Bugs
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedclangbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>fergus.henderson@gmail.com
</td>
</tr>
<tr>
<th>CC</th>
<td>llvm-bugs@lists.llvm.org, mclow.lists@gmail.com
</td>
</tr></table>
<p>
<div>
<pre>Created <span class=""><a href="attachment.cgi?id=21015" name="attach_21015" title="Example program demonstrating the problem.">attachment 21015</a> <a href="attachment.cgi?id=21015&action=edit" title="Example program demonstrating the problem.">[details]</a></span>
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 libc++ code for this function computes (high - low) 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.
Expected behaviour: program runs successfully with no output.
Observed behaviour: program fails with an assertion failure.
The specific code in the libc++ implementation that is at fault is this
function definition:
template<class _RealType>
template<class _URNG>
inline
typename uniform_real_distribution<_RealType>::result_type
uniform_real_distribution<_RealType>::operator()(_URNG& __g, const param_type&
__p)
{
return (__p.b() - __p.a())
* _VSTD::generate_canonical<_RealType,
numeric_limits<_RealType>::digits>(__g)
+ __p.a();
}
Note: a similar bug is also present in libstdc++ -- see
<a href="https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87527">https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87527</a></pre>
</div>
</p>
<hr>
<span>You are receiving this mail because:</span>
<ul>
<li>You are on the CC list for the bug.</li>
</ul>
</body>
</html>