C Language Double precision floating-point remainder: fmod()


Example

This function returns the floating-point remainder of the division of x/y. The returned value has the same sign as x.

#include <math.h> /* for fmod() */
#include <stdio.h> /* for printf() */

int main(void)
{
    double x = 10.0;
    double y = 5.1;

    double modulus = fmod(x, y);

    printf("%lf\n", modulus); /* f is the same as lf. */

    return 0;
}

Output:

4.90000

Important: Use this function with care, as it can return unexpected values due to the operation of floating point values.

#include <math.h>
#include <stdio.h>

int main(void)
{
    printf("%f\n", fmod(1, 0.1));
    printf("%19.17f\n", fmod(1, 0.1));
    return 0;
}

Output:

0.1
0.09999999999999995