<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 4052: Bogus requirements for piecewise_linear_distribution</title>
<meta property="og:title" content="Issue 4052: Bogus requirements for piecewise_linear_distribution">
<meta property="og:description" content="C++ library issue. Status: New">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue4052.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#New">New</a> status.</em></p>
<h3 id="4052"><a href="lwg-active.html#4052">4052</a>. Bogus requirements for <code>piecewise_linear_distribution</code></h3>
<p><b>Section:</b> 29.5.9.6.2 <a href="https://wg21.link/rand.dist.samp.pconst">[rand.dist.samp.pconst]</a>, 29.5.9.6.3 <a href="https://wg21.link/rand.dist.samp.plinear">[rand.dist.samp.plinear]</a> <b>Status:</b> <a href="lwg-active.html#New">New</a>
 <b>Submitter:</b> Jonathan Wakely <b>Opened:</b> 2024-02-05 <b>Last modified:</b> 2024-03-12</p>
<p><b>Priority: </b>4
</p>
<p><b>View all other</b> <a href="lwg-index.html#rand.dist.samp.pconst">issues</a> in [rand.dist.samp.pconst].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#New">New</a> status.</p>
<p><b>Discussion:</b></p>
<p>
In the second constructor of 29.5.9.6.3 <a href="https://wg21.link/rand.dist.samp.plinear">[rand.dist.samp.plinear]</a>,
<a href="https://wg21.link/P1719R2" title=" Mandating the Standard Library: Clause 26 - Numerics library">P1719R2</a> replaced:

<blockquote>
<pre><code>
template&lt;class InputIteratorB, class InputIteratorW&gt;
  piecewise_linear_distribution(InputIteratorB firstB, InputIteratorB lastB,
                                InputIteratorW firstW);
</code></pre>
<p>
<em>Effects</em>: [...] Moreover, the <em>id-expressions</em>
<code>iterator_traits&lt;InputIteratorB&gt;::value_type</code>
and
<code>iterator_traits&lt;InputIteratorW&gt;::value_type</code>
shall each denote a type that is convertible to
<code>double</code>.
</p>
</blockquote>
with
<blockquote>
<pre><code>
template&lt;class InputIteratorB, class InputIteratorW&gt;
  piecewise_linear_distribution(InputIteratorB firstB, InputIteratorB lastB,
                                InputIteratorW firstW);
</code></pre>
<p>
<em>Mandates</em>: 
<code>is_invocable_r_v&lt;double, UnaryOperation&amp;, double&gt;</code>
is <code>true</code>.
</p>
<p><em>Preconditions</em>: [...]</p>
<p><em>Effects</em>: [...]</p>

</blockquote>
</p>
<p>
This was a copy &amp; paste error from the next constructor,
and was obviously not intended. There is no <code>UnaryOperation</code>
in that constructor.
</p>
<p>A less obviously wrong issue is the use of <code>double</code> there
in the first place. Shouldn't it be <code>RealType</code> instead?
That seems to be incorrect throughout both
29.5.9.6.2 <a href="https://wg21.link/rand.dist.samp.pconst">[rand.dist.samp.pconst]</a>
and
29.5.9.6.3 <a href="https://wg21.link/rand.dist.samp.plinear">[rand.dist.samp.plinear]</a>,
and was only partially fixed by LWG <a href="lwg-defects.html#1439" title="Return from densities() functions? (Status: C++11)">1439</a><sup><a href="https://cplusplus.github.io/LWG/issue1439" title="Latest snapshot">(i)</a></sup>.
</p>

<p>
Finally, the  preconditions also say:
</p>
<blockquote>
<em>Preconditions</em>: [...]
If <code>firstB == lastB</code> or <code>++firstB == lastB</code>,
let <em>n</em> = 1, <em>ρ</em><sub>0</sub> = <em>ρ</em><sub>1</sub> = 1,
<em>b</em><sub>0</sub> = 0, and <em>b</em><sub>1</sub> = 1.
Otherwise, [<code>firstB</code>, <code>lastB</code>)
forms a sequence <em>b</em> of length <em>n</em> + 1,
the length of the sequence <em>w</em> starting
from <code>firstW</code> is at least <em>n</em>,
and any <em>w<sub>k</sub></em> for <em>k</em> ≥ <em>n</em>
are ignored by the distribution.
</blockquote>
<p>
These are not preconditions. I think it is trying to say something like:
</p>
<blockquote>
<p>
<em>Preconditions</em>: [...]
[<code>firstB</code>, <code>lastB</code>) is a valid range and
[<code>firstW</code>, <code>firstW + (lastB - firstB)</code>)
is a valid range.
</p>
<p>
<em>Effects</em>:
If <code>firstB == lastB</code> or <code>++firstB == lastB</code>,
let <em>n</em> = 1, <em>ρ</em><sub>0</sub> = <em>ρ</em><sub>1</sub> = 1,
<em>b</em><sub>0</sub> = 0, and <em>b</em><sub>1</sub> = 1.
Otherwise, let [<code>firstB</code>, <code>lastB</code>) form a sequence
<em>b</em><sub>0</sub>, &hellip;, <em>b</em><sub>n</sub>,
and let <em>w<sub>k</sub></em> = <code>*firstW++</code>
for <em>k</em> = 0, &hellip;, <em>n</em>.
</p>
</blockquote>

<p>
The equivalent constructor for <code>piecewise_constant_distribution</code>
has similar problems with its preconditions in terms of <em>n</em> + 1.
</p>

<p><i>[2024-03-12; Reflector poll]</i></p>

<p>
Set priority to 4 after reflector poll.
The copy &amp; paste error was
<a href="https://github.com/cplusplus/draft/pull/6794">fixed editorially</a>.
</p>



<p id="res-4052"><b>Proposed resolution:</b></p>
<p>
</p>





</body>
</html>
