English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ How to use the fdim() function and examples

C++ Library Function <cmath>

C ++The fdim() function in the C standard library takes two parameters and returns the positive difference between the first and second parameters.

fdim() prototype [from C ++ 11Standard starts]

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.

From C ++ 11Starting, if the parameters passed to fdim() are long double, the returned type Promoted is long double. If not, the returned type Promoted is double.

This function in<cmath>Defined in the header file.

The fdim() parameters

The fdim() function takes two floating-point or integer type parameters:

  • x -The first parameter of fdim()

  • y -The second parameter of fdim()

The return value of fdim()

The fdim() function returns:

  • If x > y, return x-y

  • Ifx ≤ y is 0

Example: How does fdim() work?

#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 running the program, the output is:

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

C++ Library Function <cmath>