fdim() Function

The fdim() function in C computes the positive difference between two values. It returns the result of subtracting the second value from the first when the first is larger, and returns zero if not, ensuring that the outcome is always non-negative.


Syntax of fdim()

</>
Copy
double fdim(double x, double y);
float fdimf(float x, float y);
long double fdiml(long double x, long double y);

Parameters

ParameterDescription
xFirst value used in the calculation.
ySecond value used in the calculation.

It is important to note that if the first value is not greater than the second, the function returns zero. This behavior guarantees that the result is always a positive number or zero, making it useful for computations that require non-negative differences.

Return Value

The function returns the positive difference between the two values, which is calculated as the first value minus the second if the first is greater, or zero otherwise.


Examples for fdim()

Example 1: Basic Positive Difference Calculation

This example demonstrates how to calculate the positive difference between two numbers when the first value exceeds the second.

Program

</>
Copy
#include <stdio.h>
#include <math.h>

int main() {
    double a = 10.0, b = 4.0;
    double result = fdim(a, b);
    printf("Positive difference between %.2f and %.2f is %.2f\n", a, b, result);
    return 0;
}

Explanation:

  1. The program initializes two double variables, a with 10.0 and b with 4.0.
  2. The fdim() function computes the positive difference, returning 6.0 because 10.0 is greater than 4.0.
  3. The resulting value is then printed to the console.

Program Output:

Positive difference between 10.00 and 4.00 is 6.00

Example 2: Handling Non-Positive Difference Case

This example illustrates the scenario where the first value is less than the second, leading to a zero result from the fdim() function.

Program

</>
Copy
#include <stdio.h>
#include <math.h>

int main() {
    double a = 3.0, b = 7.0;
    double result = fdim(a, b);
    printf("Positive difference between %.2f and %.2f is %.2f\n", a, b, result);
    return 0;
}

Explanation:

  1. The program initializes two double variables, a with 3.0 and b with 7.0.
  2. Since a is less than b, the fdim() function returns 0.0.
  3. The program prints the result, showing that the positive difference in this case is zero.

Program Output:

Positive difference between 3.00 and 7.00 is 0.00

Example 3: Calculating Positive Difference with Floating-Point Values

This example uses the single-precision version fdimf() to calculate the positive difference between two floating-point numbers.

Program

</>
Copy
#include <stdio.h>
#include <math.h>

int main() {
    float a = 5.75f, b = 2.30f;
    float result = fdimf(a, b);
    printf("Positive difference between %.2f and %.2f is %.2f\n", a, b, result);
    return 0;
}

Explanation:

  1. The program initializes two float variables, a with 5.75 and b with 2.30.
  2. The fdimf() function calculates the positive difference, returning 3.45 since 5.75 is greater than 2.30.
  3. The output is printed, demonstrating the function’s precision with floating-point numbers.

Program Output:

Positive difference between 5.75 and 2.30 is 3.45

Example 4: Extended Precision Calculation Using fdiml()

This example demonstrates the use of the extended precision version fdiml() to calculate the positive difference for long double values.

Program

</>
Copy
#include <stdio.h>
#include <math.h>

int main() {
    long double a = 12.345678901234L, b = 7.654321098765L;
    long double result = fdiml(a, b);
    printf("Positive difference between %.12Lf and %.12Lf is %.12Lf\n", a, b, result);
    return 0;
}

Explanation:

  1. The program initializes two long double variables with extended precision values.
  2. The fdiml() function computes the positive difference accurately, returning the subtraction result with high precision.
  3. The result is printed with 12 decimal places, demonstrating the extended precision capabilities.

Program Output:

Positive difference between 12.345678901234 and 7.654321098765 is 4.691357802469