프로그래밍/C++
부동소수점수의 소수점 아래에 홑 따옴표 문자 ' 붙여서 가독성 높이기
Scripter
2023. 1. 3. 11:52
부동소수점수에 '를 붙이면 가독성이 좋아 실수를 덜 합니다.
아래의 소스는 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;
}