Python fmod

Python 的 fmod 数学函数用于计算指定给定参数的模。在本节中,我们将通过一个示例讨论如何使用 fmod 函数。

math fmod 函数的语法如下所示。

math.fmod(x, y);
  • fmod 函数使用 X 和 Y 参数来查找模。如果 X 和 Y 参数都为零,它将返回 ValueError 作为输出。
  • 如果 Y 参数(第二个参数)为零,它将返回 ValueError 作为输出。
  • 如果 X 值或 Y 值不是数字,它将返回 TypeError。

Python fmod 函数示例

fmod 函数计算指定给定参数的模。在此示例中,我们将查找不同数据类型的模并显示输出。

import math

Tup = (10, 20, 12, -40 , 50) # Tuple Declaration
Lis = [-98, 32, -39, -42 , 15] # List Declaration

print('Calculating MOD of Positive Number = %.2f' %math.fmod(2, 3))
print('Calculating MOD of Negative Number = %.2f' %math.fmod(-14, 3))

print('Calculating MOD of Positive Decimal = %.2f' %math.fmod(22.45, 4.5))
print('Calculating MOD of Negative Decimal = %.2f' %math.fmod(-110, 14.78))

print('Calculating MOD of Tuple Item = %.2f' %math.fmod(Tup[2], 5))
print('Calculating MOD of Tuple Item = %.2f' %math.fmod(Tup[2], -5))
print('Calculating MOD of List Item = %.2f' %math.fmod(Lis[4], 4))
print('Calculating MOD of List Item = %.2f' %math.fmod(Lis[0], -15))

print('Calculating MOD of Multiple Number = %.2f' %math.fmod(10 + 20 - 12, 40))

print(' Calculating MOD of String Value = ', math.fmod('2', 3))
math fmod Function Example

我们尝试将其用于字符串值,它返回 TypeError 作为输出。请参考 Python 中的 元组列表数学函数