Namespaces
Variants
Views
Actions

fdim

From cppreference.com
< c‎ | numeric‎ | math
 
 
 
Common mathematical functions
Functions
Basic operations
(C99)
(C99)
(C99)
(C99)
fdim
(C99)
(C99)(C99)(C99)
Exponential functions
(C99)
(C99)
(C99)
(C99)
Power functions
(C99)
(C99)
Trigonometric and hyperbolic functions
(C99)
(C99)
(C99)
Error and gamma functions
(C99)
(C99)
(C99)
(C99)
Nearest integer floating point operations
(C99)(C99)(C99)
(C99)
(C99)(C99)(C99)
Floating point manipulation functions
(C99)(C99)
(C99)
(C99)
Classification
(C99)
(C99)
(C99)
Macro constants
 
Defined in header <math.h>
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)

Returns the positive difference between x and y. This could be implemented as fmax (x - y, 0), so if x ≤ y, the result is always equals to 0, otherwise it is x - y.

Contents

[edit] Parameters

x, y - floating point value

[edit] Return value

The positive difference value.

[edit] Example

#include <stdio.h>
#include <math.h>
#include <errno.h>
#include <fenv.h>
#include <float.h>
#include <string.h>
 
#pragma STDC FENV_ACCESS ON
 
void show_fe_exceptions(void)
{
    printf("current exceptions raised: ");
    if(fetestexcept(FE_DIVBYZERO))     printf(" FE_DIVBYZERO");
    if(fetestexcept(FE_INEXACT))       printf(" FE_INEXACT");
    if(fetestexcept(FE_INVALID))       printf(" FE_INVALID");
    if(fetestexcept(FE_OVERFLOW))      printf(" FE_OVERFLOW");
    if(fetestexcept(FE_UNDERFLOW))     printf(" FE_UNDERFLOW");
    if(fetestexcept(FE_ALL_EXCEPT)==0) printf(" none");
    printf("\n");
}
 
int main(void)
{
    printf ("fdim(4.0,1.0)      = %.1f\n", fdim(4.0,1.0));
    printf ("fdim(1.0,4.0)      = %.1f\n", fdim(1.0,4.0));
    printf ("fdim(INFINITY,1.0) = %.1f\n", fdim(INFINITY,1.0));
    printf ("fdim(1.0,INFINITY) = %.1f\n", fdim(1.0,INFINITY));
    printf ("fdim(NAN,1.0)      = %.1f\n", fdim(NAN,1.0));
    printf("\n");
 
    /* range error */
    errno = 0;
    printf ("fdim(INFINITY,1.0) = %f\n", fdim(INFINITY,1.0));
    printf("%s\n",strerror(errno));
    show_fe_exceptions();
    printf("\n");
 
    /* "inexact" floating-point exception */
    errno = 0; feclearexcept(FE_ALL_EXCEPT);
    printf ("fdim(DBL_MAX,DBL_MIN) = %g\n", fdim(DBL_MAX,DBL_MIN));
    printf("%s\n",strerror(errno));
    show_fe_exceptions();
 
    return 0;
}

Possible output:

fdim(4.0,1.0)      = 3.0
fdim(1.0,4.0)      = 0.0
fdim(INFINITY,1.0) = inf
fdim(1.0,INFINITY) = 0.0
fdim(NAN,1.0)      = nan
 
fdim(INFINITY,1.0) = inf
Numerical result out of range
current exceptions raised:  none
 
fdim(DBL_MAX,DBL_MIN) = 1.79769e+308
Success
current exceptions raised:  FE_INEXACT

[edit] See also

computes absolute value of an integral value (|x|)
(function) [edit]
(C99)
larger of two floating point values
(function) [edit]