Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is fabsf part of the std namespace in C++11?

Tags:

c++

c++11

cmath

The page https://en.cppreference.com/w/cpp/numeric/math/fabs mentions that std::fabsf is available since C++11. However, when I use G++ 6.3.0 to compile even the simplest program that uses std::fabsf, it says that fabsf is not a member of std.

#include <cmath>
int main()
{
    return (int)std::fabsf(0.0f);
}

Which one is right? Is G++ 6.3.0 wrong in not including it in std, or is the above page wrong in mentioning it as part of std in C++11?

And if it's G++ that is wrong, is that fixed in later versions?

like image 999
Pedro Gimeno Avatar asked Jan 03 '19 18:01

Pedro Gimeno


People also ask

What is Fabsf in C?

DESCRIPTION. The fabs() function computes the absolute value of a floating-point number x. The fabsf() function is a single-precision version of fabs().

Is sqrt in std namespace?

'sqrt' is not a member of 'std'

What does std :: abs do?

std::abs(float), std::fabs, std::fabsf, std::fabsl. 1-8) Computes the absolute value of a floating point value arg .

Whats is fabs in C++?

C++ fabs() The fabs() function in C++ returns the absolute value of the argument. It is defined in the cmath header file.


2 Answers

It looks like cppreference is incorrect. It appears this was added for C++17 since it was added to the draft in 2016 with the title [numerics] Apply P0175 (C Synopses) and we can see p0175r1 does indeed add:

 float fabsf(float x);

The libc++ status does not indicate a status for p0175r1 so that would indicate that it does not support these changes yet. I can't find a line item for the proposal in tjhe libstdc++ status page.

like image 93
Shafik Yaghmour Avatar answered Oct 07 '22 16:10

Shafik Yaghmour


Yes, fabsf and all other -f/-l functions from math.h is part of the std namespace via cmath in C++11. It was added in about 2002, when C++0x was rebased on top of the C99 standard library, which made [c.math]/4 include those new functions.

[c.math]/4

The contents of these headers are the same as the Standard C library headers <math.h> and <stdlib.h> respectively, with the following changes:

(historical note: the intent to add all the -f/-l variants was already apparent in C++03, see LWG289)

However, the table listing the contents of cmath was overlooked until 2016, when p0175r1 fixed all such tables to bring them in line with the standard.

p0175r1

Impact on the standard

The change is purely editorial.

like image 28
Cubbi Avatar answered Oct 07 '22 18:10

Cubbi