C++ std::fdim() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<cmath>

Definition

In C++, std::fdim(x, y) returns the positive difference of two numbers. That is the same as max(x - y, 0). When x is bigger than y you get the gap, and when it is not you get 0, never a negative number.

This is useful whenever a negative result makes no sense, such as overtime hours, remaining budget, or the amount of a number that goes over a limit. The name means "floating-point positive difference".

std::fdim() is declared in <cmath> and is part of the group Remainder, scale, and compare. These functions split a number, scale it by a power of two, find its remainder, or pick between two numbers. They work directly on the binary form of a double.

Syntax

Include <cmath> and call std::fdim. Start with these lines.

C++
double a = std::fdim(5.0, 3.0);    // 2
double b = std::fdim(3.0, 5.0);    // 0 (not -2)
double c = std::fdim(2.5, 1.0);    // 1.5
CallWhat it does
std::fdim(5.0, 3.0)5 is bigger, so the result is 5 - 3 = 2.
std::fdim(3.0, 5.0)3 is not bigger than 5, so the result is 0, not -2.
std::fdim(2.5f, 1.0f)The float overload. Returns 1.5f.

The order matters. fdim(x, y) and fdim(y, x) are not the same, and at most one of them is above zero.

Library declarations
C++
float       fdim(float x, float y);
double      fdim(double x, double y);
long double fdim(long double x, long double y);
// plus overloads for mixed and integer arguments

Parameters

ParameterDescription
xRequired. The number to subtract from. It can be float, double, long double, or an integer type (converted to double).
yRequired. The number to subtract.

Return Value

Return ValueDescription
x - yThe difference, when x is greater than y.
0Returned when x is less than or equal to y.
NaNReturned if either argument is NaN.

C++ Examples

Example 1 — Positive Differences

Print fdim() for pairs where x is bigger, smaller, equal, and negative.

C++
#include <cmath>
#include <iostream>

int main() {
    double pairs[][2] = {{5, 3}, {3, 5}, {2.5, 1}, {-1, -4}, {4, 4}};

    for (auto& p : pairs) {
        std::cout << "fdim(" << p[0] << ", " << p[1] << ") = "
                  << std::fdim(p[0], p[1]) << '\n';
    }
    return 0;
}

How It Works

  1. The loop reads each pair and calls fdim(p[0], p[1]).
  2. fdim(5, 3) is 2, and fdim(2.5, 1) is 1.5. Both have a bigger x, so the gap is returned.
  3. fdim(3, 5) and fdim(4, 4) give 0. Even with negative numbers the rule holds: -1 is bigger than -4, so fdim(-1, -4) is 3.

Example 2 — Overtime Hours

Overtime is the time worked above 40 hours. For a person who worked less, it must be 0, not negative.

C++
#include <cmath>
#include <iostream>

int main() {
    double standard = 40.0;

    for (double worked : {38.0, 40.0, 45.5}) {
        double overtime = std::fdim(worked, standard);
        std::cout << worked << " hours -> " << overtime << " overtime\n";
    }
    return 0;
}

How It Works

  1. fdim(worked, standard) compares the hours worked with the standard 40 hours.
  2. 38 hours is below 40, so the answer is 0. Exactly 40 hours also gives 0.
  3. 45.5 hours is above the limit, so the overtime is 5.5 hours. No if statement was needed.

Example 3 — Remaining Budget That Never Goes Negative

Compare plain subtraction with fdim() when spending goes over the budget.

C++
#include <cmath>
#include <iostream>

int main() {
    double budget = 100.0;

    for (double spent : {30.5, 120.0}) {
        std::cout << "Spent " << spent << ": plain " << budget - spent
                  << ", fdim " << std::fdim(budget, spent) << '\n';
    }
    return 0;
}

How It Works

  1. Both columns start with the same budget of 100, and show two ways to find what is left.
  2. When 30.5 is spent, both ways agree: 69.5 is left.
  3. When 120 is spent, plain subtraction gives -20, which looks like a negative budget. fdim() says 0, which means nothing is left.

Frequently Asked Questions

std::fdim(x, y) returns the positive difference of x and y. If x is larger than y it returns x - y, and otherwise it returns 0.
x - y when x > y, otherwise +0.0. If either argument is NaN the result is NaN. The type is the promoted type of the arguments.
No. fdim(x, y) is x - y only when x is bigger, and 0 otherwise. The absolute difference is std::fabs(x - y), which is positive in both orders.
It is std::fmax(x - y, 0.0), the bigger of the difference and zero. It is a short way to avoid a negative answer.
Include <cmath> and write std::fdim(). That always works and picks the C++ overloads. The global name ::fdim often works too, but the standard does not promise it, and it may only take a double.

Next: floor()

Keep going through the <cmath> functions. Round a number down to the next whole number.

floor() tutorial →

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.

8 people found this page helpful