mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-03-12 17:49:50 +00:00
Update power.py
This commit is contained in:
parent
c79cb95ddb
commit
456e4fbf3e
@ -29,26 +29,14 @@ def actual_power(a: int, b: int):
|
||||
return a * half * half
|
||||
|
||||
def power(a: int, b: int) -> float:
|
||||
"""
|
||||
"""
|
||||
:param a: The base (integer).
|
||||
:param b: The exponent (integer).
|
||||
:return: The result of a^b, as a float for negative exponents.
|
||||
|
||||
>>> power(4,6)
|
||||
4096
|
||||
>>> power(2,3)
|
||||
8
|
||||
>>> power(-2,3)
|
||||
-8
|
||||
>>> power(2,-3)
|
||||
0.125
|
||||
>>> power(-2,-3)
|
||||
-0.125
|
||||
"""
|
||||
if b < 0:
|
||||
return 1 / actual_power(a, b)
|
||||
return 1 / actual_power(a, -b)
|
||||
return actual_power(a, b)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(power(-2, -3))
|
||||
print(power(-2, -3)) #output -0.125
|
||||
|
Loading…
x
Reference in New Issue
Block a user