Open In App

Modulus of two float or double numbers

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++ program to find modulo of floating 
// point numbers.
#include <bits/stdc++.h>
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 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.
// 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.
<script>

// Javascript program to find 
// modulo of floating point numbers.

function findMod(a, b)
{
    let 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

    let a = 9.7, b = 2.3;
    document.write(findMod(a, b));

//This code is contributed by Mayank Tyagi
</script>
<?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.
?>
# 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.

Output
0.5


We can use the inbuilt fmod function to find the modulus of two floating-point numbers. 

// 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;
}
// 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 
// C# program to find modulo of floating
// point numbers using library function.
using System;

class GFG{
  
static void Main() 
{
    double a = 9.7;
    double b = 2.3;
    Console.WriteLine(a % b);
}
}

// This code is contributed by mukesh07
<script>

// Javascript program to find modulo of
// floating point numbers using
// library function.

// Driver Code
let a = 9.7; 
let b = 2.3;
document.write(a%b);

// This code is contributed by mohan pavan

</script>
<?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
?>
# 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

Output
0.5

Using the abs() function :

Approach:

The abs() function returns the absolute value of a number, which in this case, will be the modulus of the two given floating-point numbers.

#include <iostream>
#include <cmath>
#include <chrono>

int main() {
    double a = 9.7;
    double b = 2.3;

    std::chrono::steady_clock::time_point start_time = std::chrono::steady_clock::now();
    double modulus = std::abs(std::fmod(a, b));
    std::chrono::steady_clock::time_point end_time = std::chrono::steady_clock::now();

    std::cout << "Modulus of " << a << " and " << b << " is: " << modulus << std::endl;
    std::cout << "Time taken: " << std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time).count() / 1000.0 << " seconds" << std::endl;

    return 0;
}
import java.time.Duration;
import java.time.Instant;

public class Main {
    public static void main(String[] args) {
        double a = 9.7;
        double b = 2.3;

        Instant startTime = Instant.now();  // Record the start time
        double modulus = Math.abs(a % b);  // Calculate the modulus
        Instant endTime = Instant.now();  // Record the end time

        System.out.println("Modulus of " + a + " and " + b + " is: " + modulus);
        
        // Calculate and print the time taken in seconds
        double durationInSeconds = Duration.between(startTime, endTime).toMillis() / 1000.0;
        System.out.println("Time taken: " + durationInSeconds + " seconds");
    }
}
using System;
using System.Diagnostics;

class Program
{
    static void Main(string[] args)
    {
        double a = 9.7;
        double b = 2.3;

        // Create a Stopwatch to measure elapsed time
        Stopwatch stopwatch = new Stopwatch();
        stopwatch.Start(); // Start the stopwatch

        // Calculate the modulus of 'a' and 'b' and take the absolute value
        double modulus = Math.Abs(a % b);

        stopwatch.Stop(); // Stop the stopwatch after the calculation

        // Print the result and the time taken in seconds
        Console.WriteLine($"Modulus of {a} and {b} is: {modulus}");
        Console.WriteLine($"Time taken: {stopwatch.ElapsedMilliseconds / 1000.0} seconds");
    }
}
// Get the current time
const startTime = new Date();

// Define the values
const a = 9.7;
const b = 2.3;

// Calculate the modulus
const modulus = Math.abs(a % b);

// Get the current time again
const endTime = new Date();

// Print the modulus
console.log("Modulus of " + a + " and " + b + " is: " + modulus);

// Calculate and print the time taken in seconds
const durationInSeconds = (endTime - startTime) / 1000;
console.log("Time taken: " + durationInSeconds + " seconds");
import time

a = 9.7
b = 2.3

start_time = time.time()
modulus = abs(a % b)
end_time = time.time()

print("Modulus of", a, "and", b, "is:", modulus)
print("Time taken:", end_time - start_time, "seconds")

Output
Modulus of 9.7 and 2.3 is: 0.5
Time taken: 0 seconds

Time Complexity: O(1)
Space Complexity: O(1)

Article Tags :