printf("%a"): 十六进制浮点常量的格式和参数是如何选择的?

printf("%a"): how the format and parameters of hexadecimal floating-point constant are selected?

考虑这个简单的代码 (t0.c):

#include <stdio.h>
#include <float.h>

#if DBL_HAS_SUBNORM == 1
double d = 0x23d804860c09bp-1119;

int main(void)
{
    printf("%a\n", d);
    return 0;
}
#endif

调用和输出:

# host: CPU: Intel, OS: Windows 10
$ gcc t0.c -std=c11 && ./a.exe
0x1.2p-1070

# host: CPU: Intel, OS: Windows 10
$ clang t0.c -std=c11 && ./a.exe
0x1.2p-1070

# host: CPU: Intel, OS: Linux
$ gcc t0.c -std=c11 && ./a.out
0x0.0000000000012p-1022

# host: CPU: Intel, OS: Linux
$ clang t0.c -std=c11 && ./a.out
0x0.0000000000012p-1022

问题:对于转换说明符 %a,如何:

例如,为什么 0x1.2p-1070 而不是 0x0.0000000000012p-1022(或其他变体)(反之亦然)?

C 允许在细节上有一定的自由度

A double argument representing a floating-point number is converted in the style [-]0xh.hhhhp±d, where there is one hexadecimal digit (which is nonzero if the argument is a normalized floating-point number and is otherwise unspecified) before the decimal-point character and the number of hexadecimal digits after it is equal to the precision; if the precision is missing and FLT_RADIX is a power of 2, then the precision is sufficient for an exact representation of the value ... (more concerning base 10 encodings, Inf and NaN)
C2xdr § 7.21.6.1 8

我看到的显着变化是第一个 h 数字是 '0'-'F' 还是仅限于 '0'-'1'


why 0x1.2p-1070 and not 0x0.0000000000012p-1022

正常值的前导数字指定为非零。然而,由于 OP 的价值看起来低于正常水平,因此两者都可以接受。 未指定