test-showpos.C |
#include <iostream.h> #include <iomanip.h> int main() { for (int i = 0; i <= 10; i++) { cout << setw(5) << - 1.0 + 0.2 * i; } cout << endl; cout.setf(ios::showpos); for (int i = 0; i <= 10; i++) { cout << setw(5) << - 1.0 + 0.2 * i; } cout << endl; return 0; } |
test-showpos.out |
mathpc00% ./test-showpos -1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1 -1 -0.8 -0.6 -0.4 -0.2 +0 +0.2 +0.4 +0.6 +0.8 +1 mathpc00% |
test-showpoint.C |
#include <iostream.h> #include <iomanip.h> int main() { int i; for (i = 0; i <= 10; i++) { cout << setw(5) << - 1.0 + 0.2 * i << endl; } cout.setf(ios::showpoint); for (i = 0; i <= 10; i++) { cout << setw(5) << - 1.0 + 0.2 * i << endl; } return 0; } |
test-showpoint.out |
mathpc00% ./test-showpoint -1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1 -1.00000 -0.800000 -0.600000 -0.400000 -0.200000 0.00000 0.200000 0.400000 0.600000 0.800000 1.00000 mathpc00% |
test-fixed.C |
#include <iostream.h> #include <iomanip.h> void print() { int i; double x1, x2; x1 = x2 = 1; for (i = 1; i <= 8; i++) { cout << x1 << " " << x2 << endl; x1 *= 8; x2 /= 8; } cout << endl; } int main() { cout << "default" << endl; print(); cout << "cout.setf(ios::fixed, ios::floatfield);" << endl; cout.setf(ios::fixed, ios::floatfield); print(); cout << "resetiosflags(ios::fixed);" << endl; cout << resetiosflags(ios::fixed); print(); // cout << fixed; とはできないのかな? cout << "setiosflags(ios::fixed);" << endl; cout << setiosflags(ios::fixed); print(); // これでもリセットされるか? cout << "resetiosflags(ios::floatfield);" << endl; cout << resetiosflags(ios::floatfield); print(); return 0; } |
test-fixed.out |
mathpc00% ./test-fixed default 1 1 8 0.125 64 0.015625 512 0.00195312 4096 0.000244141 32768 3.05176e-05 262144 3.8147e-06 2.09715e+06 4.76837e-07 cout.setf(ios::fixed, ios::floatfield); 1.000000 1.000000 8.000000 0.125000 64.000000 0.015625 512.000000 0.001953 4096.000000 0.000244 32768.000000 0.000031 262144.000000 0.000004 2097152.000000 0.000000 resetiosflags(ios::fixed); 1 1 8 0.125 64 0.015625 512 0.00195312 4096 0.000244141 32768 3.05176e-05 262144 3.8147e-06 2.09715e+06 4.76837e-07 setiosflags(ios::fixed); 1.000000 1.000000 8.000000 0.125000 64.000000 0.015625 512.000000 0.001953 4096.000000 0.000244 32768.000000 0.000031 262144.000000 0.000004 2097152.000000 0.000000 resetiosflags(ios::floatfield); 1 1 8 0.125 64 0.015625 512 0.00195312 4096 0.000244141 32768 3.05176e-05 262144 3.8147e-06 2.09715e+06 4.76837e-07 mathpc00% |
test-scientific.C |
#include <iostream.h> #include <iomanip.h> void print() { int i; double x1, x2; x1 = x2 = 1; for (i = 1; i <= 8; i++) { cout << x1 << " " << x2 << endl; x1 *= 8; x2 /= 8; } cout << endl; } int main() { cout << "default" << endl; print(); cout << "cout.setf(ios::scientific, ios::floatfield);" << endl; cout.setf(ios::scientific, ios::floatfield); print(); cout << "resetiosflags(ios::scientific);" << endl; cout << resetiosflags(ios::scientific); print(); // #include <ios> とすると cout << scientific; とするだけで良いのか? cout << "setiosflags(ios::scientific);" << endl; cout << setiosflags(ios::scientific); print(); // これでもリセットされるか? cout << "resetiosflags(ios::floatfield);" << endl; cout << resetiosflags(ios::floatfield); print(); return 0; } |
test-scientific.out |
mathpc00% ./test-scientific default 1 1 8 0.125 64 0.015625 512 0.00195312 4096 0.000244141 32768 3.05176e-05 262144 3.8147e-06 2.09715e+06 4.76837e-07 cout.setf(ios::scientific, ios::floatfield); 1.000000e+00 1.000000e+00 8.000000e+00 1.250000e-01 6.400000e+01 1.562500e-02 5.120000e+02 1.953125e-03 4.096000e+03 2.441406e-04 3.276800e+04 3.051758e-05 2.621440e+05 3.814697e-06 2.097152e+06 4.768372e-07 resetiosflags(ios::scientific); 1 1 8 0.125 64 0.015625 512 0.00195312 4096 0.000244141 32768 3.05176e-05 262144 3.8147e-06 2.09715e+06 4.76837e-07 setiosflags(ios::scientific); 1.000000e+00 1.000000e+00 8.000000e+00 1.250000e-01 6.400000e+01 1.562500e-02 5.120000e+02 1.953125e-03 4.096000e+03 2.441406e-04 3.276800e+04 3.051758e-05 2.621440e+05 3.814697e-06 2.097152e+06 4.768372e-07 resetiosflags(ios::floatfield); 1 1 8 0.125 64 0.015625 512 0.00195312 4096 0.000244141 32768 3.05176e-05 262144 3.8147e-06 2.09715e+06 4.76837e-07 mathpc00% |
test-precision.C |
#include <iostream.h> #include <iomanip.h> #include <cmath> int main() { int i; double pi = atan(1.0) * 4; cout << "デフォールトでは精度" << cout.precision() << "桁である" << endl; for (i = 1; i <= 16; i++) { cout.precision(i); cout << setw(2) << i << ":" << pi << endl; } for (i = 1; i <= 16; i++) cout << setw(2) << i << ":" << setprecision(i) << pi << endl; return 0; } |
test-precision.out |
mathpc00% ./test-precision デフォールトでは精度6桁である 1:3 2:3.1 3:3.14 4:3.142 5:3.1416 6:3.14159 7:3.141593 8:3.1415927 9:3.14159265 10:3.141592654 11:3.1415926536 12:3.14159265359 13:3.14159265359 14:3.1415926535898 15:3.14159265358979 16:3.141592653589793 1:3 2:3.1 3:3.14 4:3.142 5:3.1416 6:3.14159 7:3.141593 8:3.1415927 9:3.14159265 10:3.141592654 11:3.1415926536 12:3.14159265359 13:3.14159265359 14:3.1415926535898 15:3.14159265358979 16:3.141592653589793 mathpc00% |
testformat1.C |
#include <iostream.h> void print() { int i; double x1, x2; x1 = x2 = 1; for (i = 1; i <= 30; i++) { cout << x1 << " " << x2 << endl; x1 *= 2; x2 /= 2; } } int main() { cout << "何もしないと C の %g 状態" << endl; print(); cout << "cout.setf(ios::showpoint);" << endl; cout.setf(ios::showpoint); print(); cout << "cout.setf(ios::fixes, ios::floatfield); とすると %f 相当" << endl; cout << "cout.setf(ios::fixed, ios::floatfield);" << endl; cout.setf(ios::fixed, ios::floatfield); print(); cout << "cout.unsetf(ios::showpoint); としても変化なし?" << endl; cout << "cout.unsetf(ios::showpoint);" << endl; cout.unsetf(ios::showpoint); print(); cout << "cout.setf(ios::scientific, ios::floatfield); とすると %e 相当" << endl; cout << "cout.setf(ios::scientific, ios::floatfield);" << endl; cout.setf(ios::scientific, ios::floatfield); print(); cout << "cout.precision(16); ... この場合は小数点以下の桁数" << endl; cout.precision(16); cout << " 12345678901234567890" << endl; print(); cout << "cout.setf(ios::fixed, ios::floatfield);" << endl; cout.setf(ios::fixed, ios::floatfield); cout << " 12345678901234567890" << endl; print(); } |
testformat1.out |
mathpc00% g++ -o testformat1.C mathpc00% ./testformat1 何もしないと C の %g 状態 1 1 2 0.5 4 0.25 8 0.125 16 0.0625 32 0.03125 64 0.015625 128 0.0078125 256 0.00390625 512 0.00195312 1024 0.000976562 2048 0.000488281 4096 0.000244141 8192 0.00012207 16384 6.10352e-05 32768 3.05176e-05 65536 1.52588e-05 131072 7.62939e-06 262144 3.8147e-06 524288 1.90735e-06 1.04858e+06 9.53674e-07 2.09715e+06 4.76837e-07 4.1943e+06 2.38419e-07 8.38861e+06 1.19209e-07 1.67772e+07 5.96046e-08 3.35544e+07 2.98023e-08 6.71089e+07 1.49012e-08 1.34218e+08 7.45058e-09 2.68435e+08 3.72529e-09 5.36871e+08 1.86265e-09 cout.setf(ios::showpoint); 1.00000 1.00000 2.00000 0.500000 4.00000 0.250000 8.00000 0.125000 16.0000 0.0625000 32.0000 0.0312500 64.0000 0.0156250 128.000 0.00781250 256.000 0.00390625 512.000 0.00195312 1024.00 0.000976562 2048.00 0.000488281 4096.00 0.000244141 8192.00 0.000122070 16384.0 6.10352e-05 32768.0 3.05176e-05 65536.0 1.52588e-05 131072. 7.62939e-06 262144. 3.81470e-06 524288. 1.90735e-06 1.04858e+06 9.53674e-07 2.09715e+06 4.76837e-07 4.19430e+06 2.38419e-07 8.38861e+06 1.19209e-07 1.67772e+07 5.96046e-08 3.35544e+07 2.98023e-08 6.71089e+07 1.49012e-08 1.34218e+08 7.45058e-09 2.68435e+08 3.72529e-09 5.36871e+08 1.86265e-09 cout.setf(ios::fixes, ios::floatfield); とすると %f 相当 cout.setf(ios::fixed, ios::floatfield); 1.000000 1.000000 2.000000 0.500000 4.000000 0.250000 8.000000 0.125000 16.000000 0.062500 32.000000 0.031250 64.000000 0.015625 128.000000 0.007812 256.000000 0.003906 512.000000 0.001953 1024.000000 0.000977 2048.000000 0.000488 4096.000000 0.000244 8192.000000 0.000122 16384.000000 0.000061 32768.000000 0.000031 65536.000000 0.000015 131072.000000 0.000008 262144.000000 0.000004 524288.000000 0.000002 1048576.000000 0.000001 2097152.000000 0.000000 4194304.000000 0.000000 8388608.000000 0.000000 16777216.000000 0.000000 33554432.000000 0.000000 67108864.000000 0.000000 134217728.000000 0.000000 268435456.000000 0.000000 536870912.000000 0.000000 cout.unsetf(ios::showpoint); としても変化なし? cout.unsetf(ios::showpoint); 1.000000 1.000000 2.000000 0.500000 4.000000 0.250000 8.000000 0.125000 16.000000 0.062500 32.000000 0.031250 64.000000 0.015625 128.000000 0.007812 256.000000 0.003906 512.000000 0.001953 1024.000000 0.000977 2048.000000 0.000488 4096.000000 0.000244 8192.000000 0.000122 16384.000000 0.000061 32768.000000 0.000031 65536.000000 0.000015 131072.000000 0.000008 262144.000000 0.000004 524288.000000 0.000002 1048576.000000 0.000001 2097152.000000 0.000000 4194304.000000 0.000000 8388608.000000 0.000000 16777216.000000 0.000000 33554432.000000 0.000000 67108864.000000 0.000000 134217728.000000 0.000000 268435456.000000 0.000000 536870912.000000 0.000000 cout.setf(ios::scientific, ios::floatfield); とすると %e 相当 cout.setf(ios::scientific, ios::floatfield); 1.000000e+00 1.000000e+00 2.000000e+00 5.000000e-01 4.000000e+00 2.500000e-01 8.000000e+00 1.250000e-01 1.600000e+01 6.250000e-02 3.200000e+01 3.125000e-02 6.400000e+01 1.562500e-02 1.280000e+02 7.812500e-03 2.560000e+02 3.906250e-03 5.120000e+02 1.953125e-03 1.024000e+03 9.765625e-04 2.048000e+03 4.882812e-04 4.096000e+03 2.441406e-04 8.192000e+03 1.220703e-04 1.638400e+04 6.103516e-05 3.276800e+04 3.051758e-05 6.553600e+04 1.525879e-05 1.310720e+05 7.629395e-06 2.621440e+05 3.814697e-06 5.242880e+05 1.907349e-06 1.048576e+06 9.536743e-07 2.097152e+06 4.768372e-07 4.194304e+06 2.384186e-07 8.388608e+06 1.192093e-07 1.677722e+07 5.960464e-08 3.355443e+07 2.980232e-08 6.710886e+07 1.490116e-08 1.342177e+08 7.450581e-09 2.684355e+08 3.725290e-09 5.368709e+08 1.862645e-09 cout.precision(16); ... この場合は小数点以下の桁数 12345678901234567890 1.0000000000000000e+00 1.0000000000000000e+00 2.0000000000000000e+00 5.0000000000000000e-01 4.0000000000000000e+00 2.5000000000000000e-01 8.0000000000000000e+00 1.2500000000000000e-01 1.6000000000000000e+01 6.2500000000000000e-02 3.2000000000000000e+01 3.1250000000000000e-02 6.4000000000000000e+01 1.5625000000000000e-02 1.2800000000000000e+02 7.8125000000000000e-03 2.5600000000000000e+02 3.9062500000000000e-03 5.1200000000000000e+02 1.9531250000000000e-03 1.0240000000000000e+03 9.7656250000000000e-04 2.0480000000000000e+03 4.8828125000000000e-04 4.0960000000000000e+03 2.4414062500000000e-04 8.1920000000000000e+03 1.2207031250000000e-04 1.6384000000000000e+04 6.1035156250000000e-05 3.2768000000000000e+04 3.0517578125000000e-05 6.5536000000000000e+04 1.5258789062500000e-05 1.3107200000000000e+05 7.6293945312500000e-06 2.6214400000000000e+05 3.8146972656250000e-06 5.2428800000000000e+05 1.9073486328125000e-06 1.0485760000000000e+06 9.5367431640625000e-07 2.0971520000000000e+06 4.7683715820312500e-07 4.1943040000000000e+06 2.3841857910156250e-07 8.3886080000000000e+06 1.1920928955078125e-07 1.6777216000000000e+07 5.9604644775390625e-08 3.3554432000000000e+07 2.9802322387695312e-08 6.7108864000000000e+07 1.4901161193847656e-08 1.3421772800000000e+08 7.4505805969238281e-09 2.6843545600000000e+08 3.7252902984619141e-09 5.3687091200000000e+08 1.8626451492309570e-09 cout.setf(ios::fixed, ios::floatfield); 12345678901234567890 1.0000000000000000 1.0000000000000000 2.0000000000000000 0.5000000000000000 4.0000000000000000 0.2500000000000000 8.0000000000000000 0.1250000000000000 16.0000000000000000 0.0625000000000000 32.0000000000000000 0.0312500000000000 64.0000000000000000 0.0156250000000000 128.0000000000000000 0.0078125000000000 256.0000000000000000 0.0039062500000000 512.0000000000000000 0.0019531250000000 1024.0000000000000000 0.0009765625000000 2048.0000000000000000 0.0004882812500000 4096.0000000000000000 0.0002441406250000 8192.0000000000000000 0.0001220703125000 16384.0000000000000000 0.0000610351562500 32768.0000000000000000 0.0000305175781250 65536.0000000000000000 0.0000152587890625 131072.0000000000000000 0.0000076293945312 262144.0000000000000000 0.0000038146972656 524288.0000000000000000 0.0000019073486328 1048576.0000000000000000 0.0000009536743164 2097152.0000000000000000 0.0000004768371582 4194304.0000000000000000 0.0000002384185791 8388608.0000000000000000 0.0000001192092896 16777216.0000000000000000 0.0000000596046448 33554432.0000000000000000 0.0000000298023224 67108864.0000000000000000 0.0000000149011612 134217728.0000000000000000 0.0000000074505806 268435456.0000000000000000 0.0000000037252903 536870912.0000000000000000 0.0000000018626451 mathpc00% |