Namespaces
Variants
Views
Actions

floating point literal

From cppreference.com
 
 
C++ language
General topics
Flow control
Conditional execution statements
if
Iteration statements (loops)
for
range-for (C++11)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications (until C++17*)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
const/volatile
decltype (C++11)
auto (C++11)
constexpr (C++11)
consteval (C++20)
constinit (C++20)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Memory allocation
Classes
Class-specific function properties
explicit (C++11)
static

Special member functions
Templates
Miscellaneous
 
 

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

Contents

Syntax

digit-sequence exponent suffix(optional) (1)
digit-sequence . exponent(optional) suffix(optional) (2)
digit-sequence(optional) . digit-sequence exponent(optional) suffix(optional) (3)
0x | 0X hex-digit-sequence exponent suffix(optional) (4) (since C++17)
0x | 0X hex-digit-sequence . exponent suffix(optional) (5) (since C++17)
0x | 0X hex-digit-sequence(optional) . hex-digit-sequence 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

The exponent has the form

e | E exponent-sign(optional) digit-sequence (1)
p | P exponent-sign(optional) digit-sequence (2) (since C++17)
1) The exponent syntax for a decimal floating-point literal
2) The exponent syntax for hexadecimal floating-point literal

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(') can be inserted between the digits as a separator, they are ignored when compiling. (since C++14)

Explanation

Decimal scientific notation is used, meaning that the value of the floating-point literal is the significand mutiplied by the number 10 raised to the power of exponent. 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.2p3; // hex fraction 1.2 (decimal 1.125) scaled by 2^3, that is 9.0
(since C++17)

Example

#include <iostream>
int main()
{
  std::cout << 58.         << '\n'
            << 4e2         << '\n'
            << 123.456e-67 << '\n'
            << .1E4f       << '\n'
            << 0x10.1p0    << '\n';
}

Output:

58
400
1.23456e-65
1000
16.0625

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

See also

C documentation for floating point constant