C语言 使用 M_PI 和 C89 标准
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5007925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using M_PI with C89 standard
提问by robintw
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_DEFINESto my code (see example code below). This works fine when compiling normally, but I need to be able to compile with the std=c89flag for the work that I'm doing.
我正在使用 C 并试图访问常量 M_PI (3.14159 ...)。我已经导入了 math.h 头文件,但 M_PI 常量仍未定义。通过在 StackOverflow 上的一些搜索,我发现我需要添加#define _USE_MATH_DEFINES到我的代码中(参见下面的示例代码)。这在正常编译时工作正常,但我需要能够使用std=c89我正在做的工作的标志进行编译。
How should I access M_PI from some C89 code?
我应该如何从一些 C89 代码访问 M_PI?
回答by eq-
A conforming standard library file math.his not only not required to, but actually must notdefine M_PIby default. In this context 'by default' means that M_PImust only get defined through compiler-specific tricks, most often undefined behavior through the use of reserved identifiers.
一个符合标准的库文件math.h不仅不需要,而且实际上也不能M_PI默认定义。在这种情况下,“默认”意味着M_PI只能通过特定于编译器的技巧来定义,最常见的是通过使用保留标识符来定义未定义的行为。
Just define the constant yourself (you can use the name M_PIfreely, but should you want to be able to compile the code with a non-conforming compiler, you must first check that M_PIis not already defined). For convention's sake, do not define M_PIas anything other than (the approximation of) pi.
只需自己定义常量(您可以M_PI随意使用该名称,但如果您希望能够使用不符合标准的编译器编译代码,您必须首先检查M_PI尚未定义的)。为了约定俗成,不要定义M_PI为 pi(的近似值)以外的任何东西。
回答by Sven Marnach
I would go for
我会去
#ifndef M_PI
# define M_PI 3.14159265358979323846
#endif
回答by Matteo Italia
M_PIis 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 #definefor it, last time I checked it was a universal constant so there's not much space for confusion. :)
M_PI不是 C 标准所要求的,它只是一个常见的扩展,所以如果你想成为标准,你不应该依赖它。然而,你可以很容易地定义你自己#define的,上次我检查它是一个通用常量,所以没有太多混淆的空间。:)
回答by Clifford
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.
我看不出这里有什么问题;-std=c89 和 _USE_MATH_DEFINES 之间没有不兼容,一个定义编译器将编译的语言,另一个定义 math.h 的哪些部分被启用。
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.
启用的那些部分未定义为 ISO C 标准库的一部分,但这与不是标准 C 语言不同,语言和库在 C 中是独立的实体。它与 C89 的兼容性不亚于它本来的样子如果您在自己的标题中定义了自己的宏。
I would however suggest that you define the macro on the command-line rather than in the code:
但是,我建议您在命令行而不是代码中定义宏:
-std=c89 -D_USE_MATH_DEFINES
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 的 math.h 实现,那么通过类似地使用命令行定义的宏,无需修改代码即可轻松修复:
-std=c89 -DM_PI=3.14159265358979323846

