cmath头混乱(cmath header confusion)

数学函数的命名空间是什么? 全球还是std?

考虑cos函数。 它有3个重载。 但是math.h也有传统的cos 。 C对函数重载一无所知。 因此cos(x)不能解析为cos(float) 。 解决方案是显式调用单精度版本cosf(x) 。 我错过了什么吗?

谢谢。

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?

最满意答案

通过在C ++中包含<math.c>和<cmath>可以获得相同的功能,唯一的区别是命名空间。 包括<math.h> IE也给你带来了过载。

从理论上讲,在C ++ 03中,使用<math.h>为您提供全局名称空间和std名称空间中定义的符号,同时使用<cmath>可为您提供std名称空间中定义的符号,而不是全局名称空间中定义的符号。

实践是不同的,C ++ 0X将理论与实践相结合。 <math.h>为您提供在全局名称空间中定义的符号,也可能在std名称空间中定义符号,同时使用<cmath>可为您提供在std名称空间中定义的符号,也可能在全局名称空间中定义符号。

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.

cmath头混乱(cmath header confusion)

数学函数的命名空间是什么? 全球还是std?

考虑cos函数。 它有3个重载。 但是math.h也有传统的cos 。 C对函数重载一无所知。 因此cos(x)不能解析为cos(float) 。 解决方案是显式调用单精度版本cosf(x) 。 我错过了什么吗?

谢谢。

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?

最满意答案

通过在C ++中包含<math.c>和<cmath>可以获得相同的功能,唯一的区别是命名空间。 包括<math.h> IE也给你带来了过载。

从理论上讲,在C ++ 03中,使用<math.h>为您提供全局名称空间和std名称空间中定义的符号,同时使用<cmath>可为您提供std名称空间中定义的符号,而不是全局名称空间中定义的符号。

实践是不同的,C ++ 0X将理论与实践相结合。 <math.h>为您提供在全局名称空间中定义的符号,也可能在std名称空间中定义符号,同时使用<cmath>可为您提供在std名称空间中定义的符号,也可能在全局名称空间中定义符号。

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.