带有p的点表示法,用于swift中的十六进制数字文字

前端之家收集整理的这篇文章主要介绍了带有p的点表示法,用于swift中的十六进制数字文字前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用XCode在 https://github.com/nettlep/learn-swift的第一个基本操场上工作

这个表达到底发生了什么?

0xC.3p0 == 12.1875

我已经了解了十六进制文字和特殊的“p”符号,表示2的幂.

0xF == 15

0xFp0 == 15   //  15 * 2^0

如果我尝试0xC.3我得到错误:十六进制浮点文字必须以指数结束.

我发现这很好overview of numeric literalsanother deep explanation,但我没有看到解释什么.3p0的东西.

我已经将代码分叉并将本课程升级到XCode 7 / Swift 2 – 这是specific line.

这是 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.

Example: 1.3DEp42 represents hex(1.3DE) × dec(2^42).

以您为例,我们得到:

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)

例如,您可以尝试0xC.3p1(等于十六进制(C.3)* dec(2 ^ 1)),这会产生两倍的值,即24.375.

您还可以在操场中研究十六进制值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
// ...

最后,这也在Apple`s Language Reference – Lexical Types: 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 x 2^2,which evaluates to 60. Similarly,0xFp-2 represents 15 x 2^-2,which evaluates to 3.75.

原文链接:https://www.f2er.com/swift/319161.html

猜你在找的Swift相关文章