Math Fdim Function - C++ Programming Tutorial
C++ Course / Math Functions / Math Fdim Function

Math Fdim Function

BLUF: Mastering Math Fdim Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Math Fdim Function

C++ is renowned for its efficiency. Learn how Math Fdim Function enables low-level control and high-performance computing in the tutorial below.

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

Example

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'.

Example

#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:

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'.

Example

#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:

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.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience