2.10 入出力の書式 (とりあえずの例)

#include <iostream>

int main(void)
{
  double x=1234.5678;
  std::cout << x << std::endl;
  std::cout << std::scientific << x << std::endl;
  std::cout << std::fixed << x << std::endl;
  std::cout << std::defaultfloat << x << std::endl;
  return 0;
}

二分法のプログラム、元々の C プログラムでは、固定形式も使っていたのだった。


/*
 * bisection.c -- 二分法 (bisection method) で方程式 f(x)=0 を解く
 *   コンパイル: gcc -o bisection bisection.c -lm
 *   実行: ./bisection
 */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(void)
{
  int i, maxitr = 100;
  double alpha, beta, a, b, c, eps;
  double fa, fb, fc;
  double f(double);

  printf(" 探す区間の左端α, 右端β, 許容精度ε=");
  scanf("%lf %lf %lf", &alpha, &beta, &eps);

  a = alpha;
  b = beta;

  fa = f(a);
  fb = f(b);
  if (fa * fb > 0.0) {
    printf(" f(α) f(β) > 0 なのであきらめます。\n");
    exit(0);
  }
  else {
    printf("f(%20.15f)=%9.2e, f(%20.15f)=%9.2e\n", a, fa, b, fb);
    for (i = 0; i < maxitr; i++) {
      c = (a + b) / 2;
      fc = f(c);
      if (fc == 0.0)
        break;
      else if (fa * fc <= 0.0) {
        /* 左側 [a,c] に根がある */
        b = c;
        fb = fc;
      }
      else {
        /* 左側 [a,c] には根がないかもしれない。[c,b] にあるはず */
        a = c;
        fa = fc;
      }
      printf ("f(%20.15f)=%9.2e, f(%20.15f)=%9.2e\n", a, fa, b, fb);
      if ((b - a) <= eps)
        break;
    }
    printf ("f(%20.15f)=%e\n", c, fc);
  }
  return 0;
}

double f(double x)
{
  return cos(x) - x;
}

これを C++ に直すと次のようになる。 std:: つけると、ゲップが出そう。


/*
 * bisection.cpp -- 二分法 (bisection method) で方程式 f(x)=0 を解く
 *   コンパイル: gcc -o bisection bisection.c -lm
 *   実行: ./bisection
 */

#include <iostream>
#include <iomanip>
#include <cmath>

int main(void)
{
  int i, maxitr = 100;
  double alpha, beta, a, b, c, eps;
  double fa, fb, fc;
  double f(double);

  std::ios_base::fmtflags original_flags = std::cout.flags();
  std::cout << " 探す区間の左端α, 右端β, 許容精度ε=";
  std::cin >> alpha >> beta >> eps;

  a = alpha;
  b = beta;

  fa = f(a);
  fb = f(b);
  if (fa * fb > 0.0) {
    std::cout << " f(α) f(β) > 0 なのであきらめます。" << std::endl;
    exit(0);
  }
  else {
    std::cout << "f("
              << std::fixed << std::setw(20) << std::setprecision(15) << a
              << ")="
              << std::scientific << std::setw(9) << std::setprecision(2) << fa
              << ", f("
              << std::fixed << std::setw(20) << std::setprecision(15) << b
              << ")="
              << std::scientific << std::setw(9) << std::setprecision(2) << fb << std::endl;
    for (i = 0; i < maxitr; i++) {
      c = (a + b) / 2;
      fc = f(c);
      if (fc == 0.0)
        break;
      else if (fa * fc <= 0.0) {
        /* 左側 [a,c] に根がある */
        b = c;
        fb = fc;
      }
      else {
        /* 左側 [a,c] には根がないかもしれない。[c,b] にあるはず */
        a = c;
        fa = fc;
      }
      std::cout << "f("
                << std::fixed << std::setw(20) << std::setprecision(15) << a
                << ")="
                << std::scientific << std::setw(9) << std::setprecision(2) << fa
                << ", f("
                << std::fixed << std::setw(20) << std::setprecision(15) << b
                << ")="
                << std::scientific << std::setw(9) << std::setprecision(2) << fb
                << std::endl;
      if ((b - a) <= eps)
        break;
    }
    std::cout << "f("
              << std::fixed << std::setw(20) << std::setprecision(15) << c
              << ")=";
    std::cout.flags(original_flags);     
    std::cout << std::scientific << std::setprecision(6) << fc << std::endl;
  }
  return 0;
}

double f(double x)
{
  return cos(x) - x;
}



桂田 祐史