Modulus of Two float numbers or double number
Introduction:
When working with mathematical operations in programming, it is often necessary to find the remainder of a division operation between two numbers. This operation is called modulus or modulo. The modulus of two integers is well-defined and can be easily computed using the modulo operator (%). However, when dealing with floating-point numbers, computing the modulus is not as straightforward.
In this article, we will explore the concept of modulus for floating-point numbers and present a few approaches to computing it in C++.
Modulus of Two Integers
Before diving into the modulus of floating-point numbers, let's review the modulus of two integers. Given two integers a and b, the modulus of a divided by b can be computed as follows:
a % b = a - (a / b) * b
For example, the modulus of 10 divided by 3 is:
10 % 3 = 10 - (10 / 3) * 3 = 10 - 3 * 3 = 1
Note that the modulus operator (%) returns the remainder of the division operation, which in this case is 1.
Modulus of Two Float or Double Numbers
Computing the modulus of two floating-point numbers is not as simple as the modulus of two integers. One approach to computing the modulus of two floats is to first convert them to integers by multiplying them by a power of 10, computing the modulus of the resulting integers, and then dividing the result by the same power of 10.
Here's an example implementation of this approach:
double fmod(double a, double b) {
int intA = static_cast<int>(a * 1000000);
int intB = static_cast<int>(b * 1000000);
int result = intA % intB;
return static_cast<double>(result) / 1000000;
}
In this implementation, we first multiply the input numbers by 1000000 (a power of 10), cast them to integers, and compute the modulus using the modulus operator (%). Finally, we convert the result back to a double by dividing it by 1000000.
Note that this approach is not perfect, as floating-point arithmetic can introduce rounding errors, leading to incorrect results in some cases. Also, this approach can be slow due to the need to convert the inputs to integers.
Using Fmod Function
C++ provides a built-in function for computing the modulus of two floating-point numbers, called fmod(). This function takes two double arguments and returns the floating-point remainder of the division operation.
Here's an example usage of fmod():
Example 1: Modulus of two float numbers
#include <iostream>
#include <cmath>
int main() {
float x = 10.5;
float y = 3.2;
float result = fmod(x, y);
std::cout << "The modulus of " << x << " and " << y << " is " << result << std::endl;
return 0;
}
Explanation:
- This C++ program calculates the modulus of two float numbers using the fmod() function.
- First, the program defines two float variables, x and y, and initializes them to 10.5 and 3.2, respectively.
- Next, the program calls the fmod() function to calculate the modulus of x and y.
- The fmod() function is included in the <cmath> header file and takes two arguments: the dividend and the divisor.
- In this case, x is the dividend, and y is the divisor. The fmod() function returns the remainder of the division of x by y.
- Finally, the program outputs the result using std::cout. The message printed to the console includes the original values of x and y as well as the calculated modulus.
Program Output:

Example 2: Modulus of two double numbers
#include <iostream>
#include <cmath>
int main() {
double a = 10.5;
double b = 3.2;
double result = std::fmod(a, b);
std::cout << result << std::endl;
return 0;
}
Explanation:
- This program calculates the modulus of two double numbers using the std::fmod function from the cmath library.
- The program first initializes two double variables, a and b, with the values 10.5 and 3.2, respectively.
- Then, it calls the std::fmod function to calculate the modulus of a and b and assigns the result to the result variable. Finally, the program outputs the value of the result to the console.
- The std::fmod function calculates the floating-point remainder of a divided by b.
- It takes two arguments of type double and returns a double value.
- If either argument is NaN (Not a Number), the result is NaN. If b is zero, the result is implementation-defined (may raise a floating-point exception, return NaN, or return an unspecified value).
- In this program, the result of std::fmod(a, b) is assigned to a double variable result, which is then output to the console using the std::cout object and the << operator.
Program Output:

Modulus and Special Values in Programming
When dealing with special floating-point values such as NaN (Not a Number), infinity, and zero, the behaviour of the modulus operation can be undefined or implementation-dependent in programming. In some programming languages, the modulus operation may return NaN or throw an exception when used with NaN or infinity.
Conclusion:
In conclusion, the modulus function in C++ is a useful tool for computing the remainder of a division operation between two numbers.
This function is particularly important when dealing with floating-point numbers, as the division operation can result in inaccuracies due to the nature of their representation in memory.
By using the modulus function, we can obtain an accurate result that takes into account the precision and rounding errors of floating-point numbers.
Overall, the modulus function is a valuable tool for performing accurate division operations on floating-point numbers in C++.
Whether you are working on a simple arithmetic problem or a complex mathematical calculation, the modulus function can help you obtain precise and reliable results.