Synopsis

#include <math.h>

double fmod(double numer, double denom);
float fmodf(float numer, float denom);
long double fmodl(long double numer, long double denom);

Description

These functions return the floating-point remainder of numer/denom (rounded towards zero).

Arguments:
numer - the dividend.
denom - the divisor.

An application wishing to check for error situations should set errno to zero and call feclearexcept(FE_ALL_EXCEPT) before calling these functions. On return, if errno is non-zero or fetestexcept(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW) is non-zero, an error has occurred.

Return value

These functions return the value numer- i* denom, for some integer i such that, if denom is non-zero, the result has the same sign as numer and magnitude less than the magnitude of denom.

If the correct value would cause underflow, and is not representable, a range error occurs, and fmod(), fmodf(), and fmodl() return 0.0.

If numer or denom is NaN, a NaN is returned, and none of the conditions below is considered.

If denom is zero, a domain error occurs, and a NaN is returned.

If numer is infinite, a domain error occurs, and a NaN is returned.

If numer is ±0 and denom is not zero, ±0 is returned.

If numer is not infinite and denom is ±Inf, numer is returned.

If the correct value would cause underflow, and is representable, a range error occurs and the correct value is returned.

Errors

[EDOM] The numer argument is infinite or denom is zero.

If the integer expression (math_errhandling & MATH_ERRNO) is non-zero, then errno is set to [EDOM]. If the integer expression (math_errhandling & MATH_ERREXCEPT) is non-zero, then the invalid floating-point exception is raised.

[ERANGE] The result underflows.

If the integer expression (math_errhandling & MATH_ERRNO) is non-zero, then errno is set to [ERANGE]. If the integer expression (math_errhandling & MATH_ERREXCEPT) is non-zero, then the underflow floating-point exception is raised.

Implementation tasks

  • Implement error detection for the fmod() function.
  • Implement the fmodf() function.
  • Implement the fmodl() function.