pic11
pic11

Reputation: 14943

cmath header confusion

What is the namespace for math functions? Global or std?

Consider cos function. It has 3 overloads. But there is also legacy cos from math.h. C doesn't know anything about function overloading. Therefore cos(x) can't be resolved to cos(float). The solution is to call the single precision version explicitly cosf(x). Did I miss anything?

Upvotes: 5

Views: 2927

Answers (4)

AProgrammer
AProgrammer

Reputation: 52314

You get the same functions by including <math.c> and <cmath> in C++, the only differences is the namespace. I.E. including <math.h> also gives you the overload.

In theory, in C++03, using <math.h> gives you the symbols defined in the global namespace and also in the std namespace while using <cmath> gives you the symbols defined in the std namespace and not in the global namespace.

The practice is different and C++ 0X aligned the theory with the practice. <math.h> gives you the symbols defined in the global namespace and perhaps also in the std namespace while using <cmath> gives you the symbols defined in the std namespace and perhaps also in the global namespace.

Upvotes: 5

paxdiablo
paxdiablo

Reputation: 882028

The cXXX headers place all their stuff in the std namespace. They may also put them in the global namespace but it's not required.

This is from C++0x, the upcoming standard, section D.7:

2/ Every C header, each of which has a name of the form name.h, behaves as if each name placed in the standard library namespace by the corresponding cname header is placed within the global namespace scope. It is unspecified whether these names are first declared or defined within namespace scope (3.3.6) of the namespace std and are then injected into the global namespace scope by explicit using-declarations (7.3.3).

3/ [ Example: The header <cstdlib> assuredly provides its declarations and definitions within the namespace std. It may also provide these names within the global namespace. The header <stdlib.h> assuredly provides the same declarations and definitions within the global namespace, much as in the C Standard. It may also provide these names within the namespace std. —end example ]

This is unchanged from section D.5 from C++03 (it's made more explicit in the newer standard but the effect is the same):

2/ Every C header, each of which has a name of the form name.h, behaves as if each name placed in the Standard library namespace by the corresponding cname header is also placed within the namespace scope of the namespace std and is followed by an explicit using-declaration.

3/ [Example: The header <cstdlib> provides its declarations and definitions within the namespace std. The header <stdlib.h> makes these available also in the global namespace, much as in the C Standard. —end example]

If you include the 'old-style' XXX.h header, it's placed in both namespaces (in both iterations of the standard).

Upvotes: 1

rsachetto
rsachetto

Reputation: 175

If you are using C++ you can rely on the function overloading.

There are three version of cos:

double cos (      double x );
float cos (       float x );
long double cos ( long double x );

But if you are using only C or you want portabilty only the double version of this function exists with this name. The float function is cosf.

Upvotes: 0

eduffy
eduffy

Reputation: 40232

They are in the std namespace. But, for backwards compatibility reasons the cmath header also shows them in the global namespace with a using std::cos;.

Upvotes: 3

Related Questions