[llvm-bugs] [Bug 39344] New: uniform_real_distribution can't generate the full range of finite floating point numbers

via llvm-bugs llvm-bugs at lists.llvm.org
Thu Oct 18 10:16:56 PDT 2018


https://bugs.llvm.org/show_bug.cgi?id=39344

            Bug ID: 39344
           Summary: uniform_real_distribution can't generate the full
                    range of finite floating point numbers
           Product: libc++
           Version: unspecified
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: All Bugs
          Assignee: unassignedclangbugs at nondot.org
          Reporter: fergus.henderson at gmail.com
                CC: llvm-bugs at lists.llvm.org, mclow.lists at gmail.com

Created attachment 21015
  --> https://bugs.llvm.org/attachment.cgi?id=21015&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 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
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87527

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20181018/517269d2/attachment.html>


More information about the llvm-bugs mailing list