Fmod( ) Function C++

In general, to calculate a remainder from two values, we simply divide them manually or often use a calculator to make it. But if we talk about the calculations in the programming languages, some built-in functions are responsible for calculating remainders, fractional values. C++ programming language also contains default functions regarding time, dates, and other mathematical operations. Fmod() function is one of the mathematical operations performed by having two values of the same or different data types. This tutorial will cover the description of the fmod() function and examples of implementation.


This function gives the remainder when two variables x and y, the numerator and denominator undergo a division method. In other words, when x is divided by y, a floating-point, the decimal remainder is obtained. This function is also known as a library function as it uses a cmath header file in the source code. A floating-point remainder of the numerator/ denominator that is rounded towards zero, is obtained.

fmod (x, y) = x - quote * y

The quote is a truncated value (rounded towards zero ) that is a result of x/y.


Double result = fmod (variable x, variable y),

Here, x and y are two variables for different data types that are passed as a parameter from the function.

Fmod() Prototype

dble fmod (dble x, dble y);

flt fmod (flt x, flt y);

lng dble fmod (lng dble x, lng dble y);

The function takes two arguments like in double, float, long double, integer, or mixed data types. Similarly the resultant is returned by a value of three data types.

Implementation of fmod() Function

To implement the fmod() in the Ubuntu Linux system, we need a text editor tool for the source code and an Ubuntu terminal for the execution of the resultant value. For this, the user must have privileges to access the applications easily.

Example 1

The floating-point remainder of the numerator and denominator will be calculated in this example. To execute the program of C++, we need two basic header files to be included in the code. One of these two is ‘iostream’. This library allows us the use of input and output features. For instance, to display any statement, you need cout, that is enabled because of this library. Secondly, we need ‘cmath’. As the name indicates, it is related to mathematics and responsible for providing all the operations in the program.

#include <iostream>

#include <cmath>

Inside the main program, we take two double variables. The approach of assigning values to the variables and applying operations on them is similar to the modf () function of C++. Both the variables contain positive double values in them. The resultant variable will also be of double data type, as both the input variables contain double values, so the result must be a double value.

Result = fmod (x, y);

This function will calculate the floating decimal notation answer. The result of both the values is displayed by using a slash ‘/’.

The second statement also performs the same function, but one variable contains a positive double value, and the second variable contains a negative value. The numerator is a negative value. Now, save the code in the file of the text editor and then go to the Ubuntu terminal for the execution of the code. In the Linux operating system, we need a compiler for the execution of the C++ code. This compiler is G++. It needs an input file having the code, an output file, and ‘-o’ to save the respective output.

$ g++ -o fmod fmod.c

This command will compile the code and now use the execution command to see the results.

$ ./fmod

On execution, you will see that in the first example, a simple double value is obtained. Whereas in the second case, while using a negative numerator, this will give a negative double value. We will further perform some experiments on the input variables later in the example.

Example 2

As in the previous example code, we took one positive and a negative value of the same data type that was double. But in this example, one variable will be of integer data type, whereas the second will be of double data type. The integer data type variable will contain a negative value. This will be a denominator. The resultant variable will be of double data type, as the answer of integer and double values is in the double value.

So this will result in the positive double value. In the second case, the value of x, the numerator value, is the same as we have taken in the first fmod() in the example. But the value of y, the denominator, is taken as ‘0’.

Now, execute this code by using the same compiler and see what are the results shown in the terminal.

For the first line, the resultant will be a positive value. But for the second case, when we have used a zero value in the denominator, the answer will be ‘nan’ (not a number). It might be a garbage value. In general, as we know that anything divided by ‘0’ gives a 0 value, so the same thing is applied here.

Example 3

Input will be taken externally in this example. When the program is executed, the compiler displays the message to the user so that he can enter the data. But both values of numerator and denominator must be in double data type. The fmod() function will be applied on both the entered values and the resultant will be stored in the double remainder variable.

Now the user is asked to provide data. Both values the user has entered are positive, so the answer will be a positive float value. We have applied different conditions of input numbers to see the resultant value. For the first time, the user has entered a bigger numerator value than the denominator. The value of the fmod() function lies from 0 to 1.

And if the user provides the input value both in positive values, a smaller value of numerator, and a greater denominator then the value is greater as compared to having a smaller denominator value.

On the other hand, if a numerator has a negative value, the resultant as a whole will be a negative value. But if we applied a negative value for the denominator, it does not affect the results, it will be positive.

This means that the positive/ negative resultant value of fmod() depends upon the value of the numerator.


The floating-point remainder of two values, numerator or denominator, is obtained through a fmod() function. This function not only returns a positive value but also a negative value is obtained depending on the value of the numerator. An uncertain no number value is obtained by having a denominator ‘0’. Hence, both the variables that are passed as a parameter must be a valid number of any data type. The return data type value is mostly in double or float.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.