fmod, fmodf, fmodl

Header: <math.h>

1-3) Computes the floating-point remainder of the division operation x / y.

# Declarations

float fmodf( float x, float y );

(since C99)

double fmod( double x, double y );
long double fmodl( long double x, long double y );

(since C99)

#define fmod( x, y )

(since C99)

# Parameters

# Return value

If successful, returns the floating-point remainder of the division x / y as defined above.

# Notes

POSIX requires that a domain error occurs if x is infinite or y is zero.

fmod, but not remainder is useful for doing silent wrapping of floating-point types to unsigned integer types: (0.0 <= (y = fmod(rint(x), 65536.0 )) ? y : 65536.0 + y) is in the range [-0.0,65535.0], which corresponds to unsigned short, but remainder(rint(x), 65536.0) is in the range [-32767.0,+32768.0], which is outside of the range of signed short.

The double version of fmod behaves as if implemented as follows:

# Example

#include <fenv.h>
#include <math.h>
#include <stdio.h>
 
// #pragma STDC FENV_ACCESS ON
 
int main(void)
{
    printf("fmod(+5.1, +3.0) = %.1f\n", fmod(5.1, 3));
    printf("fmod(-5.1, +3.0) = %.1f\n", fmod(-5.1, 3));
    printf("fmod(+5.1, -3.0) = %.1f\n", fmod(5.1, -3));
    printf("fmod(-5.1, -3.0) = %.1f\n", fmod(-5.1, -3));
 
    // special values
    printf("fmod(+0.0, 1.0) = %.1f\n", fmod(0, 1));
    printf("fmod(-0.0, 1.0) = %.1f\n", fmod(-0.0, 1));
    printf("fmod(+5.1, Inf) = %.1f\n", fmod(5.1, INFINITY));
 
    // error handling
    feclearexcept(FE_ALL_EXCEPT);
    printf("fmod(+5.1, 0) = %.1f\n", fmod(5.1, 0));
    if (fetestexcept(FE_INVALID))
        puts("    FE_INVALID raised");
}

# See also