vivin - 11 months ago 49

Python Question

'i am not getting the correct output while giving(9,-8).how to solve this without using pow function and ** operator'

`def power(x,y):`

if x == 0:

return x

if y == -1:

return 1

else:

return -1

if y == 0:

return 1

a = power(x,y//2)

if y%2 == 0:

return a*a

return a*a*x

print power(9,-8)

Answer Source

```
def power(x,y):
if y == 0:
return 1
a = power(x, y/2);
if y%2 == 0:
return a*a;
else:
return x*a*a;
x = 9
y = -8
if y < 0:
print 1.0/(power(x,abs(y)))
else:
print power(x,abs(y))
```

Keep it simple like this..

My pow() function calculates `x^y`

when `y`

is positive. If we want to find `x^y`

for a negative `y`

, then simply print `1/pow(x,y)`

Note: @vivin: Does it answer correctly now?

Hope it helps!!!!