[LLVMbugs] [Bug 22805] New: std::piecewise_constant_distribution calculates wrong densities

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Thu Mar 5 08:06:51 PST 2015


http://llvm.org/bugs/show_bug.cgi?id=22805

            Bug ID: 22805
           Summary: std::piecewise_constant_distribution calculates wrong
                    densities
           Product: libc++
           Version: unspecified
          Hardware: All
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: All Bugs
          Assignee: unassignedclangbugs at nondot.org
          Reporter: gnuetzi at gmail.com
                CC: llvmbugs at cs.uiuc.edu, mclow.lists at gmail.com
    Classification: Unclassified

It seems that ``std::piecewise_constant_distribution`` computes the densities
wrongly , at least with GCC and its standard library.

According to
http://www.cplusplus.com/reference/random/piecewise_constant_distribution/:
The densities should be computed as:
![enter image description here][1]

Checking this manually reveals the bug!

This can be seen here: http://coliru.stacked-crooked.com/a/ca171bf600b5148f

The source code related to this is found in
`/usr/include/c++/4.8/bits/random.tcc` (on linux) and the extract of the
initialization function `_M_initialize` called by the constructor shows that
there is something incorrect here:



    const double __sum = std::accumulate(_M_den.begin(),
                           _M_den.end(), 0.0);

          __detail::__normalize(_M_den.begin(), _M_den.end(), _M_den.begin(),
                    __sum);  <----- WRONG

          // THIS is not the cummulative distribution (since the above
normalization does not give the probability of the intervalls!)
          _M_cp.reserve(_M_den.size());
          std::partial_sum(_M_den.begin(), _M_den.end(),
                   std::back_inserter(_M_cp));


          // Make sure the last cumulative probability is one.
          _M_cp[_M_cp.size() - 1] = 1.0;



          // Dividing here by the interval length is WRONG!!!

          for (size_t __k = 0; __k < _M_den.size(); ++__k)
               _M_den[__k] /= _M_int[__k + 1] - _M_int[__k];




  [1]: http://i.stack.imgur.com/LDthp.gif

-- 
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/20150305/af3cd2f3/attachment.html>


More information about the llvm-bugs mailing list