C fdim() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
math.h

Definition

In C, fdim() returns the positive difference of x and y. If x > y, the result is x - y. Otherwise it is 0.

You pass two double values. The result is a non-negative double. Include <math.h>. On GCC, link with -lm.

Syntax

Pass the first value, then the second. Start with this call.

C
fdim(5.0, 2.0);
CallWhat it does
fdim(5.0, 2.0)Returns 3.0 because 5 > 2.
fdim(2.0, 5.0)Returns 0.0 because 2 is not greater than 5.

Include <math.h>. On GCC, link with -lm. This is not the same as fabs(x - y).

Library declarations
C
double fdim(double x, double y);
float fdimf(float x, float y);
long double fdiml(long double x, long double y);

Parameters

ParameterDescription
xRequired. First operand of the positive difference.
yRequired. Second operand. Subtracted when x is larger.

Return Value

Return ValueDescription
Non-negative doublex - y when x > y; otherwise 0. Never returns a negative result.

C Examples

Example 1 — Basic Positive Difference

Show both the positive case and the zero case.

C
#include <stdio.h>
#include <math.h>

int main(void) {
    printf("%.1f\n", fdim(5.0, 2.0));
    printf("%.1f\n", fdim(2.0, 5.0));
    return 0;
}

How It Works

  1. Include math.h. That declares fdim.
  2. x larger. 5 − 2 = 3.
  3. x smaller. Result clamps to 0.
  4. Non-negative. You never get a minus sign back.

Example 2 — Amount Above a Threshold

Measure how far a reading sits above a limit.

C
#include <stdio.h>
#include <math.h>

int main(void) {
    double reading = 81.5;
    double limit = 80.0;
    double over = fdim(reading, limit);

    printf("over by %.1f\n", over);
    return 0;
}

How It Works

  1. Read a value. 81.5 is the sample.
  2. Set a limit. 80.0 is the threshold.
  3. Call fdim. Only the excess is kept.
  4. Below limit. Would print 0.0 instead.

Example 3 — Compare with fabs

Absolute difference and positive difference are different tools.

C
#include <stdio.h>
#include <math.h>

int main(void) {
    double x = 2.0;
    double y = 5.0;

    printf("fdim = %.1f\n", fdim(x, y));
    printf("fabs = %.1f\n", fabs(x - y));
    return 0;
}

How It Works

  1. Same inputs. x = 2, y = 5.
  2. fdim clamps. Because x < y, result is 0.
  3. fabs keeps distance. Absolute gap is 3.
  4. Pick intentionally. Choose based on whether order matters.

Frequently Asked Questions

fdim(x, y) returns x - y when x > y, otherwise 0.
x and y are required doubles. The positive difference uses both.
A non-negative double. It never returns a negative number.
Include <math.h>. On GCC, link with -lm.
No. fabs(x - y) is always the absolute gap. fdim returns 0 when x is not greater than y.

Next: floor()

Round a floating-point value toward negative infinity.

floor() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful