函式
<cmath> <ctgmath>

fdim

     double fdim  (double x     , double y);      float fdimf (float x      , float y);long double fdiml (long double x, long double y);
     double fdim (double x     , double y);      float fdim (float x      , float y);long double fdim (long double x, long double y);     double fdim (Type1 x      , Type2 y);       // additional overloads
正差
返回 xy正差

如果 x>y,則該函式返回 x-y,否則返回零。

標頭檔案 <tgmath.h> 提供了此函式的型別通用宏版本。
在此標頭檔案中(<cmath>)提供了額外的過載,用於其他 算術型別Type1Type2)的組合:這些過載在計算前有效地將引數轉換為 double,除非至少有一個引數的型別是 long double(在這種情況下,兩個引數都將轉換為 long double)。

引數

x, y
計算差值的值。

返回值

xy正差

示例

1
2
3
4
5
6
7
8
9
10
11
12
/* fdim example */
#include <stdio.h>      /* printf */
#include <math.h>       /* fdim */

int main ()
{
  printf ("fdim (2.0, 1.0) = %f\n", fdim(2.0,1.0));
  printf ("fdim (1.0, 2.0) = %f\n", fdim(1.0,2.0));
  printf ("fdim (-2.0, -1.0) = %f\n", fdim(-2.0,-1.0));
  printf ("fdim (-1.0, -2.0) = %f\n", fdim(-1.0,-2.0));
  return 0;
}

輸出

fdim (2.0, 1.0) = 1.000000
fdim (1.0, 2.0) = 0.000000
fdim (-2.0,-1.0) = 0.000000
fdim (-1.0,-2.0) = 1.000000


另見