Modulus of two float or double numbers
Given two floating point numbers, find remainder.
Examples:
Input : a = 36.5,
b = 5.0
Output : 1.5
Input : a = 9.7,
b = 2.3
Output : 0.5
A simple solution is to do repeated subtraction.
C++
// CPP program to find modulo of floating // point numbers. #include <bits/stdc++.h> using namespace std; double findMod(double a, double b) { // Handling negative values if (a < 0) a = -a; if (b < 0) b = -b; // Finding mod by repeated subtraction double mod = a; while (mod >= b) mod = mod - b; // Sign of result typically depends // on sign of a. if (a < 0) return -mod; return mod; } // Driver Function int main() { double a = 9.7, b = 2.3; cout << findMod(a, b); return 0; } |
chevron_right
filter_none
Java
// Java program to find modulo of floating // point numbers class GFG { static double findMod(double a, double b) { // Handling negative values if (a < 0) a = -a; if (b < 0) b = -b; // Finding mod by repeated subtraction double mod = a; while (mod >= b) mod = mod - b; // Sign of result typically depends // on sign of a. if (a < 0) return -mod; return mod; } // Driver code public static void main (String[] args) { double a = 9.7, b = 2.3; System.out.print(findMod(a, b)); } } // This code is contributed by Anant Agarwal. |
chevron_right
filter_none
Python3
# Python3 program to find modulo # of floating point numbers. def findMod(a, b): # Handling negative values if (a < 0): a = -a if (b < 0): b = -b # Finding mod by repeated subtraction mod = a while (mod >= b): mod = mod - b # Sign of result typically # depends on sign of a. if (a < 0): return -mod return mod # Driver code a = 9.7; b = 2.3print(findMod(a, b)) # This code is contributed by Anant Agarwal. |
chevron_right
filter_none
C#
// C# program to find modulo of floating // point numbers using System; class GFG { static double findMod(double a, double b) { // Handling negative values if (a < 0) a = -a; if (b < 0) b = -b; // Finding mod by repeated subtraction double mod = a; while (mod >= b) mod = mod - b; // Sign of result typically depends // on sign of a. if (a < 0) return -mod; return mod; } // Driver code public static void Main () { double a = 9.7, b = 2.3; Console.WriteLine(findMod(a, b)); } } // This code is contributed by vt_m. |
chevron_right
filter_none
PHP
<?php // PHP program to find modulo // of floatingpoint numbers. function findMod($a, $b) { // Handling negative values if ($a < 0) $a = -$a; if ($b < 0) $b = -$b; // Finding mod by repeated // subtraction $mod = $a; while ($mod >= $b) $mod = $mod - $b; // Sign of result typically // depends on sign of a. if ($a < 0) return -$mod; return $mod; } // Driver Code $a = 9.7; $b = 2.3; echo findMod($a, $b); // This code is contributed by anuj_65. ?> |
chevron_right
filter_none
Output :
0.5
We can use the inbuilt fmod function to find the modulus of two floating-point numbers.
C++
// CPP program to find modulo of floating // point numbers using library function. #include <bits/stdc++.h> using namespace std; // Driver Function int main() { double a = 9.7, b = 2.3; cout << fmod(a, b); return 0; } |
chevron_right
filter_none
PHP
<?php // PHP program to find modulo of // floating point numbers using // library function. // Driver Code $a = 9.7; $b = 2.3; echo fmod($a, $b); // This code is contributed // by inder_verma ?> |
chevron_right
filter_none
Output:
0.5
Recommended Posts:
- Difference between float and double in C/C++
- C program to find the size of int, float, double and char
- Modulus on Negative Numbers
- Modulus function in C++ STL
- Finding 'k' such that its modulus with each array element is same
- Modulus of all pairwise consecutive elements in an Array
- Comparison of a float with a value in C
- Function Overloading and float in C++
- Assigning an integer to float and comparison in C/C++
- gcvt() | Convert float value to string in C
- Double not (!!) operator in PHP
- Double Brace Initialization in Java
- Convert String to Double in Java
- Convert Double to Integer in Java
- Double forking to prevent Zombie process
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



