부동소수점수에 '를 붙이면 가독성이 좋아 실수를 덜 합니다.
아래의 소스는 MSVC 와 g++ 로 잘 컴파일되고 동일한 실행 결과를 얻습니다.
// Filename: test_cpp_literal.cpp
//
// Compile: cl /utf-8 /EHsc test_cpp_literal.cpp
// Execute: test_cpp_literal
//
//            Or
//
// Compile: g++ -o test_cpp_literal test_cpp_literal.cpp
// Execute:./test_cpp_literal
//
// Output:
//     pie =
//     3.14159
//     3.14159
//     3.141593
//     3.141593e+00
//
//       pie =
//       3.141593
//       3.14159
//       3.141593
//       3.141593e+00
//
//     ee =
//     2.71828
//     2.71828
//     2.718280
//     2.718280e+00
// 
//       ee =
//       2.71828
//       2.71828
//       2.718280
//       2.718280e+00
//
//
// Date: 2023.01.03
//
// Copyright (c) 2023 scrpting.tistory.com
// Liscense: BSD-3
#include <iostream>
#include <iomanip>     // for std::setprecision()
#include <cstdio>
using namespace std;
#if defined(__cplusplus)
double pie = 3.141'593;
#endif
int main()
{
    cout << "pie = " << endl;
    cout << pie << endl;
    printf("%g\n", pie);
    printf("%f\n", pie);
    printf("%e\n", pie);
    cout << endl;
    cout << "  pie = " << endl;
    cout << "  " << setprecision(10) << pie << endl;
    printf("  %lg\n", pie);
    printf("  %lf\n", pie);
    printf("  %le\n", pie);
    cout << endl;
#if defined(__cplusplus)
double ee = 2.718'28;
#endif
    cout << "ee = " << endl;
    cout << ee << endl;
    printf("%g\n", ee);
    printf("%f\n", ee);
    printf("%e\n", ee);
    cout << endl;
    cout << "  ee = " << endl;
    cout << "  " << setprecision(10) << ee << endl;
    printf("  %lg\n", ee);
    printf("  %lf\n", ee);
    printf("  %le\n", ee);
    cout << endl;
    return 0;
}
'프로그래밍 > C++' 카테고리의 다른 글
| g++ 컴파일러로 int128 정수와 float128 부동소수점수 사용하기 (0) | 2025.05.12 | 
|---|---|
| C++ 언어에서 큰 부동소수점수(native double) 의 정확도 (0) | 2023.03.19 | 
| 맥 OS X에 gmplib 6.2.1 설치하고 g++로 설치 테스트하기 (0) | 2022.03.19 | 
| Boost Library를 이용하여 임의의 정밀도를 갖는 부동소수점수 계산하기 (0) | 2021.03.29 | 
| new 키워드로 생성된 배열의 크기 변경하여 재할당하기 (0) | 2021.02.24 |