next up previous contents
Next: D.2.7 浮動小数点数の書式指定 (2) 安直な Up: D.2 簡単な入門 Previous: D.2.5 8進数, 10進数, 16進数

D.2.6 浮動小数点数の書式指定 (1)

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%


next up previous contents
Next: D.2.7 浮動小数点数の書式指定 (2) 安直な Up: D.2 簡単な入門 Previous: D.2.5 8進数, 10進数, 16進数
Masashi Katsurada
平成18年4月28日