コンテンツにスキップ

C言語/double

出典: フリー教科書『ウィキブックス(Wikibooks)』

概要

[編集]

double は C 言語における 倍精度浮動小数点数 (double precision floating-point number) を表すデータ型です。IEEE 754 規格に基づく 64 ビット浮動小数点数 として実装されており、高精度の小数計算 に使用されます。

C 言語の浮動小数点数型には float (単精度) と long double (拡張精度) もありますが、double精度と速度のバランスが良いため、一般的な浮動小数点演算で最もよく使用されます

double 型の仕様

[編集]
項目 値 (IEEE 754 64 ビット)
ビット数 64 ビット
符号ビット 1 ビット
指数部 11 ビット (バイアス 1023)
仮数部 52 ビット
表現可能範囲 約 ±2.22 × 10⁻³⁰⁸ ~ ±1.80 × 10³⁰⁸
精度 (有効桁数) 約 15 ~ 17 桁
デフォルトのリテラル型 double x = 3.14; (f を付けない場合)

C における double 型のサイズは sizeof(double) で確認できます。

#include <stdio.h>
int main() {
    printf("Size of double: %zu bytes\n", sizeof(double));
    return 0;
}

出力:

Size of double: 8 bytes

double 型のリテラル

[編集]

C 言語では、浮動小数点リテラルはデフォルトで double として扱われます。明示的に double とするための接尾辞は不要です。

double a = 3.14;    // double 型
float  b = 3.14f;   // float 型 (f を付ける)
long double c = 3.14L; // long double 型 (L を付ける)

指数表記 (e または E を使用) も可能です。

double x = 1.23e4;  // 1.23 × 10⁴ = 12300.0
double y = 5.67E-2; // 5.67 × 10⁻² = 0.0567

double の範囲と特殊値

[編集]

double 型は 正の無限大 (+∞)、負の無限大 (-∞)、非数 (NaN) を扱うことができます。

特殊値 説明
+∞ オーバーフロー時に発生
-∞ 負のオーバーフロー時に発生
NaN 非数 (0 除算や未定義演算)

これらの値は math.hisfinite()isnan() などの関数で確認できます。

#include <stdio.h>
#include <math.h>
int main() {
    double x = 1.0 / 0.0;  // 正の無限大
    double y = 0.0 / 0.0;  // NaN

    printf("x = %f, isfinite(x): %d\n", x, isfinite(x));
    printf("y = %f, isnan(y): %d\n", y, isnan(y));

    return 0;
}

出力:

x = inf, isfinite(x): 0
y = nan, isnan(y): 1

double の演算

[編集]

double加減乗除、剰余、累乗、三角関数 などの演算が可能ですが、浮動小数点誤差 (丸め誤差) に注意が必要です。

#include <stdio.h>
int main() {
    double a = 0.1 + 0.2;
    printf("0.1 + 0.2 = %.17f\n", a); // 期待値: 0.3

    return 0;
}

出力:

0.1 + 0.2 = 0.30000000000000004

これは 浮動小数点誤差 によるもので、特に金融計算などでは decimal 型を使うべきです。

double の比較と誤差対策

[編集]

浮動小数点数を直接 == で比較すると誤差の影響で正しく判定できない場合があります。そのため、「許容誤差 (epsilon)」を考慮した比較 が推奨されます。

#include <stdio.h>
#include <math.h>
#define EPSILON 1e-9  // 許容誤差
int main() {
    double a = 0.1 + 0.2;
    double b = 0.3;

    if (fabs(a - b) < EPSILON) {
        printf("a と b はほぼ等しい\n");
    } else {
        printf("a と b は異なる\n");
    }

    return 0;
}

出力:

a と b はほぼ等しい

double の使用例

[編集]

1. double で三角関数を計算

[編集]
#include <stdio.h>
#include <math.h>
int main() {
    double angle = 30.0;
    double radians = angle * (M_PI / 180.0);  // 度をラジアンに変換

    printf("sin(30°) = %f\n", sin(radians));
    printf("cos(30°) = %f\n", cos(radians));

    return 0;
}

出力:

sin(30°) = 0.500000
cos(30°) = 0.866025

2. double 配列の合計と平均

[編集]
#include <stdio.h>
int main() {
    double numbers[] = {1.234, 2.345, 3.456, 4.567, 5.678};
    int size = sizeof(numbers) / sizeof(numbers[0]);
    
    double sum = 0.0;
    for (int i = 0; i < size; i++) {
        sum += numbers[i];
    }

    double avg = sum / size;
    printf("合計: %f, 平均: %f\n", sum, avg);

    return 0;
}

出力:

合計: 17.280000, 平均: 3.456000

まとめ

[編集]
  • double倍精度浮動小数点数 (64 ビット) を表すデータ型。
  • float より 高精度 (15 ~ 17 桁) で、科学技術計算や金融計算などで使用される。
  • double のリテラルは デフォルトで double (fL は不要)。
  • 浮動小数点誤差が発生する ため、比較時には EPSILON を考慮する。
  • math.h の関数 (sin(), cos(), isnan() など) と組み合わせて使用する。