public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "hyounes at google dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug libstdc++/55047] New: operator() in std::exponential_distribution may call log(0)
Date: Wed, 24 Oct 2012 02:25:00 -0000	[thread overview]
Message-ID: <bug-55047-4@http.gcc.gnu.org/bugzilla/> (raw)


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.


             reply	other threads:[~2012-10-24  2:25 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-10-24  2:25 hyounes at google dot com [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-55047-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).