Namespaces
Variants
Views
Actions

floating point literal

From cppreference.com
 
 
C++ language
General topics
Flow control
Conditional execution statements
Iteration statements
Jump statements
Functions
function declaration
lambda function declaration
function template
inline specifier
exception specifications (deprecated)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
decltype specifier (C++11)
Specifiers
cv specifiers
storage duration specifiers
constexpr specifier (C++11)
auto specifier (C++11)
alignas specifier (C++11)
Initialization
Literals
Expressions
alternative representations
Utilities
Types
typedef declaration
type alias declaration (C++11)
attributes (C++11)
Casts
implicit conversions
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
C-style and functional cast
Memory allocation
Classes
Class-specific function properties
Special member functions
Templates
class template
function template
template specialization
parameter packs (C++11)
Miscellaneous
Inline assembly
 

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

Contents

[edit] 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

[edit] 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

[edit] Example

std::cout << 123.456e-67 << '\n'
          << .1E4f       << '\n'
          << 58.         << '\n'
          << 4e2         << '\n';

Output:

1.23456e-65
1000
58
400

[edit] 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