<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 736: Comment on [rand.dist.samp.discrete]</title>
<meta property="og:title" content="Issue 736: Comment on [rand.dist.samp.discrete]">
<meta property="og:description" content="C++ library issue. Status: NAD">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue736.html">
<meta property="og:type" content="website">
<meta property="og:image" content="http://cplusplus.github.io/LWG/images/cpp_logo.png">
<meta property="og:image:alt" content="C++ logo">
<style>
  p {text-align:justify}
  li {text-align:justify}
  pre code.backtick::before { content: "`" }
  pre code.backtick::after { content: "`" }
  blockquote.note
  {
    background-color:#E0E0E0;
    padding-left: 15px;
    padding-right: 15px;
    padding-top: 1px;
    padding-bottom: 1px;
  }
  ins {background-color:#A0FFA0}
  del {background-color:#FFA0A0}
  table.issues-index { border: 1px solid; border-collapse: collapse; }
  table.issues-index th { text-align: center; padding: 4px; border: 1px solid; }
  table.issues-index td { padding: 4px; border: 1px solid; }
  table.issues-index td:nth-child(1) { text-align: right; }
  table.issues-index td:nth-child(2) { text-align: left; }
  table.issues-index td:nth-child(3) { text-align: left; }
  table.issues-index td:nth-child(4) { text-align: left; }
  table.issues-index td:nth-child(5) { text-align: center; }
  table.issues-index td:nth-child(6) { text-align: center; }
  table.issues-index td:nth-child(7) { text-align: left; }
  table.issues-index td:nth-child(5) span.no-pr { color: red; }
  @media (prefers-color-scheme: dark) {
     html {
        color: #ddd;
        background-color: black;
     }
     ins {
        background-color: #225522
     }
     del {
        background-color: #662222
     }
     a {
        color: #6af
     }
     a:visited {
        color: #6af
     }
     blockquote.note
     {
        background-color: rgba(255, 255, 255, .10)
     }
  }
</style>
</head>
<body>
<hr>
<p><em>This page is a snapshot from the LWG issues list, see the <a href="lwg-active.html">Library Active Issues List</a> for more information and the meaning of <a href="lwg-active.html#NAD">NAD</a> status.</em></p>
<h3 id="736"><a href="lwg-closed.html#736">736</a>. Comment on [rand.dist.samp.discrete]</h3>
<p><b>Section:</b> 29.5.9.6.1 <a href="https://wg21.link/rand.dist.samp.discrete">[rand.dist.samp.discrete]</a> <b>Status:</b> <a href="lwg-active.html#NAD">NAD</a>
 <b>Submitter:</b> Stephan Tolksdorf <b>Opened:</b> 2007-09-21 <b>Last modified:</b> 2016-01-28</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all other</b> <a href="lwg-index.html#rand.dist.samp.discrete">issues</a> in [rand.dist.samp.discrete].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#NAD">NAD</a> status.</p>
<p><b>Discussion:</b></p>
<ol style="list-style-type:lower-alpha">
<li>
The specification for <code>discrete_distribution</code> requires the member <code>probabilities()</code>
to return a vector of <i>standardized</i> probabilities, which forces the implementation every time to 
divide each probability by the sum of all probabilities, as the sum will in practice almost never be 
exactly 1.0. This is unnecessarily inef ficient as the implementation would otherwise not need to 
compute the standardized probabilities at all and could instead work with the non-standardized 
probabilities and the sum. If there was no standardization the user would just get back the 
probabilities that were previously supplied to the distribution object, which to me seems to be the 
more obvious solution.
</li>
<li>
The behaviour of <code>discrete_distribution</code> is not specified in case the number of given
probabilities is larger than the maximum number representable by the IntType.
</li>
</ol>

<p>
<b>Possible resolution:</b> I propose to change the specification such that the non-standardized 
probabilities need to be returned and that an additional requirement is included for the number 
of probabilities to be smaller than the maximum of IntType.
</p>

<p><i>[
Stephan Tolksdorf adds pre-Bellevue:
]</i></p>


<blockquote>
<p>
In reply to the discussion in 
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2424.pdf">N2424</a>
of this issue:
</p>
<p>
Rescaled floating-point parameter vectors can not be expected to compare
equal because of the limited precision of floating-point numbers.
My proposal would at least guarantee that a parameter
vector (of type double) passed into the distribution would compare equal
with the one returned by the <code>probabilities()</code> method. Furthermore, I do
not understand why "the changed requirement would lead to a significant
increase in the amount of state in the distribution object". A typical
implementation's state would increase by exactly one number: the sum of
all probabilities. The textual representation for serialization would
not need to grow at all. Finally, the proposed replacement "<code>0 &lt; n &lt;=
numeric_limits&lt;IntType&gt;::max() + 1</code>" makes the implementation
unnecessarily complicated, "<code>0 &lt; n &lt;= numeric_limits&lt;IntType&gt;::max()</code>"
would be better.
</p>
</blockquote>

<p><i>[
Bellevue:
]</i></p>


<blockquote>
<p>
In N2424. We agree with the observation and the proposed resolution to
part b). We recommend the wording n &gt; 0 be replaced with 0 &lt; n
numeric_limits::max() + 1. However, we disagree with part a), as it
would interfere with the definition of parameters' equality. Further,
the changed requirement would lead to a significant increase in the
amount of state of the distribution object.
</p>

