问题描述
我的构建系统MacOS 10.6.3上用于POSIX数学库的接口是math.h,但是在我的目标系统上,接口文件的名称是cmath.h。在学校,我们使用cmath,我想确保我的项目编译时,当交付,这是如何实现。学校的服务器和工作站是运行Windows XP的x86。
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.
推荐答案
在C ++标准中,数学库函数定义在两个头文件中:
In the C++ standard, the math library functions are defined in two headers:
<cmath>
在命名空间 std
std :: sin
),而
<math.h>
在全局命名空间中包含它们(因此只需 sin
)。
contains them in the global namespace (so just sin
).
两者之间还有其他区别:while < math.h>
C
There are further differences between the two: while <math.h>
contains all the C math functions with distinct names for distinct types, such as
double sin(double);
float sinf(float);
long double sinl(long double);
等,< cmath>
重载函数,例如
namespace std {
double sin(double);
float sin(float);
long double sin(long double);
}
(C ++库可能还会从< cmath>
导出 sinf
,但是你不能依赖于程序。)
etc. (C++ libraries might additionally export sinf
from <cmath>
, but you can't rely on this in a portable program.)
最后, fabs
, fabsf
并且来自C标准库的 fabsl
函数已成为中的
。 std :: abs
; cmath>
Finally, the fabs
, fabsf
and fabsl
functions from the C standard library have become overloads of std::abs
in <cmath>
.
虽然两个标头都在标准中,但你应该更倾向于< cmath> code>,因为
< math.h>
只是为了与预标准C ++和C向后兼容。
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.
在标准C ++中没有< cmath.h>
。
There's no such thing as <cmath.h>
in standard C++.
这篇关于数学界面vs cMath在C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!