def check_pow(config, num, exp, mod=None):
if num == 0 and exp < 0:
# 0 ** -1 raises a ZeroDivisionError
return False
if num < 0 and exp < 1.0 and exp != 0.0:
# pow(-25, 0.5) raises a ValueError
return False
if mod is not None:
# pow(a, b, m) only works if a and b are integers
if not isinstance(num, int):
return False
if not isinstance(exp, int):
return False
if mod == 0:
# pow(2, 1024, 0) raises a ValueError:
# 'pow() 3rd argument cannot be 0'
return False
if (isinstance(num, int)
and isinstance(exp, int)
# don't call log2(0) (error)
and num != 0
# if exp < 0, the result is a float which has a fixed size
and exp > 0):
# bits(num ** exp) = log2(num) * exp
if math.log2(abs(num)) * exp >= config.max_int_bits:
# pow() result will be larger than max_constant_size.
return False
return True
评论列表
文章目录