尝试访问 long double Math 宏和 long double 的精度

Trying to access the long double Math macros and the precision of long double

首先,我试图访问 Math.h 上定义的这些长双精度宏。

/*  Long-double versions of M_E, etc for convenience on Intel where long-
    double is not the same as double.  Define __MATH_LONG_DOUBLE_CONSTANTS
    to make these constants available.                                        */
#if defined __MATH_LONG_DOUBLE_CONSTANTS
#define M_El        0xa.df85458a2bb4a9bp-2L
#define M_LOG2El    0xb.8aa3b295c17f0bcp-3L
#define M_LOG10El   0xd.e5bd8a937287195p-5L
#define M_LN2l      0xb.17217f7d1cf79acp-4L
#define M_LN10l     0x9.35d8dddaaa8ac17p-2L
#define M_PIl       0xc.90fdaa22168c235p-2L
#define M_PI_2l     0xc.90fdaa22168c235p-3L
#define M_PI_4l     0xc.90fdaa22168c235p-4L
#define M_1_PIl     0xa.2f9836e4e44152ap-5L
#define M_2_PIl     0xa.2f9836e4e44152ap-4L
#define M_2_SQRTPIl 0x9.06eba8214db688dp-3L
#define M_SQRT2l    0xb.504f333f9de6484p-3L
#define M_SQRT1_2l  0xb.504f333f9de6484p-4L
#endif /* defined __MATH_LONG_DOUBLE_CONSTANTS */

我已将此添加到我的开头 class:

#define __MATH_LONG_DOUBLE_CONSTANTS
#import <math.h>

Apple 表示有必要公开宏,例如 M_PIl(long double PI)。

我尝试使用 M_PIl 并收到此消息:

use of undeclared identifier 'M_PIl', did you mean 'P_PID'?

Apple 在 GCC4 上将 long double 定义为 128 位数字,我怀疑在 LLDB 上也是如此,他们还说这些数字可以表示 3.36210314311209350626 E-49321.18973149535723176502 E4932 之间的数字。如果我的数学没记错的话,这是尾数符号使用1位,指数符号使用1位,指数使用16位,所以尾数必须有110位。

为了测试这个我做了

long double pi = acosl(-1.0L);
NSLog(@"%.200Lg", pi);

这是打印在控制台上的内容

3.14159265358979323851280895940618620443274267017841339111328125 

也就是64个字符,算上点。

那我试试这个

NSLog(@"%.200Lg", M_PI);

这是打印出来的

3.141592653589793115997963468544185161590576171875

有 52 个字符。

首先奇怪的是 M_PI 和 long double 版本在字符方面的区别。我期待两倍的字符数。

另一件事是Apple将M_PI定义为

#define M_PI    3.14159265358979323846264338327950288

这与上一条命令打印的内容相去甚远。

我也试过手动定义 PI 到 1000 位小数,看看会发生什么...

long double pi = 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989L;

当我使用 NSLog 进行控制台时,结果与之前相同...

3.141592653589793115997963468544185161590576171875

再次,数字开始与第 16 位小数不同...

另外,当我这样做时

NSInteger x = sizeof(long double);

我获得16(???)

你们能解释一下为什么会出现这些差异吗?我如何获得 long double PI 并使用在 Math.h 上定义的宏的 long double 版本?

如果我对 math.h:

的理解正确的话,基本上可以归结为这个

"There is a 20-digit value value guarded by #if defined __USE_BSD || defined __USE_XOPEN. The constants in those groups are not permitted by the C standard to be defined in strict standard conforming mode.

虽然第一部分对于 macOS 并不完全相同,但前提是;提到的第二件事意味着 #define 无法基于使用 and/or 严格标准符合模式的 C/C++ 方言进行访问。

https://software.intel.com/en-us/forums/intel-cilk-plus/topic/265759

在您的 .m 中包含定义似乎是前面评论中讨论的解决方案:

#define __MATH_LONG_DOUBLE_CONSTANTS
#import <math.h>
/*  Long-double versions of M_E, etc for convenience on Intel where long-
    double is not the same as double.  Define __MATH_LONG_DOUBLE_CONSTANTS
    to make these constants available.                                        */
#if defined __MATH_LONG_DOUBLE_CONSTANTS
#define M_El        0xa.df85458a2bb4a9bp-2L
#define M_LOG2El    0xb.8aa3b295c17f0bcp-3L
#define M_LOG10El   0xd.e5bd8a937287195p-5L
#define M_LN2l      0xb.17217f7d1cf79acp-4L
#define M_LN10l     0x9.35d8dddaaa8ac17p-2L
#define M_PIl       0xc.90fdaa22168c235p-2L
#define M_PI_2l     0xc.90fdaa22168c235p-3L
#define M_PI_4l     0xc.90fdaa22168c235p-4L
#define M_1_PIl     0xa.2f9836e4e44152ap-5L
#define M_2_PIl     0xa.2f9836e4e44152ap-4L
#define M_2_SQRTPIl 0x9.06eba8214db688dp-3L
#define M_SQRT2l    0xb.504f333f9de6484p-3L
#define M_SQRT1_2l  0xb.504f333f9de6484p-4L
#endif /* defined __MATH_LONG_DOUBLE_CONSTANTS */

If my math is not wrong, this is using 1 bit for the mantissa sign, 1 bit for the exponent sign, 16 bits for the exponent, so the mantissa must have 110 bits.

嗯,你的数学很好,你的假设是错误的。 Long double 倾向于使用 16 字节来实现以适应各种不同的扩展精度格式,其中包括 128 位 IEEE 和 80 位 Intel。

不用猜那些80位是怎么用的,就look it up.

如果您想使用 #define 常量,请将它们从 math.h 中复制出来,并将它们添加到您的代码中,并受 #ifndef:

保护
#ifndef M_PIl
 ...
#define M_PIl       0xc.90fdaa22168c235p-2L
 ...
#endif

现在使用 M_PIlacosl() 使用 %LA hex-fp 格式进行测试,以便您可以看到十六进制的所有位:

long double ld;
ld = acosl(-1.0L);
printf("ld = %LA\n", ld);
ld = M_PIl;
printf("ld = %LA\n", ld);

您将获得:

ld = 0XC.90FDAA22168C235P-2
ld = 0XC.90FDAA22168C235P-2

M_PIl的定义完全相同,尾数为16进制,64位。

HTH