In this blog post, I will teach you what is C fmod() and how to use in your C program. So, let’s first understand what fmod() is in C.
The fmod functions compute the floating-point remainder of x/y. In other words, you can say that the fmod function computes the floating-point remainder of the division operation x/y
. It returns the remainder of the division of x by y as a double-precision floating-point number.
The floating-point remainder of the division operation x/y calculated by this function is exactly the value x - n*y
, where x is the dividend and y is the divisor and n is x/y with its fractional part truncated (i.e., the integer part of x/y).
Example,
Dividend x = 10.5 Divisor y = 3.0 The remainder is 1.5
Syntax of C fmod():
It is declared in math.h
and takes two arguments in the form of double and returns the value of type double.
//syntax of fmod() double fmod(double x, double y);
Parameters:
x
 => floating types (double)
y
 => floating types (double)
Return value:
If y is nonzero, fmod functions returns the floating-point remainder of the division x/y. But if y is zero, whether a domain error occurs or the fmod functions return zero is implementation-defined.
C program to understand the working of fmod():
In mentioned C program, I am computing the remainder of x/y, where x and y are floating type (double).
/** C Program to Calculate Floating-Point Remainder using fmod() */ #include <math.h> #include <stdio.h> int main() { double x, y, z; x = 10.0; // Dividend y = 3.5; // Divisor z = fmod(x,y); /* z = 3.0 */ printf("fmod( %lf, %lf) = %lf\n", x, y, z); return 0; }
Output:
/* fmod( 10.000000, 3.500000) = 3.000000 */
Now it’s time to understand a few key points related to the fmod() but before explaining it I want to clear a very basic question which might come to newbies mind that “What is the requirement of fmod() if % (modulo operator) already exist in C”.
Don’t worry answer to this question is very simple.
The fmod() function and the % (modulo operator) in C serve different purposes. The % (modulo operator) in C only works for integer operands and returns an integer remainder of the modulo division.
/* This assigns the value 1 to 'rem' because 9 divided by 4 is 2 with a remainder of 1 */ int rem = 9 % 4;
On the other hand, the fmod() function works on floating points. It accepts two floating-point values (double) as arguments to compute the remainder and returns the remainder of the division as a floating point.
/* This computes the remainder of dividing 10.5 by 3.2 */ double result = fmod(10.5, 3.2);
Few important points related to fmod() in C:
Let’s see some important points related to the fmod() that you should know before using the fmod().
Non-zero divisor:
1.
In the expression fmod(x,y)
, if y (divisor) is nonzero, the result has the same sign as x. This means that the sign of the returned value is same as x (divided).
Here are some examples to explain the above statement:
Case-1: Dividend (x) is +ve:
/** Case-1: Dividend is +ve Dividend x = 10.0 Divisor y = 3.5 The remainder is 3.0 **/ #include <math.h> #include <stdio.h> int main() { double x, y, z; x = 10.0; y = 3.5; z = fmod(x,y); /* z = 3.0 */ printf("fmod( %lf, %lf) = %lf\n", x, y, z); return 0; }
Output:
fmod( 10.000000, 3.500000) = 3.000000
Case-2: Dividend (x) is -ve:
/** Case-1: Dividend is -ve Dividend x = -10.0 Divisor y = 3.5 The remainder is -3. **/ #include <math.h> #include <stdio.h> int main() { double x, y, z; x = -10.0; y = 3.5; z = fmod(x,y); /* z = -3.0 */ printf("fmod( %lf, %lf) = %lf\n", x, y, z); return 0; }
Output:
fmod( -10.000000, 3.500000) = -3.000000
Case-3: Dividend (x) is +ve and Divisor is -ve:
/** Case-2: Dividend is +ve and Divisior is -ve Dividend x = 10.0 Divisor y = -3.5 The remainder is 3. **/ #include <math.h> #include <stdio.h> int main() { double x, y, z; x = 10.0; y = -3.5; z = fmod(x,y); /* z = 3.0 */ printf("fmod( %lf, %lf) = %lf\n", x, y, z); return 0; }
Output:
fmod( 10.000000, -3.500000) = 3.000000
Case-4: Dividend (x) is -ve and Divisor is also -ve:
/** Case-2: Dividend is -ve and Divisior is -ve Dividend x = -10.0 Divisor y = -3.5 The remainder is -3. **/ #include <math.h> #include <stdio.h> int main() { double x, y, z; x = -10.0; y = -3.5; z = fmod(x,y); /* z = -3.0 */ printf("fmod( %lf, %lf) = %lf\n", x, y, z); return 0; }
Output:
fmod( -10.000000, -3.500000) = -3.000000
2.
In the expression fmod(x,y)
, if y (divisor) is nonzero, the result magnitude less than the magnitude of y. This means that the result will lie within the interval (-|y|, |y|) and will not exceed the magnitude of the divisor y.
3.
In the expression fmod(x,y)
, If y is zero, whether a domain error occurs or the fmod functions return zero is implementation-defined.
Error handling
Errors are reported as specified in math_errhandling.
- Domain error may occur ifÂ
y
 is zero. - If the implementation supports IEEE floating-point arithmetic (IEC 60559),
- IfÂ
x
 is ±0 andÂy
 is not zero, ±0 is returned - IfÂ
x
 is ±∞ andÂy
 is not NaN, NaN is returned and FE_INVALID is raised - IfÂ
y
 is ±0 andÂx
 is not NaN, NaN is returned and FE_INVALID is raised. - IfÂ
y
 is ±∞ andÂx
 is finite,Âx
 is returned. - If either argument is NaN, NaN is returned.
- IfÂ
Few MCQ on C fmod():
Here are few multiple-choice questions (MCQs) on the fmod() function in C, along with their answers. These MCQs will help you in quick revision of your concept related to fmod().
Recommended Post:
- C Programming Courses And Tutorials.
- CPP Programming Courses And Tutorials.
- ceil function use in C programming.
- floor function in C programming.
- Use of pow function in C language.
- C program to calculate the power of a number.
- sqrt function in C.
- C program to find all roots of a quadratic equation using switch case.
- C program to find the roots of a quadratic equation.
- How to find whether a given number is prime number in C?
- Use of isxdigit in C programming.
- How to use ispunct function in C programming?
- tolower function in C.
- How to use the islower function in C?
- Use of iscntrl function in C.