<p>
As it stands now, it is convenient, and the changes proposed make it
much less so.
</p>

<p>
NAD. Part a the current behavior is desirable. Part b, any constructor
can fail, but the rules under which it can fail do not need to be listed
here.
</p>
</blockquote>


<p id="res-736"><b>Proposed resolution:</b></p>
<p>
See <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2424.pdf">N2424</a>
for the proposed resolution.
</p>

<p><i>[
Stephan Tolksdorf adds pre-Bellevue:
]</i></p>


<blockquote>
<p>
In 29.5.9.6.1 <a href="https://wg21.link/rand.dist.samp.discrete">[rand.dist.samp.discrete]</a>:
</p>

<p>
Proposed wording a):
</p>

<blockquote>
<p>
Change in para. 2
</p>

<blockquote><p>
Constructs a <code>discrete_distribution</code> object with <code>n=1</code> and <code>p<sub>0</sub> <ins>= w<sub>0</sub></ins> = 1</code>
</p></blockquote>

<p>
and change in para. 5
</p>

<blockquote><p>
<i>Returns:</i> A <code>vector&lt;double&gt;</code> whose <code>size</code> member returns <code>n</code> and whose
<code>operator[]</code> member returns <del><code>p<sub>k</sub></code></del>
<ins>the weight <code>w<sub>k</sub></code> as a double value</ins>
when invoked with argument <code>k</code> for <code>k = 0,
..., n-1</code>
</p></blockquote>

</blockquote>

<p>
Proposed wording b):
</p>

<blockquote>
<p>
Change in para. 3:
</p>

<blockquote><p>
If <code>firstW == lastW</code>, let the sequence <code>w</code> have length <code>n = 1</code> and consist
of the single value <code>w<sub>0</sub> = 1</code>. Otherwise, <code>[firstW,lastW)</code> shall form a
sequence <code>w</code> of length <code>n <del>&gt; 0</del></code> 
<ins>such that <code>0 &lt; n &lt;= numeric_limits&lt;IntType&gt;::max()</code>,</ins>
and <code>*firstW</code> shall yield a value <code>w<sub>0</sub></code>
convertible to <code>double</code>. [<i>Note:</i> The values <code>w<sub>k</sub></code> are commonly known
as the weights . <i>-- end note</i>]
</p></blockquote>

</blockquote>

</blockquote>





</body>
</html>
