5 絶対値の大きな数

large.c

/*
 * large.c
 */

#include <stdio.h>
#include "bdsd.h"

int main()
{
  int i;
  double x;
  bdsd s;
  x = 1;
  for (i = 1; i <= 1040; i++) {
    double_to_bin_digits(x, s);
    printf("2^%-4d=", i-1); print_bdsd(s); printf("\n=%g\n", x);
    x *= 2;
  }
  return 0;
}

large 実行結果

takebe% ./large
2^0   =0 01111111111 0000000000000000000000000000000000000000000000000000
=1
2^1   =0 10000000000 0000000000000000000000000000000000000000000000000000
=2
2^2   =0 10000000001 0000000000000000000000000000000000000000000000000000
=4
2^3   =0 10000000010 0000000000000000000000000000000000000000000000000000
=8
2^4   =0 10000000011 0000000000000000000000000000000000000000000000000000
=16
2^5   =0 10000000100 0000000000000000000000000000000000000000000000000000
=32
中略
2^1020=0 11111111011 0000000000000000000000000000000000000000000000000000
=1.12356e+307
2^1021=0 11111111100 0000000000000000000000000000000000000000000000000000
=2.24712e+307
2^1022=0 11111111101 0000000000000000000000000000000000000000000000000000
=4.49423e+307
2^1023=0 11111111110 0000000000000000000000000000000000000000000000000000
=8.98847e+307
Floating exception (core dumped)
takebe%

今度は倍々ゲームで $2^{1023}$ の次に $2^{1024}$ を計算しようとして、 例外が発生して終了した。

もちろん (既に説明したように)、 表現可能な最大の数は $2^{1023}$ よりも大きく、 $2^{1024}(1-(1/2)^{53})\kinji 1.797\times10^{308}$ である。

large2.c

/*
 * large2.c
 */

#include <stdio.h>
#include "bdsd.h"

int main()
{
  double x;
  bdsd s;

  x = 1.7976931348623157e308;
  double_to_bin_digits(x, s);
  print_bdsd(s); printf("\n=%25.20e\n", x);
  x = 1.8e308;
  double_to_bin_digits(x, s);
  print_bdsd(s); printf("\n=%25.20e\n", x);
  x = -1.8e308;
  double_to_bin_digits(x, s);
  print_bdsd(s); printf("\n=%25.20e\n", x);

  return 0;
}

large2 実行結果

takebe% ./large2
0 11111111110 1111111111111111111111111111111111111111111111111111
=1.79769313486231570815e+308
0 11111111111 0000000000000000000000000000000000000000000000000000
=                      Inf
1 11111111111 0000000000000000000000000000000000000000000000000000
=                     -Inf
takebe% 

桂田 祐史
2019-01-20