Namespaces
Variants

Floating-point literal

From cppreference.net
C++ language
General topics
Flow control
Conditional execution statements
Iteration statements (loops)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications ( until C++17* )
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
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
Special member functions
Templates
Miscellaneous

부동 소수점 리터럴은 소스 파일에 지정된 값을 가진 컴파일 타임 상수를 정의합니다.

목차

구문

digit-sequence decimal-exponent suffix  (선택 사항) (1)
digit-sequence . decimal-exponent  (선택 사항) suffix  (선택 사항) (2)
digit-sequence  (선택 사항) . digit-sequence decimal-exponent  (선택 사항) suffix  (선택 사항) (3)
0x | 0X hex-digit-sequence hex-exponent suffix  (선택 사항) (4) (C++17부터)
0x | 0X hex-digit-sequence . hex-exponent suffix  (선택 사항) (5) (C++17부터)
0x | 0X hex-digit-sequence  (선택 사항) . hex-digit-sequence hex-exponent suffix  (선택 사항) (6) (C++17부터)
1) digit-sequence 소수점 구분자 없이 정수를 나타내는 경우, 지수는 선택적이지 않습니다: 1e10 , 1e - 5L .
2) digit-sequence 소수점 구분자를 포함한 정수를 나타내는 경우, 이때 지수는 선택사항입니다: 1 . , 1. e -2 .
3) digit-sequence 소수점 숫자를 나타내는. 지수는 선택 사항입니다: 3.14 , .1f , 0.1e - 1L .
4) 소수점 구분 기호 없이 정수를 나타내는 16진수 digit-sequence . 16진수 부동 소수점 리터럴의 경우 지수 부분은 절대 생략할 수 없습니다: 0x1ffp10 , 0X0p - 1 .
5) 전체 숫자를 나타내는 16진수 digit-sequence 와 기수 구분자. 16진수 부동 소수점 리터럴의 경우 지수는 절대 생략할 수 없습니다: 0x1 . p0 , 0xf . p - 1 .
6) 분수를 나타내는 16진수 digit-sequence (기수 구분자를 포함). 16진수 부동 소수점 리터럴의 경우 지수 부분은 절대 생략할 수 없습니다: 0x0.123p - 1 , 0xa . bp10l .

decimal-exponent 는 다음과 같은 형태를 가집니다

e | E 지수-부호  (선택사항) 숫자-시퀀스

hex-exponent 는 다음과 같은 형태를 가집니다

p | P 지수-부호  (선택사항) 숫자-시퀀스 (C++17부터)

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

접미사(suffix) 가 존재하는 경우, 다음 중 하나입니다: f , l , F , L , f16 , f32 , f64 , f128 , bf16 , F16 , F32 , F64 , F128 , BF16 (C++23부터) . 접미사는 부동 소수점 리터럴의 타입을 결정합니다:

  • (접미사 없음)은 double 을 정의합니다
  • f F float 을 정의합니다
  • l L long double 을 정의합니다
  • f16 F16 std::float16_t 를 정의합니다
  • f32 F32 std::float32_t 를 정의합니다
  • f64 F64 std::float64_t 를 정의합니다
  • f128 F128 std::float128_t 를 정의합니다
  • bf16 BF16 std::bfloat16_t 를 정의합니다
(C++23부터)

선택적으로 단일 따옴표( ' )를 숫자 사이에 구분자로 삽입할 수 있으며, 이는 리터럴의 값을 결정할 때 무시됩니다.

(C++14부터)

설명

십진 과학적 표기법이 사용되며, 이는 부동 소수점 리터럴의 값이 가수부에 10의 decimal-exponent 제곱을 곱한 값임을 의미합니다. 예를 들어, 123e4 의 수학적 의미는 123×10 4 입니다.

부동 소수점 리터럴이 문자 시퀀스 0x 또는 0X 로 시작하면, 해당 부동 소수점 리터럴은 16진 부동 소수점 리터럴 입니다. 그렇지 않으면 10진 부동 소수점 리터럴 입니다.

16진 부동 소수점 리터럴 의 경우, significand는 16진 유리수로 해석되며, 지수의 digit-sequence 는 significand를 스케일링해야 하는 2의 (10진) 정수 거듭제곱으로 해석됩니다.

double d = 0x1.4p3 ; // 16진 분수 1.4(10진수 1.25)가 2 3 으로 스케일링됨, 즉 10.0

(C++17부터)

참고 사항

16진수 부동 소수점 리터럴은 C++17 이전까지 C++의 일부가 아니었지만, C++11부터 I/O 함수에 의해 파싱 및 출력될 수 있습니다: std::hexfloat 가 활성화된 C++ I/O 스트림과 C I/O 스트림 모두에서 사용 가능합니다: std::printf , std::scanf 등. 형식 설명은 std::strtof 를 참조하십시오.

기능 테스트 매크로 표준 기능
__cpp_hex_float 201603L (C++17) 16진수 부동 소수점 리터럴

예제

#include <iomanip>
#include <iostream>
#include <limits>
#include <typeinfo>
#define OUT(x) '\n' << std::setw(16) << #x << x
int main()
{
    std::cout
        << "Literal" "\t" "Printed value" << std::left
        << OUT( 58.            ) // double
        << OUT( 4e2            ) // double
        << OUT( 123.456e-67    ) // double
        << OUT( 123.456e-67f   ) // float, truncated to zero
        << OUT( .1E4f          ) // float
        << OUT( 0x10.1p0       ) // double
        << OUT( 0x1p5          ) // double
        << OUT( 0x1e5          ) // integer literal, not floating-point
        << OUT( 3.14'15'92     ) // double, single quotes ignored (C++14)
        << OUT( 1.18e-4932l    ) // long double
        << std::setprecision(39)
        << OUT( 3.4028234e38f  ) // float
        << OUT( 3.4028234e38   ) // double
        << OUT( 3.4028234e38l  ) // long double
        << '\n';
    static_assert(3.4028234e38f == std::numeric_limits<float>::max());
    static_assert(3.4028234e38f ==  // ends with 4
                  3.4028235e38f);   // ends with 5
    static_assert(3.4028234e38 !=   // ends with 4
                  3.4028235e38);    // ends with 5
    // Both floating-point constants below are 3.4028234e38
    static_assert(3.4028234e38f !=  // a float (then promoted to double)
                  3.4028234e38);    // a double
}

가능한 출력:

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

참고문헌

  • C++23 표준 (ISO/IEC 14882:2024):
  • 5.13.4 부동 소수점 리터럴 [lex.fcon]
  • C++20 표준(ISO/IEC 14882:2020):
  • 5.13.4 부동 소수점 리터럴 [lex.fcon]
  • C++17 표준 (ISO/IEC 14882:2017):
  • 5.13.4 부동 소수점 리터럴 [lex.fcon]
  • C++14 표준(ISO/IEC 14882:2014):
  • 2.14.4 부동 소수점 리터럴 [lex.fcon]
  • C++11 표준 (ISO/IEC 14882:2011):
  • 2.14.4 부동 소수점 리터럴 [lex.fcon]
  • C++98 표준(ISO/IEC 14882:1998):
  • 2.13.3 부동 소수점 리터럴 [lex.fcon]

참고 항목

사용자 정의 리터럴 (C++11) 사용자 정의 접미사를 가진 리터럴
C 문서 for 부동 소수점 상수