M_PI和M_PI_2的区别是什么?

时间:2022-01-20 20:56:56

I forked a project from Github, Xcode shows a lot of warnings:

我从Github上放弃了一个项目,Xcode显示了很多警告:

'M_PI' is deprecated: Please use 'Double.pi' or '.pi' to get the value of correct type and avoid casting.

“M_PI”被弃用:请使用“Double”。π”或“。为了得到正确类型的值,避免铸造。

and

'M_PI_2' is deprecated: Please use 'Double.pi' or '.pi' to get the value of correct type and avoid casting.

“M_PI_2”被弃用:请使用“Double”。π”或“。为了得到正确类型的值,避免铸造。

Since both M_PI and M_PI_2 are prompted to be replaced by Double.pi, I assume there are in fact the same value. However, there's this code in the project:

因为M_PI和M_PI_2都被提示用Double替换。pi,我假设实际上是相同的值。但是,项目中有这样的代码:

switch angle {

    case M_PI_2:
        ...

    case M_PI:
        ...

    case Double.pi * 3:
        ...

    default:
        ...

}

I'm really confused here, are M_PI and M_PI_2 different? or are they just the same?

我很困惑,M_PI和M_PI_2不同吗?还是它们是一样的?

UPDATE:

更新:

It turns our to be my blunder, Xcode says 'M_PI_2' is deprecated: Please use 'Double.pi / 2' or '.pi / 2' to get the value of correct type and avoid casting. so it isn't a bug, just too hard to notice the difference of 2 prompts.

它把我们变成了我的错误,Xcode说“M_PI_2”被弃用了:请使用“Double”。/ 2'或'。pi / 2'以获得正确类型的值,避免铸造。所以它不是一个bug,只是很难注意到两个提示的区别。

3 个解决方案

#1


10  

use 'Double.pi / 2' for M_PI_2 and 'Double.pi' for M_PI

使用“双。对于M_PI_2和'Double。π的M_PI

#2


0  

These constants are related to the implementations of different functions in the math library:

这些常量与数学库中不同函数的实现有关:

s_cacos.c:  __real__ res = (double) M_PI_2 - __real__ y;
s_cacosf.c:  __real__ res = (float) M_PI_2 - __real__ y;
s_cacosh.c:                    ? M_PI - M_PI_4 : M_PI_4)
...
s_clogf.c:      __imag__ result = signbit (__real__ x) ? M_PI : 0.0;

M_PI, M_PI_2, and M_PI_4 show up quite often but there's no 2.0 * M_PI. 2π is just not that useful, at least in implementing libm.

M_PI、M_PI_2和M_PI_4经常出现,但是没有2.0 * M_PI。2π是有用的,至少在实施libm。

M_PI_2 and M_PI_4, their existences are well justified. The documentation of the GNU C library suggests that "these constants come from the Unix98 standard and were also available in 4.4BSD". Compilers were not that smart back at that time. Typing M_PI/4 instead of M_PI_4 may cause an unnecessary division. Although modern compilers can optimize that away (GCC uses mpfr since 2008 so even rounding is done correctly), using numeric constants is still a more portable way to write high-performance code.

M_PI_2和M_PI_4,它们的存在是合理的。GNU C库的文档说明“这些常量来自于Unix98标准,并且在4.4BSD中也可用”。编译器当时并没有那么聪明。键入M_PI/4而不是M_PI_4可能会造成不必要的分隔。尽管现代编译器可以优化这一点(GCC自2008年以来就使用mpfr,因此即使是对其进行优化),使用数字常量仍然是编写高性能代码的一种更轻便的方式。

#3


0  

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标准的一部分。

Follow This You can find reference answer here

根据这个你可以在这里找到参考答案。

Check This Solution - How you can use it

检查这个解决方案-如何使用它。

#1


10  

use 'Double.pi / 2' for M_PI_2 and 'Double.pi' for M_PI

使用“双。对于M_PI_2和'Double。π的M_PI

#2


0  

These constants are related to the implementations of different functions in the math library:

这些常量与数学库中不同函数的实现有关:

s_cacos.c:  __real__ res = (double) M_PI_2 - __real__ y;
s_cacosf.c:  __real__ res = (float) M_PI_2 - __real__ y;
s_cacosh.c:                    ? M_PI - M_PI_4 : M_PI_4)
...
s_clogf.c:      __imag__ result = signbit (__real__ x) ? M_PI : 0.0;

M_PI, M_PI_2, and M_PI_4 show up quite often but there's no 2.0 * M_PI. 2π is just not that useful, at least in implementing libm.

M_PI、M_PI_2和M_PI_4经常出现,但是没有2.0 * M_PI。2π是有用的,至少在实施libm。

M_PI_2 and M_PI_4, their existences are well justified. The documentation of the GNU C library suggests that "these constants come from the Unix98 standard and were also available in 4.4BSD". Compilers were not that smart back at that time. Typing M_PI/4 instead of M_PI_4 may cause an unnecessary division. Although modern compilers can optimize that away (GCC uses mpfr since 2008 so even rounding is done correctly), using numeric constants is still a more portable way to write high-performance code.

M_PI_2和M_PI_4,它们的存在是合理的。GNU C库的文档说明“这些常量来自于Unix98标准,并且在4.4BSD中也可用”。编译器当时并没有那么聪明。键入M_PI/4而不是M_PI_4可能会造成不必要的分隔。尽管现代编译器可以优化这一点(GCC自2008年以来就使用mpfr,因此即使是对其进行优化),使用数字常量仍然是编写高性能代码的一种更轻便的方式。

#3


0  

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标准的一部分。

Follow This You can find reference answer here

根据这个你可以在这里找到参考答案。

Check This Solution - How you can use it

检查这个解决方案-如何使用它。