C++ fdim()

The fdim() function in C++ takes two arguments and returns the positive difference between first and second argument.

fdim() prototype [As of C++ 11 standard]

double fdim(double x, double y);
float fdim(float x, float y);
long double fdim(long double x, long double y);
Promoted fdim(Type1 x, Type2 y); // For other combinations of arithmetic types.

Since C++11, if any argument passed to fdim() is long double, the return type Promoted is long double. If not, the return type Promoted is double.

This function is defined in <cmath> header file.

fdim() Parameters

The fdim() function take two parameters of either floating-point or integral type:

• x - first argument to fdim()
• y - second argument to fdim()

fdim() Return Value

The fdim() function returns:

• x-y if x > y
• 0 if x ≤ y

Example: How fdim() works?

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
double x = 22.31, y = 13.17, result;
result = fdim(x, y);
cout << "fdim(x, y) = " << result << endl;

long double xLD = -22.31, resultLD;
y = 13.14;
resultLD = fdim(xLD, y);
cout << "fdim(xLD, y) = " << resultLD << endl;

return 0;
}

When you run the program, the output will be:

fdim(x, y) = 9.14
fdim(xLD, yLD) = 0