Python Math.fmod()方法

在Python中,math 模块提供了fmod() 方法来寻找两个数字的余数–分子和分母。这些数字可以是正/负的整数或浮点数。

math.fmod() 方法是在Python 1.6版本中引入的,是Python文档中理论和表示函数的一部分。

操作符%math.fmod() 方法类似,当其中一个数是非正数时,它返回不同的结果。我们鼓励程序员在求浮点数的模数时使用math.fmod() 方法。

Pythonmath.fmod() 方法的语法

math.fmod(number1, number2)

参数

number1 这是一个必需的参数。它可以是一个整数或浮点数,在除法中充当分子。
number2 这是一个必要的参数。它可以是一个整数或浮点数,在除法中作为分母。

返回

该方法返回两个数字相除后的余数,即浮动值。

示例代码:使用Python中的math.fmod() 方法

要使用math.fmod() 方法,我们需要传递两个参数:分子和分母。这些参数以浮点数的形式输出一个余数。

这些参数可以是正数或负数,甚至可以是整数或浮点数。记住,这些参数不能同时持有零,否则会返回ValueError 异常。

如果分母为零,ValueError 异常也会发生。

# import math module to use the math.fmod() method
import math
# positive and negative numbers are used as a parameter to test the behavior of the method
print('The result of fmod(11,6) is',math.fmod(11, 6))
print('The result of fmod(-20,4) is',math.fmod(-20, 4))
print('The result of fmod(7,-2) is',math.fmod(7, -2))
print('The result of fmod(0,6) is',math.fmod(0, 6))
print('The result of fmod(22.1,3.4) is',math.fmod(22.1, 3.4))
print('The result of fmod(22.1,3.4) is',math.fmod(14, 7.1))

输出:

The result of fmod(11,6) is 5.0
The result of fmod(-20,4) is -0.0
The result of fmod(7,-2) is 1.0
# output always zero, if the numerator is zero
The result of fmod(0,6) is 0.0
The result of fmod(22.1,3.4) is 1.700000000000002
The result of fmod(22.1,3.4) is 6.9

示例代码:使用math.fmod() 方法时ValueError: math domain error 的原因

在Python中,我们使用方法math.fmod() 来寻找两个数字的余数。当我们传递零作为分母值时,方法math.fmod() 返回ValueError 异常。

注意,我们不能把零作为第二个参数传给这个方法,否则这个方法会抛出一个异常。

import math
# modulus method returns the ValueError if there is zero in denominator
def modulus(num1, num2):
    return math.fmod(num1,num2)
print('The result of fmod(10,0) is',modulus(10, 0))

输出:

# exception as output, when number zero is passed to the second parameter.
ValueError: math domain error

示例代码:使用math.fmod() 方法时出现TypeError 的原因

正如我们所知,math.fmod() 方法找到的是数字的余数,所以两个必要参数不能接受数字以外的数据类型。当我们传递一个字符串(不是数字)时,math.fmod() 方法会返回TypeError 异常。

为了避免这种情况,我们必须仔细选择并传递正确的参数(一个数字)给该方法。

import math
# method returns TypeError on incorrect data type
def modulus(num1, num2):
    return math.fmod(num1,num2)
print('The result of fmod(10,0) is',modulus("10", 0))

输出:

# returns this exception when one of the parameters is a string.
TypeError: must be real number, not str

示例代码:% 操作符和math.fmod() 方法之间的区别

在Python中,有一个类似的运算符来计算数字的余数,被称为% modulus运算符。这是Python中的一个内置运算符,不是一个函数。

我们取两个正整数来显示% 模数运算符和math.fmod() 方法之间的相似性。这两个运算的结果显示相同。

然而,当我们将一个正整数与负整数一起传递时,我们可以看到操作之间的区别。此外,当我们在分母中给出零的时候,也可以看到区别。

当分母中有零值的数字时,math.fmod() 方法总是返回零,与% 模数不同,它显示ValueError 。它不能解决分母中的零值问题。

我们在处理浮点数时使用math.fmod() 方法,在处理整数值时使用% 模数运算符。

import math
# method returns output for fmod() method only
def modulus(num1, num2):
    return math.fmod(num1,num2)
print('The result of fmod(10,0) is ',modulus(10, 3))
print('The result of 10 % 0 is ', 10 % 3)
print('The result of fmod(-11,6) is ',modulus(-11, 6))
print('The result of -11 % 6 is ', -11 % 6)
print('The result of fmod(-19,-5) is ',modulus(-19, -5))
print('The result of -19 % -5 is ', -19 % -5)
print('The result of fmod(0,9) is ',modulus(0, 9))
print('The result of 0 % 9 is ', 0 % 9)
# number zero cannot be used in the denominator for the % modulus operator
print('The result of fmod(4,0) is ',modulus(4, 0))
# prints the ValueError exception on the screen
print('The result of 4 % 0 is ', 4 % 0)

输出:

The result of fmod(10,0) is  1.0
The result of 10 % 0 is  1
# difference shows when there is a non-positive number involved in the division
The result of fmod(-11,6) is  -5.0
The result of -11 % 6 is  1
The result of fmod(-19,-5) is  -4.0
The result of -19 % -5 is  -4
The result of fmod(0,9) is  0.0
The result of 0 % 9 is  0
Traceback (most recent call last):
  File "C:Userslenovo.spyder-py3temp.py", line 18, in <module>
    print('The result of fmod(4,0) is ',modulus(4, 0))
  File "C:Userslenovo.spyder-py3temp.py", line 4, in modulus
    return math.fmod(num1,num2)
ValueError: math domain error