我需要在 Python 中编写一个与实基数和实数指数一起使用的幂函数。
a^b && a,b ∈ R
我被困在这一点上:
def power_real_numbers(base, exp):
if isinstance(exp, int):
return power_bin_recursive(base, exp)
else:
integer = int(exp)
rational = int(str(exp).split('.')[1])
#power_bin_recursive() works fine
intval = power_bin_recursive(base, integer)
ratval = math.sqrt(rational)
if exp == 0:
return 1
elif exp < 0:
val = intval / ratval
else:
val = intval * ratval
return val
但这仅适用于真实基础。对于 real exp,数字不同,例如:
7.5 ^ 2.5 = 154.046969298, output by power_real_numbers is 125.778823734
7.5 ^ 0.5 = 2.73861278753, output by power_real_numbers is 2.2360679775
7.5 ^ -2.5 = 0.00649152660747, output by power_real_numbers is 0.00795046392
任何帮助表示赞赏。