题目:给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。
分析:
code:
def getPower(base, exponent):
if base == 0:
return 0
if exponent == 1:
return base
res = 1
i = 0
exponent1 = abs(exponent)
while i < exponent1:
res *= base
i += 1
if exponent < 1:
return 1 / res
else:
return res
if __name__ == "__main__":
print(getPower(3, -4))
程序运行结果:
0.012345679012345678
网友评论