Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/language/floating literal"

From cppreference.com
< cpp‎ | language
m (Text replace - "/sidebar" to "/navbar")
m (r2.7.3) (Robot: Adding de, es, fr, it, ja, pt, ru, zh)
Line 37: Line 37:
 
400
 
400
 
}}
 
}}
 +
 +
[[de:cpp/language/floating literal]]
 +
[[es:cpp/language/floating literal]]
 +
[[fr:cpp/language/floating literal]]
 +
[[it:cpp/language/floating literal]]
 +
[[ja:cpp/language/floating literal]]
 +
[[pt:cpp/language/floating literal]]
 +
[[ru:cpp/language/floating literal]]
 +
[[zh:cpp/language/floating literal]]

Revision as of 21:18, 2 November 2012

 
 
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.

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 significant)
  • (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

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

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

Output:

1.23456e-65
1000
58
400