remainder, remainderf, remainderl

Header: <math.h>

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

# Declarations

float remainderf( float x, float y );

(since C99)

double remainder( double x, double y );

(since C99)

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

(since C99)

#define remainder( x, y )

(since C99)

# Parameters

# Return value

If successful, returns the IEEE 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.

# Example

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

# See also