
C++解せない点(解決済)
解せない
のなかで扱った
<iomanip>のfixed とか setprecision() の話、上の記事では「中学生への説明」という本題から外れるので深入りせず次へ進んだのですが、どうも実行結果に納得ができない。
コピペのミスと思われるかもしれませんが、そんなことはありません。再掲しますよ。環境はCodeBlocks 20.03 にGCCコンパイラ、 C++です。
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
const float x = 3.14159265358979323;
cout << x << endl;
cout << endl;
cout << setprecision(3) << x << endl;
cout << setprecision(4) << x << endl;
cout << setprecision(5) << x << endl;
cout << setprecision(6) << x << endl;
cout << setprecision(7) << x << endl;
cout << setprecision(8) << x << endl;
cout << setprecision(9) << x << endl;
cout << setprecision(10) << x << endl;
cout << endl;
cout << fixed << setprecision(3) << x << endl;
cout << fixed << setprecision(4) << x << endl;
cout << fixed << setprecision(5) << x << endl;
cout << fixed << setprecision(6) << x << endl;
cout << fixed << setprecision(7) << x << endl;
cout << fixed << setprecision(8) << x << endl;
cout << fixed << setprecision(9) << x << endl;
cout << fixed << setprecision(10) << x << endl;
cout << endl;
const double y = 3.14159265358979323;
cout << y << endl;
cout << endl;
cout << setprecision(3) << y << endl;
cout << setprecision(4) << y << endl;
cout << setprecision(5) << y << endl;
cout << setprecision(6) << y << endl;
cout << setprecision(7) << y << endl;
cout << setprecision(8) << y << endl;
cout << setprecision(9) << y << endl;
cout << setprecision(10) << y << endl;
cout << endl;
cout << fixed << setprecision(3) << y << endl;
cout << fixed << setprecision(4) << y << endl;
cout << fixed << setprecision(5) << y << endl;
cout << fixed << setprecision(6) << y << endl;
cout << fixed << setprecision(7) << y << endl;
cout << fixed << setprecision(8) << y << endl;
cout << fixed << setprecision(9) << y << endl;
cout << fixed << setprecision(10) << y << endl;
cout << endl;
return 0;
}
この結果が
3.14159
3.14
3.142
3.1416
3.14159
3.141593
3.1415927
3.14159274
3.141592741
3.142
3.1416
3.14159
3.141593
3.1415927
3.14159274
3.141592741
3.1415927410
3.1415926536
3.142
3.1416
3.14159
3.141593
3.1415927
3.14159265
3.141592654
3.1415926536
3.142
3.1416
3.14159
3.141593
3.1415927
3.14159265
3.141592654
3.1415926536
Process returned 0 (0x0) execution time : 0.128 s
Press any key to continue.
こうなんです。 const float xのところはいいとして、const double y のところがfixed を入れたのも入れていないのも同じ結果になっているというのが奇妙です。
いや、おかしいよなぁ。const float xをconst double xに修正、ちゃんと変更したコードであることを確認するためにModifiedという文字列を表示させるようにして、変更後のコードは
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
const double x = 3.14159265358979323;
cout << "Modified" << endl;
cout << x << endl;
cout << endl;
cout << setprecision(3) << x << endl;
cout << setprecision(4) << x << endl;
cout << setprecision(5) << x << endl;
cout << setprecision(6) << x << endl;
cout << setprecision(7) << x << endl;
cout << setprecision(8) << x << endl;
cout << setprecision(9) << x << endl;
cout << setprecision(10) << x << endl;
cout << endl;
cout << fixed << setprecision(3) << x << endl;
cout << fixed << setprecision(4) << x << endl;
cout << fixed << setprecision(5) << x << endl;
cout << fixed << setprecision(6) << x << endl;
cout << fixed << setprecision(7) << x << endl;
cout << fixed << setprecision(8) << x << endl;
cout << fixed << setprecision(9) << x << endl;
cout << fixed << setprecision(10) << x << endl;
cout << endl;
const double y = 3.14159265358979323;
cout << y << endl;
cout << endl;
cout << setprecision(3) << y << endl;
cout << setprecision(4) << y << endl;
cout << setprecision(5) << y << endl;
cout << setprecision(6) << y << endl;
cout << setprecision(7) << y << endl;
cout << setprecision(8) << y << endl;
cout << setprecision(9) << y << endl;
cout << setprecision(10) << y << endl;
cout << endl;
cout << fixed << setprecision(3) << y << endl;
cout << fixed << setprecision(4) << y << endl;
cout << fixed << setprecision(5) << y << endl;
cout << fixed << setprecision(6) << y << endl;
cout << fixed << setprecision(7) << y << endl;
cout << fixed << setprecision(8) << y << endl;
cout << fixed << setprecision(9) << y << endl;
cout << fixed << setprecision(10) << y << endl;
cout << endl;
return 0;
}
これ実行したらどうなるでしょうか?SaveFileして Build and run しています。こうなりました。
Modified
3.14159
3.14
3.142
3.1416
3.14159
3.141593
3.1415927
3.14159265
3.141592654
3.142
3.1416
3.14159
3.141593
3.1415927
3.14159265
3.141592654
3.1415926536
3.1415926536
3.142
3.1416
3.14159
3.141593
3.1415927
3.14159265
3.141592654
3.1415926536
3.142
3.1416
3.14159
3.141593
3.1415927
3.14159265
3.141592654
3.1415926536
Process returned 0 (0x0) execution time : 0.119 s
Press any key to continue.
いや、なんでよ。なんで冒頭から
Modified
3.14159
なのよ? xもyも同じconst double だよ?
前の結果がクリアされていないの?
途端に教える自信を喪失してしまいました(´;ω;`)
オンラインコンパイラで試す
他に検証できる環境を探していたらこちらの記事で
オンラインコンパイラと言うのがあるのを知りました。GDB Onlineというのがよさそう。これで試しましょう。

C++23でコード貼り付けて

Run

うわぁ・・再現した。
追記
わかったかも?
一度 cout<<fixed
すると、coutにはその設定が残るってことかな?
Copilot先生に聞きます。

そういうことでしたか・・・勉強になりました。