Floating-point literals

Floating-point literals are numbers that have a decimal point or an exponential part. They can be represented as:

Binary floating-point literals

A real binary floating-point constant consists of the following:

  • An integral part
  • A decimal point
  • A fractional part
  • An exponent part
  • An optional suffix

Both the integral and fractional parts are made up of decimal digits. You can omit either the integral part or the fractional part, but not both. You can omit either the decimal point or the exponent part, but not both.

Read syntax diagramSkip visual syntax diagram
Binary floating-point literal syntax

                       .-------.                              
                       V       |                              
>>-+-+-----------+--.----digit-+--+--------------+-+--+---+----><
   | | .-------. |                '-| exponent |-' |  +-f-+   
   | | V       | |                                 |  +-F-+   
   | '---digit-+-'                                 |  +-l-+   
   | .-------.                                     |  '-L-'   
   | V       |                                     |          
   +---digit-+--.--+--------------+----------------+          
   |               '-| exponent |-'                |          
   | .-------.                                     |          
   | V       |                                     |          
   '---digit-+--| exponent |-----------------------'          

Exponent

                  .-------.   
                  V       |   
|--+-e-+--+----+----digit-+-------------------------------------|
   '-E-'  +-+--+              
          '- --'              

The suffix f or F indicates a type of float, and the suffix l or L indicates a type of long double. If a suffix is not specified, the floating-point constant has a type double.

A plus (+) or minus (-) symbol can precede a floating-point literal. However, it is not part of the literal; it is interpreted as a unary operator.

The following are examples of floating-point literals:

floating-point constant Value
5.3876e4 53,876
4e-11 0.00000000004
1e+5 100000
7.321E-3 0.007321
3.2E+4 32000
0.5e-6 0.0000005
0.45 0.45
6.e10 60000000000

Hexadecimal floating-point literals

Real hexadecimal floating-point constants, which are a C99 feature, consist of the following parts.C++ onlyThe XL C/C++ compiler supports this feature as an IBM extension.C++ only

  • a hexadecimal prefix
  • a significant part
  • a binary exponent part
  • an optional suffix

The significant part represents a rational number and is composed of the following:

  • a sequence of hexadecimal digits (whole-number part)
  • an optional fraction part

The optional fraction part is a period followed by a sequence of hexadecimal digits.

The exponent part indicates the power of 2 to which the significant part is raised, and is an optionally signed decimal integer. The type suffix is optional. The full syntax is as follows:

Read syntax diagramSkip visual syntax diagram
Hexadecimal floating-point literal syntax

>>-+-0x-+------------------------------------------------------->
   '-0X-'   

     .------------------.     .------------------.                   
     V                  |     V                  |                   
>--+---+--------------+-+--.----+-digit_0_to_f-+-+--| exponent |-+-->
   |   +-digit_0_to_f-+         '-digit_0_to_F-'                 |   
   |   '-digit_0_to_F-'                                          |   
   | .------------------.                                        |   
   | V                  |                                        |   
   +---+-digit_0_to_f-+-+--.--| exponent |-----------------------+   
   |   '-digit_0_to_F-'                                          |   
   | .------------------.                                        |   
   | V                  |                                        |   
   '---+-digit_0_to_f-+-+--| exponent |--------------------------'   
       '-digit_0_to_F-'                                              

>--+---+-------------------------------------------------------><
   +-f-+   
   +-F-+   
   +-l-+   
   '-L-'   

Exponent

                  .--------------.   
                  V              |   
|--+-p-+--+----+----digit_0_to_9-+------------------------------|
   '-P-'  +-+--+                     
          '- --'                     

The suffix f or F indicates a type of float, and the suffix l or L indicates a type of long double. If a suffix is not specified, the floating-point constant has a type double. You can omit either the whole-number part or the fraction part, but not both. The binary exponent part is required to avoid the ambiguity of the type suffix F being mistaken for a hexadecimal digit.

Decimal floating-point literals (IBM extension)

A real decimal floating-point constant consists of the following:

  • An integral part
  • A decimal point
  • A fractional part
  • An exponent part
  • An optional suffix

Both the integral and fractional parts are made up of decimal digits. You can omit either the integral part or the fractional part, but not both. You can omit either the decimal point or the exponent part, but not both.

Read syntax diagramSkip visual syntax diagram
Decimal floating-point literal syntax

     .-----------.     .-------.                           
     V           |     V       |                           
>>-+---+-------+-+--.----digit-+--| exponent |-+--+-df-+-------><
   |   '-digit-'                               |  +-DF-+   
   | .-------.                                 |  +-dd-+   
   | V       |                                 |  +-DD-+   
   +---digit-+--.--| exponent |----------------+  +-dl-+   
   | .-------.                                 |  '-DL-'   
   | V       |                                 |           
   '---digit-+--| exponent |-------------------'           

Exponent

                  .-------.   
                  V       |   
|--+-e-+--+----+----digit-+-------------------------------------|
   '-E-'  +-+--+              
          '- --'              

The suffix df or DF indicates a type of _Decimal32, the suffix dd or DD indicates a type of _Decimal64, and the suffix dl or DL indicates a type of _Decimal128. If a suffix is not specified, the floating-point constant has a type double.

You cannot use mixed cases in the literal suffix. For example, the suffix dF or Df is invalid.

The following are examples of decimal floating-point literal declarations:

_Decimal32 a = 22.2df;
_Decimal64 b = 33.3dd;
Note: Decimal floating-point literal suffixes are recognized only when the -qdfp option is enabled.

Complex literals

Complex literals, which were introduced in the C99 standard, are constructed in two parts: the real part, and the imaginary part.

Read syntax diagramSkip visual syntax diagram
Complex literal syntax

>>-| real part |--+-+--+--| imaginary part |-------------------><
                  '- –-'                       

real part

|--floating-point constant--------------------------------------|

imaginary part

|--floating-point constant--*--_Complex_I-----------------------|

floating-point constant can be specified as a decimal or hexadecimal floating-point literal (including optional suffixes), in any of the formats described in the previous sections.

_Complex_I is a macro defined in the complex.h header file, representing the imaginary unit i, the square root of -1.

For example, the declaration:
varComplex = 2.0f + 2.0f * _Complex_I;
initializes the complex variable varComplex to a value of 2.0 + 2.0i.

Begin IBM extension For ease of porting applications developed with GNU C, XL C/C++ also allows you to indicate the imaginary part of a complex literal with a suffix, in addition to the standard suffixes that indicate the type of the complex number (float, double, or long double).

The simplified syntax for a complex literal using the GNU suffixes is as follows:
Read syntax diagramSkip visual syntax diagram
>>-| real part |--+-+--+--| imaginary part |-------------------><
                  '- –-'                       

real part

|--floating-point constant--------------------------------------|

imaginary part

|--floating-point constant--imaginary-suffix--------------------|

floating-point constant can be specified as a decimal or hexadecimal floating-point literal (including optional suffixes), in any of the formats described in the previous sections.

imaginary-suffix is one of the suffixes i, I, j, or J, representing the imaginary unit.

For example, the declaration
varComplex = 3.0f + 4.0fi;
initializes the complex variable varComplex to a value of 3.0 + 4.0i. End IBM extension


Voice your opinion on getting help information Ask IBM compiler experts a technical question in the IBM XL compilers forum Reach out to us