fmod() Function in C++

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

The fmod function in C++ is a function of the cmath header file used to find the remainder when two numbers are divided. These two numbers can be of the same or different data types (float, int, double).

Syntax of fmod() Function in C++

The syntax of fmod() c++ function is as follows:

Suppose there is numerator a and denominator b then:

The prototype of the fmod function in C++ is:

Parameters of fmod() Function in C++

Parameters of the fmod c++ function are:

  • a: it is numerator of type double
  • b: it is the denominator of type double

Return value of fmod() Function in C++

Return Type: double

The fmod C++ function returns a floating-point remainder, a double type value. If the value of the denominator is assigned as zero, then the function returns a value NAN, which means (Not A Number).

Example

Let us see an example of the fmod C++ function where we will use the fmod() function to return the floating-point remainder.

Output:

What is the fmod() Function in C++?

In daily life, if we want to do a division or any other mathematical calculations, we do it on paper or use a calculator. But, in a programming language, we do calculations using some built-in functions that perform the calculations.

The fmod c++ function is a library function used to find the remainder value when the numerator and denominator are used as a parameter for the division method.

Suppose we have a and b as numerator and denominator, respectively, which goes under the division method; then we will get a decimal remainder rounded towards zero using the fmod() function in C++.

This fmod c++ function is used to return this remainder value. The formula of the fmod function is as follows:

Where t is referred to as the truncated value for the given parameters- numerator and denominator.

Example: Let numerator be 16, and denominator be 3. 16 goes three times in 3, and 1 remains. So, the return value of using the fmod(16, 3) function will be 1.

fmod() function for arguments of different types.

The fmod c++ function can also be used for different arguments in a program. Suppose we take two variables for doing addition in the c++ program. Here, the two variables (arguments) can be of two data types: integer double or float. Refer to More Examples for more detail.

More Examples

Let's look at more examples to understand the fmod C++ function better.

Example 1. fmod() function for arguments of different types

Let us see an example where the arguments are of different types.

Output:

Example 2. fmod c++ function for user input.

Output:

Example 3. In this Example, let us see what happens when the value of the denominator is zero.

Output:

In the above example of the fmod c++ function, we can see that if the value of the denominator is zero, then the output we get is NAN. Its stands for Not a Number.

Conclusion

  • fmod function in c++ is used to calculate the remainder of two values: the numerator and the denominator.

  • fmod function in c++ is found in the header file named cmath, so we must include this header file at the beginning of the code before using the function.

  • If the value of the denominator is zero, then the result will be NAN in the fmod C++ function.