I’m busy writing about literal style (prefer literals when you can but add typing) and stumbled across a form of binary exponentiation that I hadn’t used before.
Do you know what these two Swift literals evaluate to?
0x15p1, 0xA.8p2
I’ll give you a hint. The numbers must start with hex values and may include elements past the decimal point. The p
exponentiation means “multiply by 2 raised to this power”, so 0x15p1 means 15 hex multiplied by 2, and 0xA.8p2 means A.8 hex multiplied by 4.
Of course you can just type them into a playground but where’s the fun in that?
let dent = 0x15p1
If you still don’t know the answers, I’m pretty sure that a pair of ordinary lab mice might.
3 Comments
For more background: http://www.exploringbinary.com/hexadecimal-floating-point-constants/
For more background: https://www.amazon.com/Hitchhikers-Guide-Galaxy-Douglas-Adams/dp/0345391802
What’s interesting, is that it evaluates to a Double. Paste this in a Playground:
type(of: 0x1p1)
It’ll say Double.Type.
I like this blog post 🙂