📜  两个浮点数或双数的模数

📅  最后修改于: 2021-05-30 10:27:54             🧑  作者: Mango

给定两个浮点数,找到余数。

例子:

一个简单的解决方案是进行重复减法。

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


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


输出 :

0.5

我们可以使用内置的fmod函数来找到两个浮点数的模数。

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


Java脚本


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

输出:

0.5
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”