C++ Math fdim
The function computes the absolute value of the subtraction between two numbers.
Conditions :
Consider two numbers 'x' and 'y' :
If the condition (x is greater than y) is true, the function will return the result of subtracting y from x. If the condition (y is greater than x) is true, the function will return zero.
Syntax
float fdim(float x, float y);
double fdim(double x, double y);
long double fdim(long double x, long double y);
promoted fdim(Arithmetic x, Arithmetic y);
Note: If any argument has an integral type, then it is cast to double. If any other argument is long double, then it is cast to long double.
Parameter
The values to be used for calculating the difference are denoted as (x,y).
Return value
It calculates the absolute value of the difference between x and y.
Example 1
Let's examine a straightforward scenario where the value assigned to variable 'x' exceeds the value assigned to variable 'y'.
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
float x=9.4;
float y=8.3;
std::cout <<"Values of x and y are :"<<x<<","<<y<< std::endl;
cout<<"Positive difference between two numbers is :"<<fdim(x,y);
return 0;
}
Output:
Values of x and y are :9.4,8.3
Positive difference between two numbers is :1.1
In this instance, the value assigned to x exceeds the value assigned to y, and the fdim function calculates the non-negative difference between x and y.
Example 2
Let's consider a straightforward scenario where the value assigned to 'x' is smaller than the value assigned to 'y'.
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
double x=3.3;
float y= 4.7;
std::cout <<"Values of x and y are :"<<x<<","<<y<< std::endl;
cout<<"Positive difference between two numbers is :"<<fdim(x,y);
return 0;
}
Output:
Values of x and y are :3.3,4.7
Positive difference between two numbers is :0
In this instance, when the value of x is smaller than the value of y, the fdim function will yield a result of zero.