两个浮点数或double数的模数

本文概述

  • C ++
  • Java
  • Python3
  • C#
  • PHP
  • C ++
  • PHP
给定两个浮点数, 找到余数。
例子:
输入:a = 36.5, b = 5.0
输出:1.5
输入:a = 9.7, b = 2.3
输出:0.5
推荐:请解决实践首先, 在继续解决方案之前。 一种简单的解决方案是做反复的减法。
C ++
//CPP 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 subtractionwhile (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 numbersclass 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 - modreturn 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
< ?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. ?>

输出:
0.5

我们可以使用内置的fmod函数找出两个浮点数的模数。
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; }

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 ?>

【两个浮点数或double数的模数】输出如下:
0.5

    推荐阅读