I need to write a power function in Python which works with real base and real exponent.
a^b && a,b ∈ R
I'm stuck at this point:
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
This only works with real base though. With real exp the numbers differ, for example:
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
Any help is appreciated.