import math
python的math库并不提供简单的加减乘除的api,如:add
import math
# 求和
s = math.fsum([0.1, 0.2, 0.3]) # 0.6,减少浮点累积误差
# 连乘
p = math.prod([1, 2, 3, 4]) # 24
# 余数与商
r = math.fmod(5.0, 2.0) # 1.0(浮点余数,符号与被除数一致)
q, rem = divmod(5, 3) # (1, 2)
# 幂
x = math.pow(2, 3) # 8.0(浮点)
浙公网安备 33010602011771号