fdim() in C++

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

This function returns a-b if a>b otherwise returns 0. where, a is the first argument and b
is the second argument.

Syntax:



double fdim(double a, double b);
float fdim(float a, float b);

Parameter:

The fdim() function takes two arguments a and b, where,  a is the first argument and b 
is the second argument.

Return:

This function returns a-b if a>b otherwise returns 0.

Error:

It is mandatory to give both the arguments otherwise it will give error
no matching function for call to 'fdim()' like this.

Examples:

Inuput : fdim(2.0, 1.0)
Output : 1

Inuput : fdim(-2.0, 1.0)
Output : 0

# CODE 1

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <cmath>
#include <iostream>
using namespace std;
  
int main()
{
    // positive difference for 2.0 and 1.0 is 1
    cout << "fdim of (2.0, 1.0) is " << fdim(2.0, 1.0) << endl;
  
    // here 1.0<2.0 so the output is 0
    cout << "fdim of (1.0, 2.0) is " << fdim(1.0, 2.0) << endl;
  
    // here -2.0<-1.0 so the output is 0
    cout << "fdim of (-2.0, -1.0) is " << fdim(-2.0, -1.0) << endl;
  
    // positive difference for -1.0 and -2.0 is 1
    cout << "fdim of (-1.0, -2.0) is " << fdim(-1.0, -2.0) << endl;
  
    return 0;
}

chevron_right


OUTPUT :

fdim of (2.0, 1.0) is 1
fdim of (1.0, 2.0) is 0
fdim of (-2.0, -1.0) is 0
fdim of (-1.0, -2.0) is 1

# CODE 2

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <cmath>
#include <iostream>
using namespace std;
  
int main()
{
    // positive difference for 5.0 and 4.0 is 1
    cout << "fdim of (5.0, 4.0) is " << fdim(5.0, 4.0) << endl;
  
    // here 4.0<5.0 so the output is 0
    cout << "fdim of (4.0, 5.0) is " << fdim(4.0, 5.0) << endl;
  
    // here -5.0<-4.0 so the output is 0
    cout << "fdim of (-5.0, -4.0) is " << fdim(-5.0, -4.0) << endl;
  
    // positive difference for 5.0 and 4.0 is 1
    cout << "fdim of (-4.0, -5.0) is " << fdim(-4.0, -5.0) << endl;
  
    return 0;
}

chevron_right


OUTPUT :

fdim of (5.0, 4.0) is 1
fdim of (4.0, 5.0) is 0
fdim of (-5.0, -4.0) is 0
fdim of (-4.0, -5.0) is 1


My Personal Notes arrow_drop_up

pawanasipugmailcom

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.