The fdim functions (C99) - fdim, fdimf, fdiml

2 つの引数の正の差 (positive difference) を計算します.

fdim (C99)

#include <math.h>
double fdim(
    double x,
    double y
);

fdim 関数は 2 つの引数の正の差を計算し,double 型で返します.

fdimf (C99)

#include <math.h>
float fdimf(
    float x,
    float y
);

fdimf 関数は 2 つの引数の正の差を計算し,float 型で返します.

fdiml (C99)

#include <math.h>
long double fdiml(
    long double x,
    long double y
);

fdiml 関数は 2 つの引数の正の差を計算し, long double 型で返します.

fdim,fdimf,fdiml 関数を実行すると,値域エラー (range error) が発生することがあります.

戻り値

  • x > yの場合: x - y
  • x <= yの場合: 0

C言語サンプルプログラム

以下に fdim 関数を使用したサンプルプログラムを示します.

/* header files */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

/* main */
int main(void) {
    double x1, x2, y1, y2;

    x1 = 2.1;
    x2 = 2.0;
    y1 = fdim(x1, x2);
    printf("fdim(%.1lf, %.1lf) = %.2lf\n", x1, x2, y1);

    x1 = 2.0;
    x2 = 2.1;
    y2 = fdim(x1, x2);
    printf("fdim(%.1lf, %.1lf) = %.2lf\n", x1, x2, y2);

    return EXIT_SUCCESS;
}

実行例

サンプルプログラムの実行結果は以下のようになります.

fdim(2.1, 2.0) = 0.10
fdim(2.0, 2.1) = 0.00