Search code examples
cfloating-pointnotation

Defining floating point values by base and exponent explicitly


I stumbled upon http://sourceware.org/ml/glibc-cvs/2013-q1/msg00115.html, which includes the line

#define  TWO5      0x1.0p5      /* 2^5     */

Apparently, TWO5 is defined as a double with the explicit value 1<<5. However, this is the first time I see this notation. How has this format been in use, and what is the advantage over just writing 2.5?


Solution

  • This notation was introduced in C99. The advantage is that the value is expressed in hexadecimal form, so it is not subject to rounding etc. that occurs when you convert a floating-point value between the underlying representation and a decimal form.

    There are plenty of pages that describe this notation, for example:

    http://www.exploringbinary.com/hexadecimal-floating-point-constants/