fdim() in C++


Here we will see what is the fdim() function in C++. The fdim() function is used to return the positive difference between two given arguments. If two arguments are a and b respectively, and if a >b, then it will return a – b. otherwise returns 0.

Example

#include <cmath>
#include <iostream>
using namespace std;
main() {
   cout << "fdim of (5.0, 2.0) is " << fdim(5.0, 2.0) << endl; //positive difference
   cout << "fdim of (2.0, 5.0) is " << fdim(2.0, 5.0) << endl; //this will return 0
   cout << "fdim of (-5.0, -2.0) is " << fdim(-5.0, -2.0) << endl; //this will return 0
   cout << "fdim of (-2.0, -5.0) is " << fdim(-2.0, -5.0) << endl; //positive difference
}

Output

fdim of (5.0, 2.0) is 3
fdim of (2.0, 5.0) is 0
fdim of (-5.0, -2.0) is 0
fdim of (-2.0, -5.0) is 3

Updated on: 30-Jul-2019

89 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements