public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/55047] New: operator() in std::exponential_distribution may call log(0)
@ 2012-10-24  2:25 hyounes at google dot com
  2012-10-24  9:44 ` [Bug libstdc++/55047] " paolo.carlini at oracle dot com
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: hyounes at google dot com @ 2012-10-24  2:25 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55047

             Bug #: 55047
           Summary: operator() in std::exponential_distribution may call
                    log(0)
    Classification: Unclassified
           Product: gcc
           Version: 4.6.3
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: hyounes@google.com


The implementation of operator() for std::exponential_distribution is:

      template<typename _UniformRandomNumberGenerator>
    result_type
    operator()(_UniformRandomNumberGenerator& __urng,
           const param_type& __p)
    {
      __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
        __aurng(__urng);
      return -std::log(__aurng()) / __p.lambda();
    }

If I understand this correctly, __aurng() returns a value in [0,1).  This
leaves the possibility of computing -log(0)/lambda, which I expect to be Inf. 
On the other hand, -log(1)/lambda will never occur, so the resulting value can
never be 0.  There are two problems with this implementation:

1. The actual range (0,Inf) U Inf is not consistent with the reported range of
[0,Inf) as computed by the min() and max() member functions.

2. -log(U01)/lambda is not the mathematically correct form for the inverse
transformation for the exponential distribution.  -log(1 - U01)/lambda is the
correct form.  This form also gives you the correct range of [0,Inf).  It is an
incorrect optimization to change 1-U01 to just U01 when U01 is [0,1).  It is
only correct if U01 is [0,1] or (0,1), but I do not believe that to be the case
here.

I believe the correct implementation should have the following return
statement:

  return -std::log(result_type(1) - __aurng()) / __p.lambda();

The same problem appears in several other distributions.  For example,
std::weibull_distribution.


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2012-11-01 21:11 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-10-24  2:25 [Bug libstdc++/55047] New: operator() in std::exponential_distribution may call log(0) hyounes at google dot com
2012-10-24  9:44 ` [Bug libstdc++/55047] " paolo.carlini at oracle dot com
2012-10-24 12:20 ` paolo at gcc dot gnu.org
2012-10-24 12:21 ` paolo.carlini at oracle dot com
2012-11-01 21:10 ` paolo at gcc dot gnu.org
2012-11-01 21:11 ` paolo.carlini at oracle dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).