我不明白浮点数是如何用
Swift中的十六进制表示法表示的. Apple的文档显示0xC.3p0等于十进制的12.1875.有人可以告诉我如何进行转换吗?我明白在十进制十六进制值0xC = 12之前.小数点后的3p0是我难倒的地方.
从
documentation:
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-format非常相似(例如,请参阅
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?欲获得更多信息.