fdim() in C++
Last Updated :
06 Feb, 2023
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()
{
cout << "fdim of (2.0, 1.0) is " << fdim(2.0, 1.0) << endl;
cout << "fdim of (1.0, 2.0) is " << fdim(1.0, 2.0) << endl;
cout << "fdim of (-2.0, -1.0) is " << fdim(-2.0, -1.0) << endl;
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()
{
cout << "fdim of (5.0, 4.0) is " << fdim(5.0, 4.0) << endl;
cout << "fdim of (4.0, 5.0) is " << fdim(4.0, 5.0) << endl;
cout << "fdim of (-5.0, -4.0) is " << fdim(-5.0, -4.0) << endl;
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
Share your thoughts in the comments
Please Login to comment...