【发布时间】:2022-01-05 13:08:38
【问题描述】:
像printf("hex representation for %f is [%a]\n", 3.14, 3.14);这样的简单代码,我希望结果是0x4048f5c3(完整的二进制版本是:0100 0000 0100 1000 1111 0101 1100 0011,0x4.8f5c3p-1)根据下面的参考网站,但编译的可执行文件显示[0xc.8f5c3p-2](二进制:1100.1000 1111 0101 1100 0011),为什么 C 编译器将指数显示为 -2 而不是 -1?
编译器设置为:
"command": "c:/msys64/mingw64/bin/gcc.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-Wall",
"-Werror",
"-std=c11"
参考: https://users.cs.fiu.edu/~downeyt/cop2400/float.htm https://gregstoll.com/~gregstoll/floattohex/
【问题讨论】:
-
0x4048f5c3是float值的编码。这不是数字的价值。0xc.8f5c3p-2是数字的值,以十六进制表示。 -
另外请注意
3.14是double。如果您想要float,请使用3.14f。 -
这是我很困惑的地方,我按照参考链接将
3.14转换为十六进制,结果出来是404.8f5c3p-1,怎么是电脑上的0xc.8f5c3p-2?
标签: c floating-point printf ieee-754