Swift 中的浮点十六进制表示法
Floating point hex notation in Swift
我不明白 Swift 中浮点数是如何用十六进制表示法表示的。 Apple 的文档显示 0xC.3p0 等于十进制的 12.1875。有人可以指导我如何进行转换吗?我知道在十进制十六进制值 0xC = 12 之前。小数点后的 3p0 是我难过的地方。
你说的0xC是12。小数部分为((1/16)*3)*10^0.
所以需要取小数部分除以16,再乘以2的p
次方
Floating-Point Literals
...
Hexadecimal floating-point literals consist of a 0x prefix, followed
by an optional hexadecimal fraction, followed by a hexadecimal
exponent. The hexadecimal fraction consists of a decimal point
followed by a sequence of hexadecimal digits. The exponent consists of
an upper- or lowercase p prefix followed by a sequence of decimal
digits that indicates what power of 2 the value preceding the p is
multiplied by. For example, 0xFp2 represents 15 × 22, which evaluates
to 60. Similarly, 0xFp-2 represents 15 × 2-2, which evaluates to 3.75.
你的情况
0xC.3p0 = (12 + 3/16) * 2^0 = 12.1875
另一个例子:
0xAB.CDp4 = (10*16 + 11 + 12/16 + 13/16^2) * 2^4 = 2748.8125
此格式与 %a
printf 格式非常相似(参见示例
http://pubs.opengroup.org/onlinepubs/009695399/functions/fprintf.html)。
它可以用来直接在其指定一个浮点数
二进制 IEEE 754 表示,参见 Why does Swift use base 2 for the exponent of hexadecimal floating point values?
获取更多信息。
使用位值系统解释 0xC.3p0
:
C (or 12) is in the 16^0 place
3 is in the 16^-1 place (and 3/16 == 0.1875)
p says the exponent follows (like the e in 6.022e23 in base 10)
0 is the exponent (in base 10) that is the power of 2 (2^0 == 1)
所以把它们放在一起[=13=]
0xC.3p0 = (12 + (3/16)) * 2^0 = 12.1875
为了总结我所阅读的内容,您可以看到这些表示如下:
0xC.3p0 = (12*16^0 + 3*16^-1) * 2^0 = 12.1875
来自上面 Martin R 的示例:
0xAB.CDp4 = (10*16^1 + 11*16^0 + 12*16^-1 + 13*16^-2) * 2^4 = 2748.8125
十六进制 -(0-9,A=10,B=11,C=12,D=13,E=14,F=15)
和 p0
表示 2^0
ex: - 0xC = 12
(0x
前缀表示十六进制)
在 0xC.3p0
中的小数部分之后,我们将数字除以 16
的幂
这里是 3/16 = 0.1875
所以 0xC.3p0 = (12 + (3/16) ) 2^0
如果是 0xC.43p0
那么对于 4 我们将使用 4/(16)
,对于 3 我们将使用 3/(16 ^2)
并且如果小数部分增加则类似。
例如:0xC.231p1 = (12 + 2/16 + 3/(256) + 1/(16^3)) 2^1 = 24.27392578125
我不明白 Swift 中浮点数是如何用十六进制表示法表示的。 Apple 的文档显示 0xC.3p0 等于十进制的 12.1875。有人可以指导我如何进行转换吗?我知道在十进制十六进制值 0xC = 12 之前。小数点后的 3p0 是我难过的地方。
你说的0xC是12。小数部分为((1/16)*3)*10^0.
所以需要取小数部分除以16,再乘以2的p
Floating-Point Literals
...
Hexadecimal floating-point literals consist of a 0x prefix, followed by an optional hexadecimal fraction, followed by a hexadecimal exponent. The hexadecimal fraction consists of a decimal point followed by a sequence of hexadecimal digits. The exponent consists of an upper- or lowercase p prefix followed by a sequence of decimal digits that indicates what power of 2 the value preceding the p is multiplied by. For example, 0xFp2 represents 15 × 22, which evaluates to 60. Similarly, 0xFp-2 represents 15 × 2-2, which evaluates to 3.75.
你的情况
0xC.3p0 = (12 + 3/16) * 2^0 = 12.1875
另一个例子:
0xAB.CDp4 = (10*16 + 11 + 12/16 + 13/16^2) * 2^4 = 2748.8125
此格式与 %a
printf 格式非常相似(参见示例
http://pubs.opengroup.org/onlinepubs/009695399/functions/fprintf.html)。
它可以用来直接在其指定一个浮点数
二进制 IEEE 754 表示,参见 Why does Swift use base 2 for the exponent of hexadecimal floating point values?
获取更多信息。
使用位值系统解释 0xC.3p0
:
C (or 12) is in the 16^0 place
3 is in the 16^-1 place (and 3/16 == 0.1875)
p says the exponent follows (like the e in 6.022e23 in base 10)
0 is the exponent (in base 10) that is the power of 2 (2^0 == 1)
所以把它们放在一起[=13=]
0xC.3p0 = (12 + (3/16)) * 2^0 = 12.1875
为了总结我所阅读的内容,您可以看到这些表示如下:
0xC.3p0 = (12*16^0 + 3*16^-1) * 2^0 = 12.1875
来自上面 Martin R 的示例:
0xAB.CDp4 = (10*16^1 + 11*16^0 + 12*16^-1 + 13*16^-2) * 2^4 = 2748.8125
十六进制 -(0-9,A=10,B=11,C=12,D=13,E=14,F=15)
和 p0
表示 2^0
ex: - 0xC = 12
(0x
前缀表示十六进制)
在 0xC.3p0
中的小数部分之后,我们将数字除以 16
这里是 3/16 = 0.1875
所以 0xC.3p0 = (12 + (3/16) ) 2^0
如果是 0xC.43p0
那么对于 4 我们将使用 4/(16)
,对于 3 我们将使用 3/(16 ^2)
并且如果小数部分增加则类似。
例如:0xC.231p1 = (12 + 2/16 + 3/(256) + 1/(16^3)) 2^1 = 24.27392578125