简体   繁体   English

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

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

Consider this simple code (t0.c):考虑这个简单的代码(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

Invocation and output:调用和 output:

# 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

Question: For conversion specifier %a , how:问题:对于转换说明符%a ,如何:

  • the exact format of hexadecimal floating-point constant is selected, and选择了十六进制浮点常量的确切格式,并且
  • the parameters of this hexadecimal floating-point constant are selected?这个十六进制浮点常数的参数选择?

For example, why 0x1.2p-1070 and not 0x0.0000000000012p-1022 (or other variations) (and vise versa)?例如,为什么是0x1.2p-1070而不是0x0.0000000000012p-1022 (或其他变体)(反之亦然)?

C allows some latitude in the details 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;表示浮点数的双精度参数转换为 [-] 0xh.hhhhp ±d 样式,其中在小数点字符及其后的十六进制位数等于精度; 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)如果缺少精度并且 FLT_RADIX 是 2 的幂,则精度足以精确表示该值......(更多关于 base 10 编码,Inf 和 NaN)
C2xdr § 7.21.6.1 8 C2xdr § 7.21.6.1 8

Notable variations I have seen is if the first h digit is '0'-'F' or limited to '0'-'1' .我看到的显着变化是第一个h数字是'0'-'F'还是限制为'0'-'1'


why 0x1.2p-1070 and not 0x0.0000000000012p-1022为什么是 0x1.2p-1070 而不是 0x0.0000000000012p-1022

Leading digit specified as non-zero for normal values.对于正常值,前导数字指定为非零。 Yet as OP's value looks like a sub-normal one, either would have been acceptable.然而,由于 OP 的值看起来低于正常值,因此两者都可以接受。 It is unspecified .它是未指定的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM