How to use fmod() function to get the floating-point remainder (modulo) of the division x/y in C

3 Answers

0 votes
#include <stdio.h>     
#include <math.h>
 
int main(int argc, char **argv)
{	
	printf("fmod(4.3, 2.0) = %.1f\n", fmod (4.3, 2.0));
	printf("fmod(18.5, 4.2) = %.1f\n", fmod(18.5, 4.3));
	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("fmod(0.0, 1.0) = %.1f\n", fmod(0, 1.0));
    printf("fmod(-0.0, 1.0) = %.1f\n", fmod(-0.0, 1.0));
    printf("fmod(3.1, INFINITY) = %.1f\n", fmod(3.1, INFINITY));
	
    return 0;
}

/*
run:
  
fmod(4.3, 2.0) = 0.3
fmod(18.5, 4.2) = 1.3
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(3.1, INFINITY) = 3.1

*/

 



answered Mar 18, 2016 by avibootz
edited Mar 18, 2016 by avibootz
0 votes
#include <stdio.h>
#include <math.h>

int main(void)
{
    double x = 13.0;
    double y = 4.7;

    double modulus = fmod(x, y);

    printf("%lf\n", modulus); 
    
    return 0;
}

   
/*
run:
 
3.600000

*/

 



answered Sep 1, 2017 by avibootz
0 votes
#include <stdio.h>
#include <math.h>

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

   
/*
run:
 
0.100000
0.0999999999999999500

*/

 



answered Sep 1, 2017 by avibootz
...