gpt4 book ai didi

swift - 带 p 的点符号用于 swift 中的十六进制数字文字

转载 作者:搜寻专家 更新时间:2023-10-30 21:56:53 25 4
gpt4 key购买 nike

我正在研究 https://github.com/nettlep/learn-swift 中的第一个基本 Playground 使用 XCode

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

0xC.3p0 == 12.1875

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

0xF == 15

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

如果我尝试 0xC.3,我会收到错误消息:十六进制浮点文字必须以指数结尾。

我觉得这很好 overview of numeric literalsanother deep explanation ,但我没有看到可以解释 .3p0 功能的内容。

我已经 fork 代码并将本课升级到 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(等于 hex(C.3) * dec(2^1)),它会产生双倍的值,即,24.375

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

关于swift - 带 p 的点符号用于 swift 中的十六进制数字文字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34426506/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com