Open In App

fdim() in C++

Last Updated : 06 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Time Complexity: O(1)

Space Complexity: O(1)

Examples: 

Input : fdim(2.0, 1.0)
Output : 1

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

# CODE 1 

C++




#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;
}


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 

C++




#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;
}


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


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments