<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 4014: LWG 3809 changes behavior of some existing std::subtract_with_carry_engine code</title>
<meta property="og:title" content="Issue 4014: LWG 3809 changes behavior of some existing std::subtract_with_carry_engine code">
<meta property="og:description" content="C++ library issue. Status: WP">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue4014.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#WP">WP</a> status.</em></p>
<h3 id="4014"><a href="lwg-defects.html#4014">4014</a>. LWG 3809 changes behavior of some existing <code>std::subtract_with_carry_engine</code> code</h3>
<p><b>Section:</b> 29.5.4.4 <a href="https://wg21.link/rand.eng.sub">[rand.eng.sub]</a> <b>Status:</b> <a href="lwg-active.html#WP">WP</a>
 <b>Submitter:</b> Matt Stephanson <b>Opened:</b> 2023-11-15 <b>Last modified:</b> 2024-11-28</p>
<p><b>Priority: </b>2
</p>
<p><b>View all other</b> <a href="lwg-index.html#rand.eng.sub">issues</a> in [rand.eng.sub].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#WP">WP</a> status.</p>
<p><b>Discussion:</b></p>
<p>
Issue <a href="lwg-defects.html#3809" title="Is std::subtract_with_carry_engine&lt;uint16_t&gt; supposed to work? (Status: WP)">3809</a><sup><a href="https://cplusplus.github.io/LWG/issue3809" title="Latest snapshot">(i)</a></sup> pointed out that <code>subtract_with_carry_engine&lt;T&gt;</code> can be seeded with values
from a <code>linear_congruential_engine&lt;T, 40014u, 0u, 2147483563u&gt;</code> object, which results in narrowing
when <code>T</code> is less than 32 bits. Part of the resolution was to modify the LCG seed sequence as follows:
</p>
<blockquote>
<pre>explicit subtract_with_carry_engine(result_type value);</pre>
<p>-7- <i>Effects</i>:
Sets the values of
<math>
  <mrow>
    <msub> <mi>X</mi> <mrow> <mo>-</mo> <mi>r</mi> </mrow> </msub>
    <mo separator="true">,</mo>
    <mo>&hellip;</mo>
    <mo separator="true">,</mo>
    <msub> <mi>X</mi> <mrow> <mo>-</mo> <mn>1</mn> </mrow> </msub>
  </mrow>
</math>,
in that order, as specified below. If
<math>
  <mrow>
    <msub> <mi>X</mi> <mrow> <mo>-</mo> <mn>1</mn> </mrow> </msub>
  </mrow>
</math>
is then <math> <mn>0</mn> </math>,
sets <math> <mi>c</mi> </math> to <math> <mn>1</mn> </math>;
otherwise sets <math> <mi>c</mi> </math> to <math> <mn>0</mn> </math>.
</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;
To set the values
<math> <mrow> <msub> <mi>X</mi> <mi>k</mi> </msub> </mrow> </math>,
first construct <code>e</code>, a <code>linear_congruential_engine</code> object,
as if by the following definition:
</p>
<blockquote><pre>
linear_congruential_engine&lt;<del>result_type</del><ins>uint_least32_t</ins>,
                           40014u,0u,2147483563u&gt; e(value == 0u ? default_seed : value);
</pre></blockquote>
<p>&nbsp;&nbsp;&nbsp;&nbsp;
Then, to set each
<math> <mrow> <msub> <mi>X</mi> <mi>k</mi> </msub> </mrow> </math>,
obtain new values
<math>
  <mrow>
    <msub> <mi>z</mi> <mn>0</mn> </msub>
    <mo separator="true">,</mo>
    <mo>&hellip;</mo>
    <mo separator="true">,</mo>
    <msub> <mi>z</mi> <mrow> <mi>n</mi> <mo>-</mo> <mn>1</mn> </mrow> </msub>
  </mrow>
</math>
from
<math>
  <mrow>
    <mi>n</mi> <mo>=</mo>
    <mo form="prefix" stretchy="false">&#x2308;</mo>
    <mi>w</mi> <mo lspace="0em" rspace="0em">/</mo> <mn>32</mn>
    <mo form="postfix" stretchy="false">&#x2309;</mo>
  </mrow>
</math>
successive invocations of <code>e</code>.
Set
<math> <mrow> <msub> <mi>X</mi> <mi>k</mi> </msub> </mrow> </math>
to
<math>
  <mrow>
    <mrow>
      <mo fence="true" form="prefix">(</mo>
      <msubsup>
        <mo movablelimits="false">&sum;</mo>
        <mrow> <mi>j</mi> <mo>=</mo> <mn>0</mn> </mrow>
        <mrow> <mi>n</mi> <mo>-</mo> <mn>1</mn> </mrow>
      </msubsup>
      <msub> <mi>z</mi> <mi>j</mi> </msub>
      <mo>&#x2219;</mo>
      <msup> <mn>2</mn> <mrow> <mn>32</mn> <mi>j</mi> </mrow> </msup>
      <mo fence="true" form="postfix">)</mo>
    </mrow>
    <mo lspace="0.2222em" rspace="0.2222em">mod</mo>
    <mi>m</mi>
  </mrow>
