使用符合 C89 标准的 M_PI
我正在使用 C 并尝试访问常量 M_PI (3.14159...)。我已经导入了 math.h 头文件,但 M_PI 常量仍然未定义。通过在 StackOverflow 上的一些搜索,我发现我需要将 #define _USE_MATH_DEFINES
添加到我的代码中(请参阅下面的示例代码)。正常编译时效果很好,但我需要能够使用 std=c89
标志来编译我正在做的工作。
我应该如何从某些 C89 代码访问 M_PI?
I'm using C and trying to get access to the constant M_PI (3.14159...). I have imported the math.h header file, but the M_PI constant was still undefined. Through some searching on StackOverflow I have found that I need to add #define _USE_MATH_DEFINES
to my code (see example code below). This works fine when compiling normally, but I need to be able to compile with the std=c89
flag for the work that I'm doing.
How should I access M_PI from some C89 code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
符合标准的标准库文件
math.h
不仅不需要,而且实际上不得默认定义M_PI
。在这种情况下,“默认”意味着M_PI
必须只能通过编译器特定的技巧来定义,最常见的是通过使用保留标识符来定义未定义的行为。只需自己定义常量即可(您可以自由使用名称
M_PI
,但如果您希望能够使用非兼容编译器编译代码,则必须首先检查M_PI
code> 尚未定义)。为了方便起见,不要将M_PI
定义为 pi(的近似值)以外的任何值。A conforming standard library file
math.h
is not only not required to, but actually must not defineM_PI
by default. In this context 'by default' means thatM_PI
must only get defined through compiler-specific tricks, most often undefined behavior through the use of reserved identifiers.Just define the constant yourself (you can use the name
M_PI
freely, but should you want to be able to compile the code with a non-conforming compiler, you must first check thatM_PI
is not already defined). For convention's sake, do not defineM_PI
as anything other than (the approximation of) pi.我会去
I would go for
M_PI
不是 C 标准所必需的,它只是一个常见的扩展,因此如果您想成为标准,则不应依赖它。但是,您可以轻松地为其定义自己的#define
,上次我检查它是一个通用常量,因此没有太多混淆的空间。 <代码>:)M_PI
is not required by the C standard, it's just a common extension, so if you want to be standard you shouldn't rely on it. However, you can easily define your own#define
for it, last time I checked it was a universal constant so there's not much space for confusion.:)
我不明白这里有什么问题; -std=c89 和 _USE_MATH_DEFINES 之间没有不兼容性,一个定义编译器将编译什么语言,另一个定义启用 math.h 的哪些部分。
那些启用的部分并未定义为 ISO C 标准库的一部分,但这与不是标准 C 语言不同,语言和库在 C 中是独立的实体。与在自己的标头中定义自己的宏相比,C89 的兼容性较差。
不过,我建议您在命令行上而不是在代码中定义宏:
如果您遇到未定义 M_PI 的 math.h 实现,那么通过类似地使用命令行定义的宏,无需修改代码即可轻松修复该问题:
I fail to see what the problem is here; there is no incompatability between -std=c89 and _USE_MATH_DEFINES, one defines what language the compiler will compile, the other defines what parts of math.h get enabled.
Those parts that are enabled are not defined as part of the ISO C standard library, but that is not the same thing as not being standard C language, language and library are separate entities in C. It is no less C89 compliant than it would be if you had defined your own macros in your own header.
I would however suggest that you define the macro on the command-line rather than in the code:
If you ever encounter a math.h implementation that does not define M_PI, then that is easily fixed without code modification by similarly using command line defined macros:
除了缺少
M_PI
定义之外,还记得以下内容仅定义了double
常量。尽管代码中的 3.14159... 适合大约 113 位,但精度通常会变为 53 位,因为它是一个 double 常量。
这对于
long double
对象和数学来说非常重要。声明
long double
常量时请务必使用L
示例输出:
In addition to
M_PI
definition missing, recall that the following only defines adouble
constant.Even though the 3.14159... in code is good for about 113-bits, the precision typically becomes 53-bit as it is a
double
constant.This can be quite important for
long double
objects and math.Be sure to use
L
when declaring along double
constantSample output: