cpp / latest / language / floating_literal.html /

Floating-point literal

Floating-point literal defines a compile-time constant whose value is specified in the source file.

Syntax

digit-sequence decimal-exponent suffix(optional) (1)
digit-sequence . decimal-exponent(optional) suffix(optional) (2)
digit-sequence(optional) . digit-sequence decimal-exponent(optional) suffix(optional) (3)
0x | 0X hex-digit-sequence hex-exponent suffix(optional) (4) (since C++17)
0x | 0X hex-digit-sequence . hex-exponent suffix(optional) (5) (since C++17)
0x | 0X hex-digit-sequence(optional) . hex-digit-sequence hex-exponent suffix(optional) (6) (since C++17)
1) digit-sequence representing a whole number without a decimal separator, in this case the exponent is not optional: 1e10, 1e-5L
2) digit-sequence representing a whole number with a decimal separator, in this case the exponent is optional: 1., 1.e-2
3) digit-sequence representing a fractional number. The exponent is optional: 3.14, .1f, 0.1e-1L
4) Hexadecimal digit-sequence representing a whole number without a radix separator. The exponent is never optional for hexadecimal floating-point literals: 0x1ffp10, 0X0p-1
5) Hexadecimal digit-sequence representing a whole number with a radix separator. The exponent is never optional for hexadecimal floating-point literals: 0x1.p0, 0xf.p-1
6) Hexadecimal digit-sequence representing a fractional number with a radix separator. The exponent is never optional for hexadecimal floating-point literals: 0x0.123p-1, 0xa.bp10l

decimal-exponent has the form.

e | E exponent-sign(optional) digit-sequence

hex-exponent has the form.

p | P exponent-sign(optional) digit-sequence (since C++17)

exponent-sign, if present, is either + or -

suffix, if present, is one of f, F, l, or L. The suffix determines the type of the floating-point literal:

  • (no suffix) defines double
  • f F defines float
  • l L defines long double

Optional single quotes (') may be inserted between the digits as a separator; they are ignored during compilation.

(since C++14)

Explanation

Decimal scientific notation is used, meaning that the value of the floating-point literal is the significand multiplied by the number 10 raised to the power of decimal-exponent. E.g. the mathematical meaning of 123e4 is 123×104.

If the floating literal begins with the character sequence 0x or 0X, the floating literal is a hexadecimal floating literal. Otherwise, it is a decimal floating literal.

For a hexadecimal floating literal, the significand is interpreted as a hexadecimal rational number, and the digit-sequence of the exponent is interpreted as the integer power of 2 to which the significand has to be scaled.

double d = 0x1.4p3; // hex fraction 1.4 (decimal 1.25) scaled by 2^3, that is 10.0
(since C++17)

Notes

The hexadecimal floating-point literals were not part of C++ until C++17, although they can be parsed and printed by the I/O functions since C++11: both C++ I/O streams when std::hexfloat is enabled and the C I/O streams: std::printf, std::scanf, etc. See std::strtof for the format description.

Example

#include <iostream>
#include <iomanip>
#include <limits>
#include <cassert>
#include <typeinfo>
 
int main()
{
  std::cout << "Literal       "   << "Printed value"
            << "\n58.           " << 58.          // double
            << "\n4e2           " << 4e2          // double
            << "\n123.456e-67   " << 123.456e-67  // double
            << "\n123.456e-67f  " << 123.456e-67f // float, truncated to zero
            << "\n.1E4f         " << .1E4f        // float
            << "\n0x10.1p0      " << 0x10.1p0     // double
            << "\n0x1p5         " << 0x1p5        // double
            << "\n0x1e5         " << 0x1e5        // integer literal, not floating-point
            << "\n3.14'15'92    " << 3.14'15'92   // double, single quotes ignored (C++14)
            << "\n1.18e-4932l   " << 1.18e-4932l  // long double
            << std::setprecision(39)
            << "\n3.4028234e38f " << 3.4028234e38f // float
            << "\n3.4028234e38  " << 3.4028234e38  // double
            << "\n3.4028234e38l " << 3.4028234e38l // long double
            << '\n';
 
  assert(3.4028234e38f == std::numeric_limits<float>::max());
  assert(3.4028234e38f == 3.402823'5e38f); // Left ends in 4, right ends in 5
  assert(3.4028234e38  != 3.402823'5e38);
  // Both side below are 3.4028234e38 but the right is a double and
  assert(3.4028234e38f != 3.4028234e38); // the left is a float (then promoted to double)
 
}

Possible output:

Literal       Printed value
58.           58
4e2           400
123.456e-67   1.23456e-65
123.456e-67f  0
.1E4f         1000
0x10.1p0      16.0625
0x1p5         32
0x1e5         485
3.14'15'92    3.14159
1.18e-4932l   1.18e-4932
3.4028234e38f 340282346638528859811704183484516925440
3.4028234e38  340282339999999992395853996843190976512
3.4028234e38l 340282339999999999995912555211526242304

See also

user-defined literals(C++11) literals with user-defined suffix
C documentation for Floating constant

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/language/floating_literal