fmod(), fmodf(), fmodl() — Calculate floating-point remainder

Standards

Standards / Extensions C or C++ Dependencies
ISO C
POSIX.1
XPG4
XPG4.2
ISO/ANSI C++
C99
Single UNIX Specification, Version 3
C++ TR1 C99
both  

Format

#include <math.h>

double fmod(double x, double y);
float fmod(float x, float y);                    /* C++ only */
long double fmod(long double x, long double y);  /* C++ only */
float fmodf(float x, float y);
long double fmodl(long double x, long double y);

General description

Calculates the floating-point remainder of x/y. The absolute value of the result is always less than the absolute value of y. The result will have the same sign as x.
Note: These functions work in both IEEE Binary Floating-Point and hexadecimal floating-point formats. See IEEE binary floating-point for more information about IEEE Binary Floating-Point.
Restriction: The fmodf() function does not support the _FP_MODE_VARIABLE feature test macro.

Returned value

If y is 0, or the result would overflow, then the function returns 0. Errno remains unchanged.

Special behavior for IEEE

If successful, the function returns the floating-point remainder of x/y.

If y is 0, the function sets errno to EDOM and returns NaNQ. No other errors will occur.

Example

CELEBF25
/* CELEBF25                                      

   This example computes z as the remainder of x/y; here x/y is -3 with a       
   remainder of -1.                                                             

 */                                                                             
#include <math.h>                                                               
#include <stdio.h>                                                              
                                                                                
int main(void)                                                                  
{                                                                               
   double x, y, z;                                                              
                                                                                
   x = -10.0;                                                                   
   y = 3.0;                                                                     
   z = fmod(x,y);      /* z = -1.0 */                                           
                                                                                
   printf("fmod( %f, %f) = %lf\n", x, y, z);                                    
}                                                                               
Output
fmod( -10.000000, 3.000000) = -1.000000

Related information