# floating point literal

< cpp‎ | language
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

C++
 Language Standard library headers Concepts Utilities library Strings library Containers library Algorithms library Iterators library Numerics library Input/output library Localizations library Regular expressions library (C++11) Atomic operations library (C++11) Thread support library (C++11) Technical Specifications

Expressions
 General value categories (lvalue, rvalue, xvalue) order of evaluation (sequence points) constant expressions unevaluated expressions primary expressions lambda-expression(C++11) Literals integer literals floating-point literals boolean literals character literals including escape sequences string literals null pointer literal(C++11) user-defined literal(C++11) Operators Assignment operators: a=b, a+=b, a-=b, a*=b, a/=b, a%=b, a&=b, a|=b, a^=b, a<<=b, a>>= Increment and decrement: ++a, --a, a++, a-- Arithmetic operators: +a, -a, a+b, a-b, a*b, a/b, a%b, ~a, a&b, a|b, a^b, a<>b Logical operators: a||b}, a&&b, !a Comparison operators: a==b, a!=b, ab, a<=b, a>=b Member access operators: a[b], *a, &a, a->b, a.b, a->*b, a.*b Other operators: a(...), a,b, (T)a, a?b:c Alternative representations of operators Precedence and associativity Fold expression(C++17) new-expression delete-expression throw-expression alignof sizeof sizeof...(C++11) typeid noexcept(C++11) Operator overloading Conversions Implicit conversions const_cast static_cast reinterpret_cast dynamic_cast Explicit conversions (T)a, T(a) User-defined conversion

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

## Contents

### Syntax

A floating-point literals have two syntaxes. The first one consists of the following parts:

• nonempty sequence of decimal digits containing a decimal point character (defines significand)
• (optional) e or E followed with optional minus or plus sign and nonempty sequence of decimal digits (defines exponent)
• (optional) a suffix type specifier as a l, f, L or F

The second one consists of the following parts:

• nonempty sequence of decimal digits (defines significant)
• e or E followed with optional minus or plus sign and nonempty sequence of decimal digits (defines exponent)
• (optional) a suffix type specifier as a l, f, L or F

The suffix type specifier defines the actual 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

A decimal scientific notation is used, meaning that the exponent is the power of 10 by which the significant is multiplied.
The mathematical meaning of 123e4 is 123×104

### Example

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

Output:

1.23456e-65
1000
58
400

### Notes

The hexadecimal floating-point constants (e.g. 0x1p-5, 0x1.0Ap-2, 0x1.8p-1), which are allowed in the C programming language, cannot be used as floating point literals in C++ (although some C++ compilers might interpret them). However, they may be parsed and printed by the I/O functions: 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