<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2294: &lt;cstdlib&gt; should declare abs(double)</title>
<meta property="og:title" content="Issue 2294: &lt;cstdlib&gt; should declare abs(double)">
<meta property="og:description" content="C++ library issue. Status: Resolved">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2294.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#Resolved">Resolved</a> status.</em></p>
<h3 id="2294"><a href="lwg-defects.html#2294">2294</a>. <code>&lt;cstdlib&gt;</code> should declare <code>abs(double)</code></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#Resolved">Resolved</a>
 <b>Submitter:</b> Pete Becker <b>Opened:</b> 2013-09-04 <b>Last modified:</b> 2017-03-12</p>
<p><b>Priority: </b>2
</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#Resolved">Resolved</a> status.</p>
<p><b>Discussion:</b></p>
<p>
&hellip; and <code>abs(float)</code> and <code>abs(long double)</code>. And <code>&lt;cmath&gt;</code> should declare 
<code>abs(int)</code>, <code>abs(long)</code>, and <code>abs(long long)</code>.
<p/>
As things currently stand, this program is illegal:
</p>
<blockquote><pre>
#include &lt;cstdlib&gt;

int main() {
  double d = -1.23;
  double dd = std::abs(d);
  return 0;
}
</pre></blockquote>
<p>
The call is ambiguous because of the various integer overloads, that's because <code>&lt;cstdlib&gt;</code> provides 
<code>abs(int)</code> but not <code>abs(double)</code>.
<p/>
This lead one commenter on Stackoverflow to state that <code>abs</code> is dangerous, and to recommend using <code>fabs</code> instead.
<p/>
In general, it makes sense to declare overloaded functions that take user-defined types in the same header as the 
definition of the user-defined types; it isn't necessary to declare all of the overloads in the same place. But 
here we're not dealing with any user-defined types; we're dealing with builtin types, which are always defined; 
all of the overloads should be defined in the same place, to avoid mysterious problems like the one in the code above.
<p/>
The standard library has six overloads for <code>abs</code>:
</p>
<blockquote><pre>
int abs(int);  // &lt;cstdlib&gt;
long abs(long); // &lt;cstdlib&gt;
long long abs(long long); // &lt;cstdlib&gt;

float abs(float); // &lt;cmath&gt;
double abs(double); // &lt;cmath&gt;
long double abs(long double); // &lt;cmath&gt;
</pre></blockquote>
<p>
These should all be declared in both headers.
<p/>
I have no opinion on <code>&lt;stdlib.h&gt;</code> and <code>&lt;math.h&gt;</code>.
</p>

<p><i>[2013-09 Chicago]</i></p>

<p>
This issue is related to LWG <a href="lwg-defects.html#2192" title="Validity and return type of std::abs(0u) is unclear (Status: C++17)">2192</a><sup><a href="https://cplusplus.github.io/LWG/issue2192" title="Latest snapshot">(i)</a></sup>
<p/>
Move to open
</p>

<p><i>[2014-02-13 Issaquah &mdash; Nicolai Josuttis suggest wording]</i></p>


<p><i>[2015-03-03, Geoffrey Romer provides improved wording]</i></p>

<p>
See proposed resolution of LWG <a href="lwg-defects.html#2192" title="Validity and return type of std::abs(0u) is unclear (Status: C++17)">2192</a><sup><a href="https://cplusplus.github.io/LWG/issue2192" title="Latest snapshot">(i)</a></sup>.
</p>

<p><i>[2015-09-11, Telecon]</i></p>

<p>Geoff provided combined wording for <a href="lwg-defects.html#2192" title="Validity and return type of std::abs(0u) is unclear (Status: C++17)">2192</a><sup><a href="https://cplusplus.github.io/LWG/issue2192" title="Latest snapshot">(i)</a></sup> after Cologne, Howard to provide updated wording for Kona.</p>
<p>Howard: my notes say I wanted to use <code>is_unsigned</code> instead of 'unsigned integral type'.</p>

<strong>Previous resolution from Nicolai [SUPERSEDED]:</strong>
<blockquote class = "note">
<ol>
<li><p>Edit 29.7 <a href="https://wg21.link/c.math">[c.math]</a> after p7 as indicated:</p>

<blockquote><p>
-6- In addition to the <code>int</code> versions of certain math functions in <code>&lt;cstdlib&gt;</code>, C++ adds <code>long</code> and <code>long long</code>
overloaded versions of these functions, with the same semantics.
<p/>
-7- The added signatures are:
</p>
<blockquote><pre>
long abs(long);                    <i>// labs()</i>
long long abs(long long);          <i>// llabs()</i>
ldiv_t div(long, long);            <i>// ldiv()</i>
lldiv_t div(long long, long long); <i>// lldiv()</i>
</pre></blockquote>
<p>
<ins>-?- To avoid ambiguities, C++ also adds the following overloads of <code>abs()</code> to <code>&lt;cstdlib&gt;</code>,
with the semantics defined in <code>&lt;cmath&gt;</code>:</ins>
</p>
<blockquote><pre>
<ins>float abs(float);
double abs(double);
long double abs(long double);</ins>
</pre></blockquote>
<p>
<ins>-?- To avoid ambiguities, C++ also adds the following overloads of <code>abs()</code> to <code>&lt;cmath&gt;</code>,
with the semantics defined in <code>&lt;cstdlib&gt;</code>:</ins>
</p>
<blockquote><pre>
<ins>int abs(int);
long abs(long);
long long abs(long long);</ins>
</pre></blockquote>
</blockquote>
</li>
</ol>
</blockquote>

<p><i>[2015-08 Chicago]</i></p>

<p>Resolved by <a href="lwg-defects.html#2192" title="Validity and return type of std::abs(0u) is unclear (Status: C++17)">2192</a><sup><a href="https://cplusplus.github.io/LWG/issue2192" title="Latest snapshot">(i)</a></sup></p>


<p id="res-2294"><b>Proposed resolution:</b></p>
<p>
See proposed resolution of LWG <a href="lwg-defects.html#2192" title="Validity and return type of std::abs(0u) is unclear (Status: C++17)">2192</a><sup><a href="https://cplusplus.github.io/LWG/issue2192" title="Latest snapshot">(i)</a></sup>.
</p>





</body>
</html>
