<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3248: std::format #b, #B, #o, #x, and #X 
presentation types misformat negative numbers</title>
<meta property="og:title" content="Issue 3248: std::format #b, #B, #o, #x, and #X 
presentation types misformat negative numbers">
<meta property="og:description" content="C++ library issue. Status: C++20">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3248.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#C++20">C++20</a> status.</em></p>
<h3 id="3248"><a href="lwg-defects.html#3248">3248</a>. <code>std::format</code> <code>#b</code>, <code>#B</code>, <code>#o</code>, <code>#x</code>, and <code>#X</code> 
presentation types misformat negative numbers</h3>
<p><b>Section:</b> 28.5.2.2 <a href="https://wg21.link/format.string.std">[format.string.std]</a> <b>Status:</b> <a href="lwg-active.html#C++20">C++20</a>
 <b>Submitter:</b> Richard Smith <b>Opened:</b> 2019-08-01 <b>Last modified:</b> 2021-02-25</p>
<p><b>Priority: </b>2
</p>
<p><b>View other</b> <a href="lwg-index-open.html#format.string.std">active issues</a> in [format.string.std].</p>
<p><b>View all other</b> <a href="lwg-index.html#format.string.std">issues</a> in [format.string.std].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++20">C++20</a> status.</p>
<p><b>Discussion:</b></p>
<p>
According to both the specification for <code>'#'</code> and the presentation types <code>b</code>, <code>B</code>, 
<code>o</code>, <code>x</code>, and <code>X</code> (which redundantly duplicate the rule for the relevant prefixes), 
the string <code>0b</code>, <code>0B</code>, <code>0</code>, <code>0x</code>, or <code>0X</code> is prefixed to the result 
of <code>to_chars</code>. That means:
</p>
<blockquote><pre>
std::format("{0:#b} {0:#B} {0:#o} {0:#x} {0:#X}", -1)
</pre></blockquote>
<p>
produces
</p>
<blockquote><pre>
"0b-1 0B-1 0-1 0x-1 0X-1"
</pre></blockquote>
<p>
I assume that's a bug?
<p/>
(Additionally, if the <code>+</code> specifier is used, there appears to be no specification of where 
the sign is inserted into the output.)
<p/>
<b>Victor Zverovich:</b>
<p/>
Yes. I suggest replacing
</p>
<blockquote><p>
adds the respective prefix <code>"0b"</code> (<code>"0B"</code>), <code>"0"</code>, or <code>"0x"</code> (<code>"0X"</code>) 
to the output value
</p></blockquote>
<p>
with something like 
</p>
<blockquote><p>
inserts the respective prefix <code>"0b"</code> (<code>"0B"</code>), <code>"0"</code>, or <code>"0x"</code> (<code>"0X"</code>) 
to the output value after the sign, if any,
</p></blockquote>
<p>
I think the duplicate wording in the presentation types <code>b</code>, <code>B</code>, <code>o</code>, <code>x</code>, and 
<code>X</code> can be dropped.
<p/>
Regarding the <code>+</code> specifier problem: How about adding
</p>
<blockquote><p>
The sign is inserted before the output of <code>to_chars</code>.
</p></blockquote>
<p>
?
</p>

<p><i>[2019-08-21 Priority set to 2 based on reflector discussion]</i></p>


<strong>Previous resolution [SUPERSEDED]:</strong>
<blockquote class="note">
<p>This wording is relative to <a href="https://wg21.link/n4830">N4830</a>.</p>

<ol>
<li><p>Modify the <i>sign</i> options Table [tab:format.sign] in 28.5.2.2 <a href="https://wg21.link/format.string.std">[format.string.std]</a> as indicated:</p>

<blockquote>
<table border="1">
<caption>Table 59: Meaning of <i>sign</i> options [tab:format.sign]</caption>
<tr align="center">
<th>Option</th>
<th>Meaning</th>
</tr> 

<tr>
<td>
<code>'+'</code>
</td>
<td>
Indicates that a sign should be used for both non-negative and negative numbers. <ins>The sign is 
inserted before the output of <code>to_chars</code>.</ins>
</td>
</tr>

<tr>
<td>
<code>'-'</code>
</td>
<td>
Indicates that a sign should be used only for negative numbers (this is the default behavior).
</td>
</tr>

<tr>
<td>
space
</td>
<td>
Indicates that a leading space should be used for non-negative numbers, and a minus
sign for negative numbers.
</td>
</tr>

</table>
</blockquote>
</li>

<li><p>Modify [format.string] as indicated:</p>

<blockquote>
<p>
-6 The <code>#</code> option causes the <i>alternate form</i> to be used for the conversion. This option 
is only valid for arithmetic types other than <code>charT</code> and <code>bool</code> or when an integer 
presentation type is specified. For integral types, the alternate form <del>adds</del><ins>inserts</ins> 
the base prefix (if any) specified in Table [tab:format.type.int] to the output value <ins>after the 
sign, if any</ins>. [&hellip;]
</p>
</blockquote>
</li>

