Determining floating point values ​​by base and exponent explicitly

I came across http://sourceware.org/ml/glibc-cvs/2013-q1/msg00115.html which includes a line

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

Apparently TWO5 is defined as double with an explicit value 1<<5. However, this is the first time I see this designation. How is this format used, and what is the advantage over writing 2.5?

+5
source share
3 answers

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

, , :

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

+6

, C99 ( ). , . ( , 2, 4, 8 16:)

+3

, C99, :

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

4 :

  • 0[xX]: , 2:

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

    34.2f
    .de
    b3.
    

    ( ), :

    2f4
    10
    

    .

  • [pP][+-]?[0-9]+: binary part of the exponent (indicated in decimal form), for example:

    p-4
    p20
    

    We will indicate the hexadecimal constant with a floating point, indicating the mantissa in hexadecimal, and the exponent b (for base 2) decimal.

  • [flFL]?: optional floating suffix to indicate type ( float, doubleor long double).

+2
source

All Articles