Reputation: 7941
Could you please explain double hex number representation that uses 'p' character :
0x1.5p10 or 0x1P-1
Thank you in advance.
Upvotes: 0
Views: 492
Reputation: 106127
This actually isn't specific to Objective-C; it's a standard feature inherited from C. It provides a way to write floating-point numbers without rounding. It is clearly documented in the C standard, but here's a quick overview:
Hexadecimal floating-point literals have the form 0x[significand]p[exponent]
, where [significand]
is a hexadecimal number, and [exponent]
is a base-10 integer. The number has the value [significand] x 2^[exponent]
.
So, for example:
0x1.5p10 = (1 + 5/16) x 2^10 = 1344.0
0x1p-1 = 1 x 2^-1 = 0.5
These examples aren't particularly interesting, because they can both easily be written exactly as decimal floating-point literals as well. The notation gets more interesting as the exponent gets more extreme. For example, consider the largest finite double-precision number: 0x1.fffffffffffffp1023
in hexadecimal floating-point. Writing it exactly in decimal gives us:
179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0
which is rather cumbersome.
Upvotes: 4