C fmod(): Calculate Floating-Point Remainder

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.

 

 

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().

0 votes, 0 avg

You have 15 minutes to take the MCQ On C fmod()

Your time has been Over.


MCQ On C fmod()

1 / 17

What will be the output of fmod(-10.0, 3.0)?

2 / 17

When using fmod(x, y), what happens if y is zero?

3 / 17

Which mathematical function is similar to the fmod function in C?

4 / 17

Which arithmetic operation does fmod() perform?

5 / 17

What is the syntax for using fmod() to find the remainder of x divided by y?

6 / 17

Which header file is required to use the fmod() function in C?

7 / 17

What happens if the second argument in fmod() is zero?

8 / 17

The fmod() function is used for

9 / 17

What will be the output of fmod(-10.0, -3.0)?

10 / 17

What will be the output of fmod(10.0, -3.0)?

11 / 17

What does the fmod() function in C do?

12 / 17

What is the return type of the fmod() function?

13 / 17

Which statement is true?

14 / 17

Which of the following is the correct usage of fmod() to find the remainder of 10.5 divided by 3.2?

15 / 17

Which of the following statements is true about fmod()?

16 / 17

Which of the following is the correct prototype of the fmod function in C?

17 / 17

If fmod(5.7, 1.2) is called, what will be the result?

Your score is

The average score is 0%

0%

 

Recommended Post:

 

Leave a Reply

Your email address will not be published. Required fields are marked *