<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3172: 3-arg std::hypot is underspecified compared to the 2-arg overload</title>
<meta property="og:title" content="Issue 3172: 3-arg std::hypot is underspecified compared to the 2-arg overload">
<meta property="og:description" content="C++ library issue. Status: New">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3172.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="3172"><a href="lwg-active.html#3172">3172</a>. 3-arg <code>std::hypot</code> is underspecified compared to the 2-arg overload</h3>
<p><b>Section:</b> 29.7.3 <a href="https://wg21.link/c.math.hypot3">[c.math.hypot3]</a> <b>Status:</b> <a href="lwg-active.html#New">New</a>
 <b>Submitter:</b> Matthias Kretz <b>Opened:</b> 2018-12-06 <b>Last modified:</b> 2018-12-21</p>
<p><b>Priority: </b>3
</p>
<p><b>View all issues with</b> <a href="lwg-status.html#New">New</a> status.</p>
<p><b>Discussion:</b></p>
<p>
The 2-arg <code>hypot</code> function specified in the C standard is required to avoid overflow and underflow 
(7.12.7.3 p2). Furthermore C's Annex F (IEC 60559 floating-point arithmetic) defines special cases for 
inputs of <code>&plusmn;0</code> and <code>&plusmn;inf</code> (F. 10.4.3). The 3-arg <code>hypot</code> function 
29.7.3 <a href="https://wg21.link/c.math.hypot3">[c.math.hypot3]</a> is only specified as "<i>Returns:</i> 
<math xmlns="http://www.w3.org/1998/Math/MathML"><msqrt><msup><mi>x</mi><mn>2</mn></msup><mo>+</mo>
<msup><mi>y</mi><mn>2</mn></msup><mo>+</mo><msup><mi>z</mi><mn>2</mn></msup></msqrt></math>.". This is 
inconsistent with the 2-arg overload.
<p/>
It is not clear whether C's Annex F is "imported" into the C++ standard. [cmath.syn] p1 suggests that it 
is: "The contents and meaning of the header <code>&lt;cmath&gt;</code> are the same as the C standard library 
header <code>&lt;math.h&gt;</code>, [&hellip;]". 
</p>

<p><i>[2018-12-21 Reflector prioritization]</i></p>

<p>Set Priority to 3</p>


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

<blockquote class="note">
<p>
[<i>Drafting Note:</i> Two mutually exclusive options are prepared, depicted below by <b>Option A</b> and 
<b>Option B</b>, respectively.]
</p>
</blockquote>

<p><b>Option A</b></p>

<ol>
<li><p>Modify 29.7.3 <a href="https://wg21.link/c.math.hypot3">[c.math.hypot3]</a> as indicated:</p>

<blockquote>
<blockquote>
<pre>
float hypot(float x, float y, float z);
double hypot(double x, double y, double z);
long double hypot(long double x, long double y, long double z);
</pre>
<blockquote>
<p>
<ins>-?- <i>Effects:</i> The <code>hypot</code> functions compute the square root of the sum of the
squares of <code>x</code>, <code>y</code>, and <code>z</code>, without undue overflow or underflow. A range error
may occur.</ins>
<p/>
-1- <i>Returns:</i> <math xmlns="http://www.w3.org/1998/Math/MathML"><msqrt><msup><mi>x</mi><mn>2</mn></msup><mo>+</mo>
<msup><mi>y</mi><mn>2</mn></msup><mo>+</mo><msup><mi>z</mi><mn>2</mn></msup></msqrt></math>.
<p/>
<ins>-?- <i>Remarks:</i> If <code>__STDC_IEC_559__</code> is defined, the following guarantees hold:</ins>
</p>
<ul>
<li><p><ins><code>hypot(x, y, z)</code>, <code>hypot(x, z, y)</code>, <code>hypot(z, y, x)</code>, and <code>hypot(x, y, -z)</code> 
are equivalent.</ins></p></li>
<li><p><ins>if <code>y<sup>2</sup> + z<sup>2</sup> == &plusmn;0</code>, <code>hypot(x, y, z)</code> is equivalent to <code>fabs(x)</code>.</ins></p></li>
<li><p><ins><code>hypot(&plusmn;&infin;, y, z)</code> returns <code>+&infin;</code>, even if <code>y</code> and/or <code>z</code> is a 
NaN.</ins></p></li>
</ul>
</blockquote>
</blockquote>
</blockquote>
</li>
</ol>

<p><b>Option B</b></p>

<ol>
<li><p>Add a note that clarifies that the behavior of the 3-arg <code>hypot</code> function
differs from the C specification. I.e. that no special guarantees wrt. over-/underflow or 
special values are given.</p>
</li>
</ol>





</body>
</html>
