Improve power.py (#12567)
* Fix And Add power.py
To fix the inaccuracies and allow handling of negative exponents and bases, the key issue lies in how negative numbers are handled in the power calculation, especially when dividing.
## Example Output:
```python
>>> power(4, 6)
4096
>>> power(2, 3)
8
>>> power(-2, 3)
-8
>>> power(2, -3)
0.125
>>> power(-2, -3)
-0.125
```
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update power.py
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update power.py
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update power.py
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update power.py
---------
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Maxim Smolskiy <mithridatus@mail.ru>
2025-02-10 01:51:18 +08:00
|
|
|
def actual_power(a: int, b: int) -> int:
|
2020-01-12 06:04:10 +02:00
|
|
|
"""
|
|
|
|
Function using divide and conquer to calculate a^b.
|
|
|
|
It only works for integer a,b.
|
2024-06-01 05:09:03 -04:00
|
|
|
|
|
|
|
:param a: The base of the power operation, an integer.
|
|
|
|
:param b: The exponent of the power operation, a non-negative integer.
|
|
|
|
:return: The result of a^b.
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
>>> actual_power(3, 2)
|
|
|
|
9
|
|
|
|
>>> actual_power(5, 3)
|
|
|
|
125
|
|
|
|
>>> actual_power(2, 5)
|
|
|
|
32
|
|
|
|
>>> actual_power(7, 0)
|
|
|
|
1
|
2020-01-12 06:04:10 +02:00
|
|
|
"""
|
|
|
|
if b == 0:
|
|
|
|
return 1
|
Improve power.py (#12567)
* Fix And Add power.py
To fix the inaccuracies and allow handling of negative exponents and bases, the key issue lies in how negative numbers are handled in the power calculation, especially when dividing.
## Example Output:
```python
>>> power(4, 6)
4096
>>> power(2, 3)
8
>>> power(-2, 3)
-8
>>> power(2, -3)
0.125
>>> power(-2, -3)
-0.125
```
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update power.py
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update power.py
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update power.py
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update power.py
---------
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Maxim Smolskiy <mithridatus@mail.ru>
2025-02-10 01:51:18 +08:00
|
|
|
half = actual_power(a, b // 2)
|
|
|
|
|
2020-01-12 06:04:10 +02:00
|
|
|
if (b % 2) == 0:
|
Improve power.py (#12567)
* Fix And Add power.py
To fix the inaccuracies and allow handling of negative exponents and bases, the key issue lies in how negative numbers are handled in the power calculation, especially when dividing.
## Example Output:
```python
>>> power(4, 6)
4096
>>> power(2, 3)
8
>>> power(-2, 3)
-8
>>> power(2, -3)
0.125
>>> power(-2, -3)
-0.125
```
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update power.py
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update power.py
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update power.py
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update power.py
---------
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Maxim Smolskiy <mithridatus@mail.ru>
2025-02-10 01:51:18 +08:00
|
|
|
return half * half
|
2020-01-12 06:04:10 +02:00
|
|
|
else:
|
Improve power.py (#12567)
* Fix And Add power.py
To fix the inaccuracies and allow handling of negative exponents and bases, the key issue lies in how negative numbers are handled in the power calculation, especially when dividing.
## Example Output:
```python
>>> power(4, 6)
4096
>>> power(2, 3)
8
>>> power(-2, 3)
-8
>>> power(2, -3)
0.125
>>> power(-2, -3)
-0.125
```
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update power.py
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update power.py
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update power.py
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update power.py
---------
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Maxim Smolskiy <mithridatus@mail.ru>
2025-02-10 01:51:18 +08:00
|
|
|
return a * half * half
|
2020-01-12 06:04:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
def power(a: int, b: int) -> float:
|
|
|
|
"""
|
2024-06-01 05:09:03 -04:00
|
|
|
:param a: The base (integer).
|
|
|
|
:param b: The exponent (integer).
|
|
|
|
:return: The result of a^b, as a float for negative exponents.
|
|
|
|
|
2020-01-12 06:04:10 +02:00
|
|
|
>>> 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:
|
Improve power.py (#12567)
* Fix And Add power.py
To fix the inaccuracies and allow handling of negative exponents and bases, the key issue lies in how negative numbers are handled in the power calculation, especially when dividing.
## Example Output:
```python
>>> power(4, 6)
4096
>>> power(2, 3)
8
>>> power(-2, 3)
-8
>>> power(2, -3)
0.125
>>> power(-2, -3)
-0.125
```
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update power.py
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update power.py
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update power.py
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update power.py
---------
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Maxim Smolskiy <mithridatus@mail.ru>
2025-02-10 01:51:18 +08:00
|
|
|
return 1 / actual_power(a, -b)
|
2020-01-12 06:04:10 +02:00
|
|
|
return actual_power(a, b)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
Improve power.py (#12567)
* Fix And Add power.py
To fix the inaccuracies and allow handling of negative exponents and bases, the key issue lies in how negative numbers are handled in the power calculation, especially when dividing.
## Example Output:
```python
>>> power(4, 6)
4096
>>> power(2, 3)
8
>>> power(-2, 3)
-8
>>> power(2, -3)
0.125
>>> power(-2, -3)
-0.125
```
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update power.py
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update power.py
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update power.py
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update power.py
---------
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Maxim Smolskiy <mithridatus@mail.ru>
2025-02-10 01:51:18 +08:00
|
|
|
print(power(-2, -3)) # output -0.125
|