# <cmath> and namespace std



## kz97 (Oct 3, 2013)

The following snippet does not compile with g++ v4.8.2:


```
#include <cmath>
int main()
{
  double m = std::fmax(2., 8.);
  m = std::abs(2.);
}
```

The command `g++48 -std=c++11 asd.cpp` gives this error:


```
asd.cpp: In function 'int main()':
asd.cpp:6:14: error: 'fmax' is not a member of 'std'
   double m = std::fmax(2., 8.);
              ^
asd.cpp:6:14: note: suggested alternative:
In file included from /usr/local/lib/gcc48/include/c++/cmath:44:0,
                 from asd.cpp:2:
/usr/include/math.h:270:8: note:   'fmax'
 double fmax(double, double) __pure2;
        ^
```

It seems, that abs is a member of the namespace std, but fmax is not. The version of the g++ is 
	
	



```
g++48 (FreeBSD Ports Collection) 4.8.2 20130919 (prerelease)
```

I also have installed g++ v4.2.1 and v4.6.3. g++ v4.8.1 in Arch Linux and in MinGW compiles this code without errors. How should I fix this problem?


----------



## expl (Oct 4, 2013)

Well it seems like the overloading function 
	
	



```
double fabs (T x);
```
 was not ported/implemented on our C++11 library, so just use the C version directly.


----------



## kz97 (Oct 4, 2013)

I was more hoping that the problem may be in multiple interfering compiler versions and has therefore some simple solution. Could someone with lang/gcc48 try to compile the code in the first post and confirm that the other installations have the same problem?


----------



## nslay (Oct 6, 2013)

Unless you need to deal with NaN safely, you could just use std::max from <algorithm> instead.


```
#include <algorithm>

int main(int argc, char **argv) {
  double m = std::max(2.0, 8.0);
  return 0;
}
```


----------



## kz97 (Oct 6, 2013)

std::max is really a good alternative for std::fmax, but there are also other math functions missing from the std napespace . It seems, that @expl was right, and the problem is with the C++11 library, as there is already a patch: http://www.freebsd.org/cgi/query-pr.cgi?pr=182657.


----------



## fonz (Oct 6, 2013)

FWIW:

```
[CMD=%]clang++ snippet.cc[/cmd]
snippet.cc:4:19: error: no member named 'fmax' in namespace 'std'
  double m = std::fmax(2., 8.);
             ~~~~~^
1 error generated.
```
It's not the compiler(s), it's the library.


----------

