function
<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
Positive difference
Returns the positive difference between x and y.

The function returns x-y if x>y, and zero otherwise.

Header <tgmath.h> provides a type-generic macro version of this function.
Additional overloads are provided in this header (<cmath>) for other combinations of arithmetic types (Type1 and Type2): These overloads effectively cast its arguments to double before calculations, except if at least one of the arguments is of type long double (in which case both are casted to long double instead).

Parameters

x, y
Values whose difference is calculated.

Return Value

The positive difference between x and y.

Example

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;
}

Output:

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


See also