使用M_PI和C89标准

时间:2022-04-20 20:57:27

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 * 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.

我正在使用C并尝试访问常量M_PI(3.14159 ...)。我已导入math.h头文件,但M_PI常量仍未定义。通过对*的一些搜索,我发现我需要在代码中添加#define _USE_MATH_DEFINES(参见下面的示例代码)。这在正常编译时工作正常,但我需要能够使用std = c89标志进行编译以完成我正在进行的工作。

How should I access M_PI from some C89 code?

我应该如何从某些C89代码访问M_PI?

4 个解决方案

#1


37  

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.

符合标准的库文件math.h不仅不需要,但实际上默认情况下不能定义M_PI。在此上下文中“默认情况下”意味着M_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,但是如果您希望能够使用不符合的编译器编译代码,则必须首先检查M_PI是否尚未定义)。为了惯例,不要将M_PI定义为pi的近似值(近似值)。

#2


24  

I would go for

我会去的

#ifndef M_PI
#    define M_PI 3.14159265358979323846
#endif

#3


9  

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. :)

C标准不要求M_PI,它只是一个常见的扩展,所以如果你想成为标准,你就不应该依赖它。但是,您可以轻松地为它定义自己的#define,上次我检查它是一个通用常量,因此没有太多的混淆空间。 :)

#4


8  

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一致,并不符合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

#1


37  

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.

符合标准的库文件math.h不仅不需要,但实际上默认情况下不能定义M_PI。在此上下文中“默认情况下”意味着M_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,但是如果您希望能够使用不符合的编译器编译代码,则必须首先检查M_PI是否尚未定义)。为了惯例,不要将M_PI定义为pi的近似值(近似值)。

#2


24  

I would go for

我会去的

#ifndef M_PI
#    define M_PI 3.14159265358979323846
#endif

#3


9  

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. :)

C标准不要求M_PI,它只是一个常见的扩展,所以如果你想成为标准,你就不应该依赖它。但是,您可以轻松地为它定义自己的#define,上次我检查它是一个通用常量,因此没有太多的混淆空间。 :)

#4


8  

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一致,并不符合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