Namespaces
Variants
Views
Actions

fmod

From cppreference.com
< c‎ | numeric‎ | math
 
 
 
Common mathematical functions
Functions
Basic operations
(C99)
(C99)
(C99)
(C99)
(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       fmodf( float x, float y );
(since C99)
double      fmod( double x, double y );
long double fmodl( long double x, long double y );
(since C99)

Computes the remainder of the division operation x/y.

Specifically, the returned value is x - n*y, where n is x/y with its fractional part truncated.

The returned value will have the same sign as x.

Contents

[edit] Parameters

x, y - floating point values

[edit] Return value

Remainder of dividing arguments. The returned value will have the same sign as x.

[edit] Example

fmod <-- 5.1 - (5.1/3.0) * 3.0
         5.1 - (1.7)     * 3.0
         5.1 - (1.0)     * 3.0  (fractional part truncated)
         5.1 - 3.0
         2.1

#include <stdio.h>
#include <math.h>
#include <errno.h>
#include <fenv.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("fmod(+5.1,+3.0) = %+.1f\n", fmod(+5.1,+3.0));
    printf("fmod(-5.1,+3.0) = %+.1f\n", fmod(-5.1,+3.0));
    printf("fmod(+5.1,-3.0) = %+.1f\n", fmod(+5.1,-3.0));
    printf("fmod(-5.1,-3.0) = %+.1f\n", fmod(-5.1,-3.0));
    printf("\n");
 
    printf("fmod(+0.0,+1.0)      = %+.1f\n", fmod(+0.0,+1.0));      /* y != 0         */
    printf("fmod(-0.0,+1.0)      = %+.1f\n", fmod(-0.0,+1.0));
    printf("fmod(+1.0,+INFINITY) = %+.1f\n", fmod(+1.0,+INFINITY)); /* x not infinite */
    printf("fmod(+1.0,-INFINITY) = %+.1f\n", fmod(+1.0,-INFINITY));
    printf("fmod(+1.0,NAN)       = %f\n",    fmod(+1.0,NAN));
    printf("\n");
 
    /* domain error                              */
    /* "invalid" floating-point exception        */
    errno = 0; feclearexcept(FE_ALL_EXCEPT);
    printf("fmod(INFINITY,1.0)   = %f\n", fmod(INFINITY,1.0));   /* x is infinite */
    printf("%s\n",strerror(errno));
    show_fe_exceptions();
    printf("\n");
 
    errno = 0; feclearexcept(FE_ALL_EXCEPT);
    printf("fmod(1.0,0.0)        = %f\n", fmod(1.0,0.0));        /* y == 0 */
    printf("%s\n",strerror(errno));
    show_fe_exceptions();
 
    return 0;
}

Possible output:

fmod(+5.1,+3.0) = +2.1
fmod(-5.1,+3.0) = -2.1
fmod(+5.1,-3.0) = +2.1
fmod(-5.1,-3.0) = -2.1
 
fmod(+0.0,+1.0)      = +0.0
fmod(-0.0,+1.0)      = -0.0
fmod(+1.0,+INFINITY) = +1.0
fmod(+1.0,-INFINITY) = +1.0
fmod(+1.0,NAN)       = nan
 
fmod(INFINITY,1.0)   = -nan
Numerical argument out of domain
current exceptions raised:  FE_INVALID
 
fmod(1.0,0.0)        = -nan
Numerical argument out of domain
current exceptions raised:  FE_INVALID

[edit] See also

the quotient and remainder of integer division
(function) [edit]
signed remainder of the division operation
(function) [edit]