fmod() Function
The fmod()
function computes the floating-point remainder of a division, where the result is the difference between the dividend and the product of the divisor with the truncated quotient. This function is useful when you need to determine the leftover part after division for floating-point numbers.
Syntax of fmod()
double fmod(double numer, double denom);
float fmodf(float numer, float denom);
long double fmodl(long double numer, long double denom);
Parameters
Parameter | Description |
---|---|
numer | Dividend value for which the remainder is computed. |
denom | Divisor value used in the division. |
It is important to note that the function returns the remainder with the same sign as the dividend. Also, if the divisor is zero, the behavior depends on the library implementation, possibly resulting in a domain error or returning zero.
Examples for fmod()
Example 1: Basic Usage of fmod() to Compute the Remainder
This example demonstrates how to use fmod()
to compute the remainder of a division between two positive floating-point numbers.
Program
#include <stdio.h>
#include <math.h>
int main() {
double dividend = 10.5;
double divisor = 3.0;
double result = fmod(dividend, divisor);
printf("Remainder of %.2f / %.2f is %.2f\n", dividend, divisor, result);
return 0;
}
Explanation:
- The program initializes a dividend of 10.5 and a divisor of 3.0.
fmod()
computes the remainder using the formula: remainder = dividend – (truncated quotient * divisor).- The calculated remainder is stored in
result
. - The remainder is then printed using
printf()
.
Program Output:
Remainder of 10.50 / 3.00 is 1.50
Example 2: fmod() with Negative Dividend
This example illustrates the behavior of fmod()
when the dividend is negative, showing that the remainder carries the sign of the dividend.
Program
#include <stdio.h>
#include <math.h>
int main() {
double dividend = -10.5;
double divisor = 3.0;
double result = fmod(dividend, divisor);
printf("Remainder of %.2f / %.2f is %.2f\n", dividend, divisor, result);
return 0;
}
Explanation:
- The program sets a negative dividend (-10.5) while keeping the divisor positive (3.0).
fmod()
calculates the remainder, ensuring the sign of the result matches the dividend.- The resulting remainder is stored in
result
. - The output is then printed, demonstrating that the remainder is negative.
Program Output:
Remainder of -10.50 / 3.00 is -1.50
Example 3: Using fmodf() for Single-Precision Floating-Point Numbers
This example demonstrates how to use the single-precision variant fmodf()
to compute the remainder with float values.
Program
#include <stdio.h>
#include <math.h>
int main() {
float dividend = 7.25f;
float divisor = 2.0f;
float result = fmodf(dividend, divisor);
printf("Remainder of %.2f / %.2f is %.2f\n", dividend, divisor, result);
return 0;
}
Explanation:
- The program uses float variables for the dividend (7.25f) and divisor (2.0f).
- The function
fmodf()
computes the remainder for single-precision floats. - The result is stored in the variable
result
. - The remainder is printed using
printf()
, demonstrating the function’s operation with float values.
Program Output:
Remainder of 7.25 / 2.00 is 1.25