Related Articles
Modulus of two float or double numbers
• Difficulty Level : Basic
• Last Updated : 30 Apr, 2021

Given two floating-point numbers, find the 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++

 `// C++ program to find modulo of floating``// point numbers.``#include ``using` `namespace` `std;` `double` `findMod(``double` `a, ``double` `b)``{``    ``double` `mod;``    ``// Handling negative values``    ``if` `(a < 0)``        ``mod = -a;``    ``else``        ``mod =  a;``    ``if` `(b < 0)``        ``b = -b;` `    ``// Finding mod by repeated subtraction``    ` `    ``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;``}`

## 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.`

## 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.3``print``(findMod(a, b))` `# This code is contributed by Anant Agarwal.`

## 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.`

## PHP

 `= ``\$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.``?>`

## Javascript

 ``

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 ``using` `namespace` `std;` `// Driver Function``int` `main()``{``    ``double` `a = 9.7, b = 2.3;``    ``cout << ``fmod``(a, b);``    ``return` `0;``}`

## Python3

 `# Python3 program to find modulo of floating``# point numbers using library function.``from` `math ``import` `fmod` `# Driver code``if` `__name__ ``=``=` `'__main__'``:``    ` `    ``a ``=` `9.7``    ``b ``=` `2.3``    ` `    ``print``(fmod(a, b))` `# This code is contributed by mohit kumar 29`

## PHP

 ``

## Javascript

 ``

## Java

 `// JAVA program to find modulo of floating``// point numbers using library function.``import` `java.util.*;` `class` `GFG{` `// Driver Function``public` `static` `void` `main(String[] args)``{``    ``double` `a = ``9.7``, b = ``2.3``;``    ``System.out.print((a % b));``}``}` `// This code contributed by umadevi9616`

Output:

`0.5`

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
My Personal Notes arrow_drop_up