<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2086: Overly generic type support for math functions</title>
<meta property="og:title" content="Issue 2086: Overly generic type support for math functions">
<meta property="og:description" content="C++ library issue. Status: C++14">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2086.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++14">C++14</a> status.</em></p>
<h3 id="2086"><a href="lwg-defects.html#2086">2086</a>. Overly generic type support for math functions</h3>
<p><b>Section:</b> 29.7 <a href="https://wg21.link/c.math">[c.math]</a> <b>Status:</b> <a href="lwg-active.html#C++14">C++14</a>
 <b>Submitter:</b> Daniel Kr&uuml;gler <b>Opened:</b> 2011-09-22 <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#c.math">issues</a> in [c.math].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++14">C++14</a> status.</p>
<p><b>Discussion:</b></p>

<p>
29.7 <a href="https://wg21.link/c.math">[c.math]</a> ends with a description of a rule set for "sufficient overloads"
in p11:
</p>

<blockquote>
<p>Moreover, there shall be additional overloads sufficient to ensure:</p>
<ol>
<li>
If any argument corresponding to a <code>double</code> parameter has type <code>long double</code>, then all arguments
corresponding to <code>double</code> parameters are effectively cast to <code>long double</code>.
</li>
<li>
Otherwise, if any argument corresponding to a <code>double</code> parameter has type <code>double</code> or an integer type,
then all arguments corresponding to <code>double</code> parameters are effectively cast to <code>double</code>.
</li>
<li>
Otherwise, all arguments corresponding to <code>double</code> parameters are effectively cast to <code>float</code>.
</li>
</ol>
</blockquote>

<p>
My impression is that this rule set is probably more generic as intended, my assumption is that it is written 
to mimic the C99&#47;C1x rule set in 7.25 p2+3 in the "C++" way:
</p>

<blockquote>
<p>
-2- Of the <code>&lt;math.h&gt;</code> and <code>&lt;complex.h&gt;</code> functions without an 
<code>f</code> (<code>float</code>) or <code>l</code> (<code>long double</code>) suffix, several have 
one or more parameters whose corresponding real type is <code>double</code>. For each such 
function, except <code>modf</code>, there is a corresponding type-generic macro. (footnote 313) 
The parameters whose corresponding real type is <code>double</code> in the function
synopsis are generic parameters. Use of the macro invokes a function whose
corresponding real type and type domain are determined by the arguments for the generic
parameters. (footnote 314)
<p/>
-3- Use of the macro invokes a function whose generic parameters have the corresponding 
real type determined as follows:
</p>
<ul>
<li>
First, if any argument for generic parameters has type <code>long double</code>, the type
determined is <code>long double</code>.
</li>
<li>
Otherwise, if any argument for generic parameters has type <code>double</code> or is of integer
type, the type determined is <code>double</code>.
</li>
<li>
Otherwise, the type determined is <code>float</code>.
</li>
</ul>
</blockquote>

<p>
where footnote 314 clarifies the intent:
</p>

<blockquote>
<p>
If the type of the argument is not compatible with the type of the parameter for the selected function,
the behavior is undefined.
</p>
</blockquote>

<p>
The combination of the usage of the unspecific term "cast" with otherwise no further constraints 
(note that C constraints the valid set to types that C++ describes as arithmetic types, but see below 
for one important difference) has the effect that it requires the following examples to be well-formed 
and well-defined:
</p>

<blockquote><pre>
#include &lt;cmath&gt;

enum class Ec { };

struct S { explicit operator long double(); };

void test(Ec e, S s) {
 std::sqrt(e); // OK, behaves like std::sqrt((float) e);
 std::sqrt(s); // OK, behaves like std::sqrt((float) s);
}
</pre></blockquote>
<p>
GCC 4.7 does not accept any of these examples.
<p/>
I found another example where the C++ rule differs from the C set, 
but in this case I'm not so sure, which direction C++ should follow. 
The difference is located in the fact, that in C enumerated types are 
<em>integer types</em> as described in 6.2.5 p17 (see e.g. n1569 or n1256):
<p/>
"The type char, the signed and unsigned integer types, and
the enumerated types are collectively called integer types. The
integer and real floating types are collectively called real types."
<p/>
This indicates that in C the following code
</p>
<blockquote><pre>
#include &lt;math.h&gt;

enum E { e };

void test(void) {
  sqrt(e); // OK, behaves like sqrt((double) e);
}
</pre></blockquote>
<p>
seems to be well-defined and <code>e</code> is cast to <code>double</code>, but in C++
referring to
</p>
<blockquote><pre>
#include &lt;cmath&gt;

enum E { e };

void test() {
  std::sqrt(e); // OK, behaves like sqrt((float) e);
}
</pre></blockquote>

<p>
is also well-defined (because of our lack of constraints) but we
must skip bullet 2 (because E is not an integer type) and effectively
cast <code>e</code> to <code>float</code>. Accepting this, we would introduce 
a silent, but observable runtime difference for C and C++.
<p/>
GCC 4.7 does not accept this example, but causes an ambiguity
error among the three floating point overloads of sqrt.
<p/>
My current suggestion to fix these problems would be to constrain the 
valid argument types of these functions to arithmetic types.
</p>

<blockquote><p>
Howard provided wording to solve the issue.
</p></blockquote>

<p><i>[2012, Kona]</i></p>

<p>
Moved to Ready.  The proposed wording reflects both original intent from
TR1, and current implementations.
</p>

<p><i>[2012, Portland: applied to WP]</i></p>




<p id="res-2086"><b>Proposed resolution:</b></p>
<p>This wording is relative to the FDIS.</p>

<p>Change 29.7 <a href="https://wg21.link/c.math">[c.math]</a> p11 as indicated:</p>

<blockquote>
<p>Moreover, there shall be additional overloads sufficient to ensure:</p>
<ol>
<li>
If any <ins>arithmetic</ins> argument corresponding to a <code>double</code> parameter has 
type <code>long double</code>, then all <ins>arithmetic</ins> arguments corresponding to 
<code>double</code> parameters are effectively cast to <code>long double</code>.
</li>
<li>
Otherwise, if any <ins>arithmetic</ins> argument corresponding to a <code>double</code> 
parameter has type <code>double</code> or an integer type, then all <ins>arithmetic</ins> 
arguments corresponding to <code>double</code> parameters are effectively cast to <code>double</code>.
</li>
<li>
Otherwise, all <ins>arithmetic</ins> arguments corresponding to <code>double</code> parameters 
<del>are effectively cast to</del><ins>have type</ins> <code>float</code>.
</li>
</ol>
</blockquote>






</body>
</html>
