Missing std::sign

2,609 views
Skip to first unread message

Matthew Fioravante

unread,
Aug 21, 2014, 1:50:17 PM8/21/14
to std-pr...@isocpp.org
The math library is missing the signum function.

//Returns 1 if x > 0, 0 if x == 0, or -1 if x < 0
template <typename T> constexpr T sign(T x);

Here are a whole bunch of people arguing about ways to implement it. Be nice if there was a standard version with optimal efficiency on each platform.
http://stackoverflow.com/questions/1903954/is-there-a-standard-sign-function-signum-sgn-in-c-c

It is possible to add new functions to the <cmath> header without getting them in the C standard first?

Howard Hinnant

unread,
Aug 21, 2014, 2:03:29 PM8/21/14
to std-pr...@isocpp.org
Yes, as long as the spec clearly states that the signature is not guaranteed to be available from <math.h>. Unofficially, but from a practical standpoint, we own <cmath>, and the C committee owns <math.h>. Whenever either of the two committees tries to specify the other’s header (and both committees have attempt to do so in the past), a little bit more chaos escapes into the universe, and is nearly impossible to recapture. To this day we suffer from such attempts in the past.

Howard

Gabriel Dos Reis

unread,
Aug 23, 2014, 4:59:58 PM8/23/14
to std-pr...@isocpp.org
Howard Hinnant <howard....@gmail.com> writes:

| guaranteed to be available from <math.h>. Unofficially, but from a
| practical standpoint, we own <cmath>, and the C committee owns
| <math.h>. Whenever either of the two committees tries to specify the
| other's header (and both committees have attempt to do so in the
| past), a little bit more chaos escapes into the universe, and is
| nearly impossible to recapture. To this day we suffer from such
| attempts in the past.

Amen to that.

Furthermore, LWG should stop Pavlovian reflexive adoption of everything
WG14 invents as <cxxx>. Case in point: <cstdalign>.

-- Gaby

Matthew Fioravante

unread,
Aug 27, 2014, 8:13:28 PM8/27/14
to std-pr...@isocpp.org, g...@axiomatics.org
Maybe it would make sense to have a C++ <math> header which includes <cmath> and also has C++ specific math functions? Similarly for other headers. The header <cmath> sounds like a C compatibility header, not the primary header for the C++ math library.

Howard Hinnant

unread,
Aug 27, 2014, 9:19:26 PM8/27/14
to std-pr...@isocpp.org, g...@axiomatics.org
On Aug 27, 2014, at 8:13 PM, Matthew Fioravante <fmatth...@gmail.com> wrote:

> Maybe it would make sense to have a C++ <math> header which includes <cmath> and also has C++ specific math functions? Similarly for other headers. The header <cmath> sounds like a C compatibility header, not the primary header for the C++ math library.

No, just put the C++ specific math functions in <cmath>, and then explicitly state that they do not appear in <math.h> (unless the C committee says otherwise).

We don’t need more headers. And there is no problem in the C++ committee adding functionality to <cmath>. We only get in trouble when the C++ committee adds (or subtracts) functionality to <math.h>, or when the C committee adds (or subtracts) functionality to <cmath>.

Howard

Gabriel Dos Reis

unread,
Aug 28, 2014, 7:52:51 AM8/28/14
to Howard Hinnant, std-pr...@isocpp.org
Howard Hinnant <howard....@gmail.com> writes:

| On Aug 27, 2014, at 8:13 PM, Matthew Fioravante <fmatth...@gmail.com> wrote:
|
| > Maybe it would make sense to have a C++ <math> header which includes
| > <cmath> and also has C++ specific math functions? Similarly for
| > other headers. The header <cmath> sounds like a C compatibility
| > header, not the primary header for the C++ math library.
|
| No, just put the C++ specific math functions in <cmath>, and then
| explicitly state that they do not appear in <math.h> (unless the C
| committee says otherwise).

Agreed. We already have too many C++ headers that do "math" stuff and I
always forget which one to include -- <cmath>, <cstdlib>, <numerics>,
<algorithm>, <complex>, <valarray>, etc.

| We don’t need more headers. And there is no problem in the C++
| committee adding functionality to <cmath>. We only get in trouble
| when the C++ committee adds (or subtracts) functionality to <math.h>,
| or when the C committee adds (or subtracts) functionality to <cmath>.
|
| Howard

-- Gaby
Reply all
Reply to author
Forward
0 new messages