fdim, fdimf, fdiml

Header: <math.h>

1-3) Returns the positive difference between x and y, that is, if x>y, returns x-y, otherwise (if x≤y), returns +0.

# Declarations

float fdimf( float x, float y );

(since C99)

double fdim( double x, double y );

(since C99)

long double fdiml( long double x, long double y );

(since C99)

#define fdim( x, y )

(since C99)

# Parameters

# Return value

If successful, returns the positive difference between x and y.

# Notes

Equivalent to fmax(x-y, 0) except for the NaN handling requirements.

# Example

#include <errno.h>
#include <fenv.h>
#include <math.h>
#include <stdio.h>
// #pragma STDC FENV_ACCESS ON
 
int main(void)
{
    printf("fdim(4, 1) = %f, fdim(1, 4)=%f\n", fdim(4,1), fdim(1,4));
    printf("fdim(4,-1) = %f, fdim(1,-4)=%f\n", fdim(4,-1), fdim(1,-4));
    //error handling
    errno = 0; feclearexcept(FE_ALL_EXCEPT);
    printf("fdim(1e308, -1e308) = %f\n", fdim(1e308, -1e308));
    if (errno == ERANGE)
        perror("    errno == ERANGE");
    if (fetestexcept(FE_OVERFLOW))
        puts("    FE_OVERFLOW raised");
}

# See also