</math>.
</p>
</blockquote>
<p>
Inside <code>linear_congruential_engine</code>, the seed is reduced modulo 2147483563, so <code>uint_least32_t</code>
is fine from that point on. This resolution, however, forces <code>value</code>, the user-provided seed, to be
truncated from <code>result_type</code> to <code>uint_least32_t</code> before the reduction, which generally will
change the result. It also breaks the existing behavior that two seeds are equivalent if they're in the same
congruence class modulo the divisor.
</p>

<p><i>[2024-01-11; Reflector poll]</i></p>

<p>
Set priority to 2 after reflector poll.

<p><i>[2024-01-11; Jonathan comments]</i></p>

<p>
More precisely, the resolution forces <code>value</code> to be <i>converted</i>
to <code>uint_least32_t</code>, which doesn't necessarily truncate, and if it
does truncate, it doesn't necessarily change the value.
But it will truncate whenever <code>value_type</code> is wider than
<code>uint_least32_t</code>,
e.g. for 32-bit <code>uint_least32_t</code> you get a different result for
<code>std::ranlux48_base(UINT_MAX + 1LL)()</code>.
The new proposed resolution below restores the old behaviour for that type.
</p>

</p>

<p><i>[2024-10-09; LWG telecon: Move to Ready]</i></p>


<p><i>[Wrocław 2024-11-23; Status changed: Voting &rarr; WP.]</i></p>



<p id="res-4014"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="https://wg21.link/N4964" title=" Working Draft, Programming Languages — C++">N4964</a> <em>after the wording changes applied</em> by LWG <a href="lwg-defects.html#3809" title="Is std::subtract_with_carry_engine&lt;uint16_t&gt; supposed to work? (Status: WP)">3809</a><sup><a href="https://cplusplus.github.io/LWG/issue3809" title="Latest snapshot">(i)</a></sup>,
which had been accepted into the working paper during the Kona 2023-11 meeting.
</p>

<ol>

<li><p>Modify 29.5.4.4 <a href="https://wg21.link/rand.eng.sub">[rand.eng.sub]</a> as indicated:</p>

<blockquote>
<pre>explicit subtract_with_carry_engine(result_type value);</pre>
<p>-7- <i>Effects</i>:
Sets the values of
<math>
  <mrow>
    <msub> <mi>X</mi> <mrow> <mo>-</mo> <mi>r</mi> </mrow> </msub>
    <mo separator="true">,</mo>
    <mo>&hellip;</mo>
    <mo separator="true">,</mo>
    <msub> <mi>X</mi> <mrow> <mo>-</mo> <mn>1</mn> </mrow> </msub>
  </mrow>
</math>,
in that order, as specified below. If
<math>
  <mrow>
    <msub> <mi>X</mi> <mrow> <mo>-</mo> <mn>1</mn> </mrow> </msub>
  </mrow>
</math>
is then <math> <mn>0</mn> </math>,
sets <math> <mi>c</mi> </math> to <math> <mn>1</mn> </math>;
otherwise sets <math> <mi>c</mi> </math> to <math> <mn>0</mn> </math>.
</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;
To set the values
<math> <mrow> <msub> <mi>X</mi> <mi>k</mi> </msub> </mrow> </math>,
first construct <code>e</code>, a <code>linear_congruential_engine</code> object,
as if by the following definition:
</p>
<blockquote><pre>
linear_congruential_engine&lt;uint_least32_t,
                           40014u,0u,2147483563u&gt; e(value == 0u ? default_seed :
                           <ins>static_cast&lt;uint_least32_t&gt;(</ins>value<ins> % 2147483563u)</ins>);
</pre></blockquote>
<p>&nbsp;&nbsp;&nbsp;&nbsp;
Then, to set each
<math> <mrow> <msub> <mi>X</mi> <mi>k</mi> </msub> </mrow> </math>,
obtain new values
<math>
  <mrow>
    <msub> <mi>z</mi> <mn>0</mn> </msub>
    <mo separator="true">,</mo>
    <mo>&hellip;</mo>
    <mo separator="true">,</mo>
    <msub> <mi>z</mi> <mrow> <mi>n</mi> <mo>-</mo> <mn>1</mn> </mrow> </msub>
  </mrow>
</math>
from
<math>
  <mrow>
    <mi>n</mi> <mo>=</mo>
    <mo form="prefix" stretchy="false">&#x2308;</mo>
    <mi>w</mi> <mo lspace="0em" rspace="0em">/</mo> <mn>32</mn>
    <mo form="postfix" stretchy="false">&#x2309;</mo>
  </mrow>
</math>
successive invocations of <code>e</code>.
Set
<math> <mrow> <msub> <mi>X</mi> <mi>k</mi> </msub> </mrow> </math>
to
<math>
  <mrow>
    <mrow>
      <mo fence="true" form="prefix">(</mo>
      <msubsup>
        <mo movablelimits="false">&sum;</mo>
        <mrow> <mi>j</mi> <mo>=</mo> <mn>0</mn> </mrow>
        <mrow> <mi>n</mi> <mo>-</mo> <mn>1</mn> </mrow>
      </msubsup>
      <msub> <mi>z</mi> <mi>j</mi> </msub>
      <mo>&#x2219;</mo>
      <msup> <mn>2</mn> <mrow> <mn>32</mn> <mi>j</mi> </mrow> </msup>
      <mo fence="true" form="postfix">)</mo>
    </mrow>
    <mo lspace="0.2222em" rspace="0.2222em">mod</mo>
    <mi>m</mi>
  </mrow>
</math>.
</p>
</blockquote>

</li>

</ol>





</body>
</html>
