I'm working through the first basic playground in https://github.com/nettlep/learn-swift using XCode
我正在使用XCode在https://github.com/nettlep/learn-swift中完成第一个基本操场
What exactly is happening with this expression?
这个表达到底发生了什么?
0xC.3p0 == 12.1875
I've learned about hexadecimal literals and the special "p" notation that indicates a power of 2.
我已经了解了十六进制文字和特殊的“p”符号,表示2的幂。
0xF == 15
0xFp0 == 15 // 15 * 2^0
If I try 0xC.3
I get the error: Hexadecimal floating point literal must end with an exponent.
如果我尝试0xC.3我得到错误:十六进制浮点文字必须以指数结束。
I found this nice overview of numeric literals and another deep explanation, but I didn't see something that explains what .3p0
does.
我发现了数字文字的这个很好的概述和另一个深刻的解释,但我没有看到解释什么.3p0的东西。
I've forked the code and upgraded this lesson to XCode 7 / Swift 2 -- here's the specific line.
我已经将代码分叉并将本课程升级到XCode 7 / Swift 2 - 这是具体的一行。
1 个解决方案
#1
5
This is Hexadecimal exponential notation.
这是十六进制指数表示法。
By convention, the letter P (or p, for "power") represents times two raised to the power of ... The number after the P is decimal and represents the binary exponent.
按照惯例,字母P(或p,表示“幂”)表示时间2增加到...的幂.P后面的数字是十进制并表示二进制指数。
...
...
Example: 1.3DEp42 represents hex(1.3DE) × dec(2^42).
示例:1.3DEp42表示十六进制(1.3DE)×dec(2 ^ 42)。
For your example, we get:
以您为例,我们得到:
0xC.3p0 represents 0xC.3 * 2^0 = 0xC.3 * 1 = hex(C.3) = 12.1875
where hex(C.3) = dec(12.{3/16}) = dec(12.1875)
As an example, you can try 0xC.3p1
(equals hex(C.3) * dec(2^1)
), which yields double the value, i.e., 24.375
.
例如,您可以尝试0xC.3p1(等于十六进制(C.3)* dec(2 ^ 1)),这会产生两倍的值,即24.375。
You can also study the binary exponent growth in a playground for hex-value 1:
您还可以在操场中研究十六进制值1的二进制指数增长:
// ...
print(0x1p-3) // 1/8 (0.125)
print(0x1p-2) // 1/4 (0.25)
print(0x1p-1) // 1/2 (0.5)
print(0x1p1) // 2.0
print(0x1p2) // 4.0
print(0x1p3) // 8.0
// ...
Finally, this is also explained in Apple`s Language Reference - Lexical Types: Floating-Point Literals:
最后,Apple的语言参考 - 词汇类型:浮点文字:
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 x 2^2, which evaluates to 60. Similarly, 0xFp-2 represents 15 x 2^-2, which evaluates to 3.75.
十六进制浮点文字由0x前缀组成,后跟可选的十六进制小数,后跟十六进制指数。十六进制小数由小数点后跟一系列十六进制数字组成。指数由大写或小写的p前缀后跟一个十进制数字序列组成,这些十进制数字表示p之前的值乘以2的2的幂。例如,0xFp2表示15 x 2 ^ 2,其计算结果为60.类似地,0xFp-2表示15 x 2 ^ -2,其计算结果为3.75。
#1
5
This is Hexadecimal exponential notation.
这是十六进制指数表示法。
By convention, the letter P (or p, for "power") represents times two raised to the power of ... The number after the P is decimal and represents the binary exponent.
按照惯例,字母P(或p,表示“幂”)表示时间2增加到...的幂.P后面的数字是十进制并表示二进制指数。
...
...
Example: 1.3DEp42 represents hex(1.3DE) × dec(2^42).
示例:1.3DEp42表示十六进制(1.3DE)×dec(2 ^ 42)。
For your example, we get:
以您为例,我们得到:
0xC.3p0 represents 0xC.3 * 2^0 = 0xC.3 * 1 = hex(C.3) = 12.1875
where hex(C.3) = dec(12.{3/16}) = dec(12.1875)
As an example, you can try 0xC.3p1
(equals hex(C.3) * dec(2^1)
), which yields double the value, i.e., 24.375
.
例如,您可以尝试0xC.3p1(等于十六进制(C.3)* dec(2 ^ 1)),这会产生两倍的值,即24.375。
You can also study the binary exponent growth in a playground for hex-value 1:
您还可以在操场中研究十六进制值1的二进制指数增长:
// ...
print(0x1p-3) // 1/8 (0.125)
print(0x1p-2) // 1/4 (0.25)
print(0x1p-1) // 1/2 (0.5)
print(0x1p1) // 2.0
print(0x1p2) // 4.0
print(0x1p3) // 8.0
// ...
Finally, this is also explained in Apple`s Language Reference - Lexical Types: Floating-Point Literals:
最后,Apple的语言参考 - 词汇类型:浮点文字:
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 x 2^2, which evaluates to 60. Similarly, 0xFp-2 represents 15 x 2^-2, which evaluates to 3.75.
十六进制浮点文字由0x前缀组成,后跟可选的十六进制小数,后跟十六进制指数。十六进制小数由小数点后跟一系列十六进制数字组成。指数由大写或小写的p前缀后跟一个十进制数字序列组成,这些十进制数字表示p之前的值乘以2的2的幂。例如,0xFp2表示15 x 2 ^ 2,其计算结果为60.类似地,0xFp-2表示15 x 2 ^ -2,其计算结果为3.75。