使用 M_PI 和 C89 标准 [英] Using M_PI with C89 standard
问题描述
我正在使用 C 并试图访问常量 M_PI (3.14159...).我已导入 math.h 头文件,但 M_PI 常量仍未定义.通过在 StackOverflow 上的一些搜索,我发现我需要将 #define _USE_MATH_DEFINES
添加到我的代码中(参见下面的示例代码).这在正常编译时工作正常,但我需要能够使用 std=c89
标志来编译我正在做的工作.
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.
我应该如何从一些 C89 代码访问 M_PI?
How should I access M_PI from some C89 code?
推荐答案
符合标准的标准库文件 math.h
不仅不需要,而且实际上不能默认定义 M_PI
.在这种情况下,默认"意味着 M_PI
只能通过特定于编译器的技巧来定义,最常见的是通过使用保留标识符来定义未定义的行为.
A conforming standard library file math.h
is not only not required to, but actually must not define M_PI
by default. In this context 'by default' means that M_PI
must only get defined through compiler-specific tricks, most often undefined behavior through the use of reserved identifiers.
只需自己定义常量(您可以随意使用名称M_PI
,但如果您希望能够使用不符合标准的编译器编译代码,则必须首先检查M_PI
尚未定义).为了约定俗成,不要将 M_PI
定义为 pi(的近似值)以外的任何东西.
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 that M_PI
is not already defined). For convention's sake, do not define M_PI
as anything other than (the approximation of) pi.
这篇关于使用 M_PI 和 C89 标准的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!