python math.fmod() 方法
python math.fmod(x, y) 方法返回 x/y 的余数。
python 版本:2.7
语法math.fmod() 方法语法如下:
math.fmod(x, y)
参数说明:
- x -- 必需,正数或负数。被除数。如果 x 不是一个数字,返回 typeerror。
- y -- 必需,正数或负数。除数。如果 y 不是一个数字,返回 typeerror。
注意:如果 x 和 y = 0,则返回 valueerror。
注意:如果 y = 0,则返回 valueerror。
注意:如果 x 或 y 不是数字,则返回 typeerror。
返回值一个浮点值,表示 x/y 的余数
实例以下实例计算余数:
实例
# 导入 math 包
import math
# 计算余数
print(math.fmod(20, 4))
print(math.fmod(20, 3))
print(math.fmod(15, 6))
print(math.fmod(-10, 3))
# 报错,valueerror: math domain error
print(math.fmod(0, 0))
import math
# 计算余数
print(math.fmod(20, 4))
print(math.fmod(20, 3))
print(math.fmod(15, 6))
print(math.fmod(-10, 3))
# 报错,valueerror: math domain error
print(math.fmod(0, 0))
输出结果:
0.0
2.0
3.0
-1.0
traceback (most recent call last):
file "/users/51frw/51frw-test/test.py", line 9, in <module>
print(math.fmod(0, 0))
valueerror: math domain error【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!