[英]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
調用和 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
問題:對於轉換說明符%a
,如何:
例如,為什么是0x1.2p-1070
而不是0x0.0000000000012p-1022
(或其他變體)(反之亦然)?
C 在細節上允許一些自由度
表示浮點數的雙精度參數轉換為 [-] 0xh.hhhhp ±d 樣式,其中在小數點字符及其后的十六進制位數等於精度; 如果缺少精度並且 FLT_RADIX 是 2 的冪,則精度足以精確表示該值......(更多關於 base 10 編碼,Inf 和 NaN)
C2xdr § 7.21.6.1 8
我看到的顯着變化是第一個h
數字是'0'-'F'
還是限制為'0'-'1'
。
為什么是 0x1.2p-1070 而不是 0x0.0000000000012p-1022
對於正常值,前導數字指定為非零。 然而,由於 OP 的值看起來低於正常值,因此兩者都可以接受。 它是未指定的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.