math.fmod()方法是数学模块的一种库方法,用于查找模数或给定数字,其中第一个参数是除数,第二个参数是除数。它接受两个参数,并以浮点类型返回模数。
注意: math.fmod() 可用于获取正负整数,正负浮点数的模数/余数。
它的语法 math.fmod() 方法:
math.fmod(a, b)
Parameter(s): a,b –数字(正数或负数,整数或浮点数)。
返回值: float-返回一个浮点值,它是给定数字a,b的余数(模)。
示例
Input: a = -10 b = 3 # 函数调用 print(math.fmod(a,b)) Output: -1.0
# Python代码演示示例 # fmod() method # 导入数学 import math # 整数正数 a = 10 b = 3 # 计算模块 result = math.fmod(a,b)print("result = ", result) # 整数负数 a = -10 b = 3 result = math.fmod(a,b)print("result = ", result) # 浮动正数 a = 10.123 b = 3.12 result = math.fmod(a,b)print("result = ", result) # 浮动负数 a = -10.123 b = 3.12 result = math.fmod(a,b)print("result = ", result)
输出结果
result = 1.0 result = -1.0 result = 0.762999999999999 result = -0.762999999999999