5

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?

mafu
  • 31,798
  • 42
  • 154
  • 247

3 Answers3

6

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/

Lindydancer
  • 25,428
  • 4
  • 49
  • 68
3

This is the hexadecimal floating point notation that came with C99 (I think). The advantage is that it allows to specify such constants with their exact representable value. (well this supposes that the floating point base is 2, 4, 8 or 16 :)

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
2

This is the grammar of hexadecimal floating point constant, as defined in C99 draft, written as regular expression:

0[xX]([a-fA-F0-9]*[.][a-fA-F0-9]+|[a-fA-F0-9]+[.]?)[pP][+-]?[0-9]+[flFL]?

Which consists of 4 parts:

  • 0[xX]: Hexadecimal prefix, either of these 2:

    0x
    0X
    
  • ([a-fA-F0-9]*[.][a-fA-F0-9]+|[a-fA-F0-9]+[.]?): Hexadecimal fractional constant, for example:

    34.2f
    .de
    b3.
    

    or hexadecimal digit sequence (whole number in hexadecimal), for example:

    2f4
    10
    

    The second part basically describes the mantissa.

  • [pP][+-]?[0-9]+: Binary exponent part (specified in decimal), for example:

    p-4
    p20
    

    We specify a hexadecimal floating point constant by specifying the mantissa in hexadecimal, and the exponent b (for base 2) in decimal.

  • [flFL]?: Optional floating suffix, to indicate the type (float, double or long double).

nhahtdh
  • 55,989
  • 15
  • 126
  • 162