![]() |
![]() |
![]() |
Compute a floating-point modulus
#include <math.h> double fmod( double x, double y ); float fmodf( float x, float y );
libm
The fmod() and fmodf() functions compute the floating-point remainder of x / y, even if the quotient x / y isn't representable.
x - (i * y)
For an integer i such that:
If y is: | fmod() returns: |
---|---|
0 | 0 |
=/=0 | The same sign as x, and a magnitude less than the magnitude of y. |
#include <stdio.h> #include <math.h> #include <stdlib.h> int main( void ) { printf( "%f\n", fmod( 4.5, 2.0 ) ); printf( "%f\n", fmod( -4.5, 2.0 ) ); printf( "%f\n", fmod( 4.5, -2.0 ) ); printf( "%f\n", fmod( -4.5, -2.0 ) ); return EXIT_SUCCESS; }
produces the output:
0.500000 -0.500000 0.500000 -0.500000
Safety: | |
---|---|
Cancellation point | No |
Interrupt handler | No |
Signal handler | No |
Thread | Yes |
ceil(), div(), fabs(), floor()
![]() |
![]() |
![]() |