objc中的“pi”和“M_PI”有什么区别

时间:2022-07-06 07:23:00

Including some math in my code I stumbled over the constant "PI". At least in my Xcode version 4.6 I could use either one. But what is the difference between pi and M_PI? The documentation is little bit tight on that topic.

在我的代码中包含了一些数学运算,我被常数“PI”给绊倒了。至少在我的Xcode版本4.6中,我可以使用任何一个。但是pi和M_PI之间有什么区别呢?这个主题的文档有点紧。

2 个解决方案

#1


23  

pi is defined in the "CarbonCore.framework" headers as

pi在“CarbonCore.framework”头文件中定义为

extern const double_t pi  __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_0, __MAC_10_8, __IPHONE_NA, __IPHONE_NA);

but marked as "deprecated". I assume that it is a relict from older Carbon frameworks.

但是标记为“弃用”。我认为这是旧的碳结构的残余。

M_PI is defined as a macro

M_PI被定义为一个宏。

#define M_PI   3.14159265358979323846264338327950288

in math.h and part of the POSIX standard.

在数学。h和POSIX标准的一部分。

The values are identical, but you should use M_PI for portability reasons.

这些值是相同的,但是出于可移植性的考虑,应该使用M_PI。

(And for Swift, see How to get mathemical PI constant in Swift.)

(对于Swift来说,看看如何快速获得数学PI常数。)

#2


0  

M_PI is close enough to approximate the circumference of the galaxy (and probably the whole Universe) within a few miles, so I wouldn't lose sleep over it.

M_PI足够接近银河系(可能还有整个宇宙)的周长,在几英里之内,所以我不会因此而失眠。

#1


23  

pi is defined in the "CarbonCore.framework" headers as

pi在“CarbonCore.framework”头文件中定义为

extern const double_t pi  __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_0, __MAC_10_8, __IPHONE_NA, __IPHONE_NA);

but marked as "deprecated". I assume that it is a relict from older Carbon frameworks.

但是标记为“弃用”。我认为这是旧的碳结构的残余。

M_PI is defined as a macro

M_PI被定义为一个宏。

#define M_PI   3.14159265358979323846264338327950288

in math.h and part of the POSIX standard.

在数学。h和POSIX标准的一部分。

The values are identical, but you should use M_PI for portability reasons.

这些值是相同的,但是出于可移植性的考虑,应该使用M_PI。

(And for Swift, see How to get mathemical PI constant in Swift.)

(对于Swift来说,看看如何快速获得数学PI常数。)

#2


0  

M_PI is close enough to approximate the circumference of the galaxy (and probably the whole Universe) within a few miles, so I wouldn't lose sleep over it.

M_PI足够接近银河系(可能还有整个宇宙)的周长,在几英里之内,所以我不会因此而失眠。