加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 05:25:52 所属栏目:百科 来源:网络整理
导读:我正在使用XCode在 https://github.com/nettlep/learn-swift的第一个基本操场上工作 这个表达到底发生了什么? 0xC.3p0 == 12.1875 我已经了解了十六进制文字和特殊的“p”符号,表示2的幂. 0xF == 150xFp0 == 15 // 15 * 2^0 如果我尝试0xC.3我得到错误:十
我正在使用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 literals和another 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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读