Namespaces
Variants
Views
Actions

remainder

From cppreference.com
< c‎ | numeric‎ | math
 
 
 
Common mathematical functions
Functions
Basic operations
remainder
(C99)
(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       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)

Computes the signed remainder of the floating point division operation x/y.

Specifically, the returned value is x - n*y, where n is x/y rounded to the nearest integer, or the nearest even integer if x/y is halfway between two integers.

In contrast to fmod(), the returned value is not guaranteed to have the same sign as x.

If the returned value is 0, it will have the same sign as x.

Contents

[edit] Parameters

x, y - floating point values

[edit] Return value

Remainder of dividing arguments

[edit] Example

remainder <-- 5.1 - (5.1/3.0) * 3.0
              5.1 - (1.7)     * 3.0
              5.1 - (2.0)     * 3.0  (rounded to nearest integer)
              5.1 - 6.0
              -0.9

#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("remainder(+5.1,+3.0) = %+.1f\n", remainder(+5.1,+3.0));
    printf("remainder(-5.1,+3.0) = %+.1f\n", remainder(-5.1,+3.0));
    printf("remainder(+5.1,-3.0) = %+.1f\n", remainder(+5.1,-3.0));
    printf("remainder(-5.1,-3.0) = %+.1f\n", remainder(-5.1,-3.0));
    printf("\n");
 
    /* When remainder is zero, sign of remainder is sign of x */
    printf("remainder(+9.0,+3.0) = %+.1f\n", remainder(+9.0,+3.0));
    printf("remainder(-9.0,+3.0) = %+.1f\n", remainder(-9.0,+3.0));
    printf("\n");
 
    /* domain error                              */
    /* "invalid" floating-point exception        */
    errno = 0; feclearexcept(FE_ALL_EXCEPT);
    printf("remainder(1.0,0.0)   = %f\n", remainder(1.0,0.0));   /* y == 0 */
    printf("%s\n",strerror(errno));
    show_fe_exceptions();
 
    return 0;
}

Output:

remainder(+5.1,+3.0) = -0.9
remainder(-5.1,+3.0) = +0.9
remainder(+5.1,-3.0) = -0.9
remainder(-5.1,-3.0) = +0.9
 
remainder(+9.0,+3.0) = +0.0
remainder(-9.0,+3.0) = -0.0
 
remainder(1.0,0.0)   = -nan
Numerical argument out of domain
current exceptions raised:  FE_INVALID

[edit] See also

remainder of the floating point division operation
(function) [edit]
the quotient and remainder of integer division
(function) [edit]
C++ documentation for remainder