The interface on my build system MacOS 10.6.3 for the POSIX math library is math.h, however on my target system the name of the interface file is cmath.h. At school we use cmath and I would like to be sure my project compiles when it is handed in, how is this achieved. The servers and workstations at school are x86 running Windows XP. The GCC is available on both platforms.
Share
In the C++ standard, the math library functions are defined in two headers:
contains them in the namespace
std(e.g.std::sin), whilecontains them in the global namespace (so just
sin).There are further differences between the two: while
<math.h>contains all the C math functions with distinct names for distinct types, such asetc.,
<cmath>contains overloaded functions such asetc. (C++ libraries might additionally export
sinffrom<cmath>, but you can’t rely on this in a portable program.)Finally, the
fabs,fabsfandfabslfunctions from the C standard library have become overloads ofstd::absin<cmath>.Though both headers are in the standard, you should really prefer
<cmath>, as<math.h>is only there for backward compatibility with pre-standard C++ and C.There’s no such thing as
<cmath.h>in standard C++.