C++ で浮動小数点数の精度を設定する
- 
          
            C++ で浮動小数点数の精度を設定するには std::setprecisionを使う
- 
          
            std::floorとstd::ceilを使用して浮動小数点数の精度を変更する
- 
          
            std::roundおよびstd::lroundを使用して、浮動小数点数の精度を変更する
 
この記事では、C++ で浮動小数点数の精度を設定する方法のいくつかの方法について説明します。
C++ で浮動小数点数の精度を設定するには std::setprecision を使う
    
std::setprecision は、入力/出力ストリームのフォーマットに使用できる STL I/O マニピュレータライブラリの一部です。setprecision は浮動小数点数の精度を変更し、小数点以下に表示する桁数を指定する整数パラメーターのみを取ります。つまり、浮動小数点数に対して暗黙的に想定されるデフォルトの精度は、コンマの後の 6 桁です。それでも、数が少なすぎてマニピュレータが使用されていない場合、フロートが科学的記数法で表示されることがあります。次のサンプルコードに示すように、このような数値はすべての有効数字を失い、ゼロとして表示される場合があることに注意してください。
#include <iomanip>
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
using std::fixed;
using std::setprecision;
using std::vector;
int main() {
  vector<double> d_vec = {123.231,       2.2343, 0.012,
                          26.9491092019, 113,    0.000000234};
  for (auto &i : d_vec) {
    cout << i << " | ";
  }
  cout << endl;
  for (auto &i : d_vec) {
    cout << setprecision(3) << i << " | ";
  }
  cout << endl;
  return EXIT_SUCCESS;
}
出力:
123.231 | 2.2343 | 0.012 | 26.9491 | 113 | 2.34e-07 |
123.231 | 2.234 | 0.012 | 26.949 | 113.000 | 0.000 |
std::floor と std::ceil を使用して浮動小数点数の精度を変更する
std::floor 関数と std::ceil 関数は、元々C 標準ライブラリに実装されていた <cmath> ヘッダーによって提供されます。ceil 関数は、唯一の引数として渡された浮動小数点以上の最小の整数値を計算します。一方、floor は、引数以下の最大の整数値を計算します。これらの関数は、float、double、および long double タイプに対して定義されています。
#include <cmath>
#include <iomanip>
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
using std::fixed;
using std::vector;
int main() {
  vector<double> d_vec = {123.231,       2.2343, 0.012,
                          26.9491092019, 113,    0.000000234};
  for (auto &i : d_vec) {
    cout << i << " | ";
  }
  cout << endl;
  for (auto &i : d_vec) {
    cout << fixed << std::ceil(i) << " | ";
  }
  cout << endl;
  for (auto &i : d_vec) {
    cout << fixed << std::floor(i) << " | ";
  }
  cout << endl;
  return EXIT_SUCCESS;
}
出力:
123.231 | 2.2343 | 0.012 | 26.9491 | 113 | 2.34e-07 |
124.000000 | 3.000000 | 1.000000 | 27.000000 | 113.000000 | 1.000000 |
123.000000 | 2.000000 | 0.000000 | 26.000000 | 113.000000 | 0.000000 |
std::round および std::lround を使用して、浮動小数点数の精度を変更する
または、std::round と std::round を使用して、ゼロから四捨五入された最も近い整数値を計算できます。これらの関数は、浮動小数点演算関連のエラーをスローする可能性があります。これについては、ページで詳しく説明されています。
#include <cmath>
#include <iomanip>
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
using std::fixed;
using std::vector;
int main() {
  vector<double> d_vec = {123.231,       2.2343, 0.012,
                          26.9491092019, 113,    0.000000234};
  for (auto &i : d_vec) {
    cout << i << " | ";
  }
  cout << endl;
  for (auto &i : d_vec) {
    cout << fixed << std::round(i) << " | ";
  }
  cout << endl;
  for (auto &i : d_vec) {
    cout << fixed << std::lround(i) << " | ";
  }
  cout << endl;
  return EXIT_SUCCESS;
}
出力:
123.231 | 2.2343 | 0.012 | 26.9491 | 113 | 2.34e-07 |
123.000000 | 2.000000 | 0.000000 | 27.000000 | 113.000000 | 0.000000 |
123 | 2 | 0 | 27 | 113 | 0 |
        チュートリアルを楽しんでいますか? <a href="https://www.youtube.com/@delftstack/?sub_confirmation=1" style="color: #a94442; font-weight: bold; text-decoration: underline;">DelftStackをチャンネル登録</a> して、高品質な動画ガイドをさらに制作するためのサポートをお願いします。 Subscribe
    
著者: 胡金庫
    
