<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 550: What should the return type of pow(float,int) be?</title>
<meta property="og:title" content="Issue 550: What should the return type of pow(float,int) be?">
<meta property="og:description" content="C++ library issue. Status: CD1">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue550.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#CD1">CD1</a> status.</em></p>
<h3 id="550"><a href="lwg-defects.html#550">550</a>. What should the return type of <code>pow(float,int)</code> be?</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#CD1">CD1</a>
 <b>Submitter:</b> Howard Hinnant <b>Opened:</b> 2006-01-12 <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#CD1">CD1</a> status.</p>
<p><b>Discussion:</b></p>
<p>
Assuming we adopt the
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1836.pdf">C
compatibility package from C99</a>  what should be the return type of the
following signature be:
</p>
<blockquote><pre>
?  pow(float, int);
</pre></blockquote>
<p>
C++03 says that the return type should be <code>float</code>. 
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1836.pdf">
TR1</a> and C90&#47;99 say the return type should be <code>double</code>.  This can put
clients into a situation where C++03 provides answers that are not as high
quality as C90&#47;C99&#47;TR1.  For example:
</p>
<blockquote><pre>
#include &lt;math.h&gt;

int main()
{
    float x = 2080703.375F;
    double y = pow(x, 2);
}
</pre></blockquote>
<p>
Assuming an IEEE 32 bit float and IEEE 64 bit double, C90&#47;C99&#47;TR1 all suggest:
</p>

<blockquote><pre>
y = 4329326534736.390625
</pre></blockquote>

<p>
which is exactly right.  While C++98&#47;C++03 demands:
</p>

<blockquote><pre>
y = 4329326510080.
</pre></blockquote>

<p>
which is only approximately right.
</p>

<p>
I recommend that C++0X adopt the mixed mode arithmetic already adopted by
Fortran, C and TR1 and make the return type of <code>pow(float,int)</code> be
<code>double</code>.
</p>

<p><i>[
Kona (2007): Other functions that are affected by this issue include
<code>ldexp</code>, <code>scalbln</code>, and <code>scalbn</code>. We also believe that there is a typo in
26.7&#47;10: <code>float nexttoward(float, long double);</code> [sic] should be <code>float
nexttoward(float, float);</code> Proposed Disposition: Review (the proposed
resolution appears above, rather than below, the heading "Proposed
resolution")
]</i></p>


<p><i>[Howard, post Kona:]</i></p>

<blockquote>
<p>
Unfortunately I strongly disagree with a part of the resolution
from Kona.  I am moving from New to Open instead of to Review because I do not believe
we have consensus on the intent of the resolution.
</p>
<p>
This issue does not include <code>ldexp</code>, <code>scalbln</code>, and <code>scalbn</code> because
the second integral parameter in each of these signatures (from C99) is <b>not</b> a
<i>generic parameter</i> according to C99 7.22p2.  The corresponding C++ overloads are
intended (as far as I know) to correspond directly to C99's definition of <i>generic parameter</i>.
</p>
<p>
For similar reasons, I do not believe that the second <code>long double</code> parameter of
<code>nexttoward</code>, nor the return type of this function, is in error.  I believe the
correct signature is:
</p>
<blockquote>
<pre>
float nexttoward(float, long double);
</pre>
</blockquote>
<p>
which is what both the C++0X working paper and C99 state (as far as I currently understand).
</p>
<p>
This is really <b>only</b> about <code>pow(float, int)</code>.  And this is because C++98 took one
route (with <code>pow</code> only) and C99 took another (with many math functions in <code>&lt;tgmath.h&gt;</code>.
The proposed resolution basically says: C++98 got it wrong and C99 got it right; let's go with C99.
</p>
</blockquote>

<p><i>[
Bellevue:
]</i></p>


<blockquote><p>
This signature was not picked up from C99. Instead, if one types
<code>pow(2.0f,2)</code>, the promotion rules will invoke "double pow(double,
double)", which generally gives special treatment for integral
exponents, preserving full accuracy of the result.  New proposed
wording provided.
</p></blockquote>


<p id="res-550"><b>Proposed resolution:</b></p>
<p>
Change 29.7 <a href="https://wg21.link/c.math">[c.math]</a> p10:
</p>

<blockquote>
<p>
The added signatures are:
</p>
<blockquote><pre>
...
<del>float pow(float, int);</del>
...
<del>double pow(double, int);</del>
...
<del>long double pow(long double, int);</del>
</pre></blockquote>
</blockquote>






</body>
</html>