</ol>
</blockquote>

<p><i>[2019-08-21; Victor Zverovich provides improved wording]</i></p>


<strong>Previous resolution [SUPERSEDED]:</strong>
<blockquote class="note">
<p>This wording is relative to <a href="https://wg21.link/n4830">N4830</a>.</p>

<ol>
<li><p>Modify the <i>sign</i> options Table [tab:format.sign] in 28.5.2.2 <a href="https://wg21.link/format.string.std">[format.string.std]</a> as indicated:</p>

<blockquote>
<table border="1">
<caption>Table 59: Meaning of <i>sign</i> options [tab:format.sign]</caption>
<tr align="center">
<th>Option</th>
<th>Meaning</th>
</tr> 

<tr>
<td>
<code>'+'</code>
</td>
<td>
Indicates that a sign should be used for both non-negative and negative numbers. <ins>The <code>+</code> 
sign is inserted before the output of <code>to_chars</code> for non-negative numbers other than 
the negative zero. [<i>Note:</i> For negative numbers and the negative zero the output of 
<code>to_chars</code> will already contain the sign so no additional transformation is performed. &mdash; 
<i>end note</i>]</ins>
</td>
</tr>

<tr>
<td>
<code>'-'</code>
</td>
<td>
Indicates that a sign should be used only for negative numbers (this is the default behavior).
</td>
</tr>

<tr>
<td>
space
</td>
<td>
Indicates that a leading space should be used for non-negative numbers, and a minus
sign for negative numbers.
</td>
</tr>

</table>
</blockquote>
</li>

<li><p>Modify [format.string] as indicated:</p>

<blockquote>
<p>
-6- The <code>#</code> option causes the <i>alternate form</i> to be used for the conversion. This option 
is only valid for arithmetic types other than <code>charT</code> and <code>bool</code> or when an integer 
presentation type is specified. For integral types, the alternate form <del>adds</del><ins>inserts</ins> 
the base prefix (if any) specified in Table [tab:format.type.int] to the output value <ins>after the 
sign character (possibly space) if there is one, or before the output of <code>to_chars</code> otherwise</ins>. 
[&hellip;]
</p>
</blockquote>
</li>

</ol>
</blockquote>

<p><i>[2020-02-13, Prague]</i></p>

<p>
LWG made some improvements to the wording.
</p>
<p><i>[2020-02 Status to Immediate on Thursday night in Prague.]</i></p>



<p id="res-3248"><b>Proposed resolution:</b></p>
<p>This wording is relative to <a href="https://wg21.link/n4849">N4849</a>.</p>

<ol>
<li><p>Modify the <i>sign</i> options Table [tab:format.sign] in 28.5.2.2 <a href="https://wg21.link/format.string.std">[format.string.std]</a> as indicated:</p>

<blockquote>
<table border="1">
<caption>Table 58: Meaning of <i>sign</i> options [tab:format.sign]</caption>
<tr align="center">
<th>Option</th>
<th>Meaning</th>
</tr> 

<tr>
<td>
<code>'+'</code>
</td>
<td>
Indicates that a sign should be used for both non-negative and negative numbers. <ins>The <code>+</code> 
sign is inserted before the output of <code>to_chars</code> for non-negative numbers other than 
negative zero. [<i>Note:</i> For negative numbers and negative zero the output of 
<code>to_chars</code> will already contain the sign so no additional transformation is performed. &mdash; 
<i>end note</i>]</ins>
</td>
</tr>

<tr>
<td>
<code>'-'</code>
</td>
<td>
Indicates that a sign should be used only for negative numbers (this is the default behavior).
</td>
</tr>

<tr>
<td>
space
</td>
<td>
Indicates that a leading space should be used for non-negative numbers, and a minus
sign for negative numbers.
</td>
</tr>

</table>
</blockquote>
</li>

<li><p>Modify [format.string] as indicated:</p>

<blockquote>
<p>
-6- The <code>#</code> option causes the <i>alternate form</i> to be used for the conversion. This option 
is <del>only</del> valid for arithmetic types other than <code>charT</code> and <code>bool</code> or when an integer 
presentation type is specified<ins>, and not otherwise</ins>. For integral types, the alternate form 
<del>adds</del><ins>inserts</ins> the base prefix (if any) specified in Table [tab:format.type.int] 
<del>to</del><ins>into</ins> the output <del>value</del> <ins>after the sign character (possibly space) 
if there is one, or before the output of <code>to_chars</code> otherwise</ins>. 
[&hellip;]
</p>
</blockquote>
</li>

</ol>




</body>
</html>
