C Standard Library

C <math.h> - fdim() Function



The C <math.h> fdim() function returns positive difference between x and y, if x > y, else returns zero. Mathematically, it can be expressed as:

fdim (x, y) = max (x-y, 0)

Syntax

double fdim  (double x, double y);
float fdimf (float x, float y);
long double fdiml (long double x, long double y);                                    

Parameters

x Specify the first value.
y Specify the second value.

Return Value

Returns the positive difference between x and y.

Example:

The example below shows the usage of fdim() function.

#include <stdio.h>
#include <math.h>
 
int main () {

  printf("fdim(10, 5): %f\n", fdim(10, 5));
  printf("fdim(5, 10): %f\n", fdim(5, 10));
  printf("fdim(10, 10): %f\n", fdim(10, 10)); 
  return 0;
}

The output of the above code will be:

fdim(10, 5): 5.000000
fdim(5, 10): 0.000000
fdim(10, 10): 0.000000

❮ C <math.h> Library