In this C++ tutorial, you will learn how to find the positive different between two given numbers using fdim() function of cmath, with syntax and examples.
C++ fdim()
C++ fdim(x, y) returns positive different between x and y.
fdim() returns x-y if x>=0, else it returns 0.
Syntax
The syntax of C++ fdim() is
fdim(x, y)
where
Parameter | Description |
---|---|
x | A double, float, long double, or integral type value. |
y | A double, float, long double, or integral type value. |
Returns
The return value depends on the type of value passed for parameters x and y. The return value of fdim() is
- double if x and y are double.
- float if x and y are float.
- long double if x and y are long double.
- Promoted to the higher of the given arguments for integral type arguments.
The synopsis of fdim() function is
double fdim(double x, double y); float fdim(float x, float y); long double fdim(long double x, long double y); Promoted fdim(Type1 x, Type2 y); // for integral type argument values
fdim() is a function of cmath library. Include cmath library at the start of program, if using fdim() function.
Example
In this example, we read two values from user into x and y, and find the positive difference of x and y using fdim(x, y) function.
C++ Program
#include <iostream> #include<cmath> using namespace std; int main() { double x, y; cout << "Enter x : "; cin >> x; cout << "Enter y : "; cin >> y; double result = fdim(x, y); cout << "fdim(x, y) : " << result << endl; }
Output
Enter x : 24 Enter y : 15 fdim(x, y) : 9 Program ended with exit code: 0
Since x-y is greater than 0, fdim(x, y) returns the difference of x and y.
Run#2
Enter x : 23 Enter y : 68 fdim(x, y) : 0 Program ended with exit code: 0
Since x-y is greater than 0, fdim(x, y) returns 0.
Conclusion
In this C++ Tutorial, we learned the syntax of C++ fdim(), and how to use this function to find the positive difference, with the help of examples.