C++ Standard Library C++ STL Library
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

C++ <cmath> - fdim() Function

The C++ <cmath> 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)


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


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

Return Value

Returns the positive difference between x and y.


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

#include <iostream>
#include <cmath>

using namespace std;
int main () {

  cout<<"fdim(10, 5): "<<fdim(10, 5)<<"\n";
  cout<<"fdim(5, 10): "<<fdim(5, 10)<<"\n";
  cout<<"fdim(10, 10): "<<fdim(10, 10)<<"\n"; 
  return 0;

The output of the above code will be:

fdim(10, 5): 5
fdim(5, 10): 0
fdim(10, 10): 0

❮ C++ <cmath> Library