mirror of
https://github.com/Sosokker/Calculator-for-Matrix-and-Algebra.git
synced 2025-12-19 21:24:05 +01:00
Add Power Method
This commit is contained in:
parent
b2fe8a53f4
commit
1e6a977102
@ -26,10 +26,10 @@ class Fraction:
|
|||||||
|
|
||||||
@denom.setter
|
@denom.setter
|
||||||
def denom(self, value):
|
def denom(self, value):
|
||||||
if denom < 0:
|
if value < 0:
|
||||||
numer = -numer
|
self.__numer = -self.numer
|
||||||
denom = -denom
|
self.__denom = -value
|
||||||
if denom == 0:
|
if value == 0:
|
||||||
raise ValueError("Denominator shouldn't be zero.")
|
raise ValueError("Denominator shouldn't be zero.")
|
||||||
self.__denom = value
|
self.__denom = value
|
||||||
|
|
||||||
@ -90,6 +90,42 @@ class Fraction:
|
|||||||
result = Fraction(numer, denom)
|
result = Fraction(numer, denom)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
def __pow__(self, other):
|
||||||
|
"""
|
||||||
|
Power Fraction.
|
||||||
|
>>> m1 = Fraction(2, 3)
|
||||||
|
>>> m2 = 2
|
||||||
|
>>> m3 = m1**m2
|
||||||
|
>>> print(m3)
|
||||||
|
4/9
|
||||||
|
>>> m1 = Fraction(-2, 3)
|
||||||
|
>>> m2 = 2
|
||||||
|
>>> m3 = m1**m2
|
||||||
|
>>> print(m3)
|
||||||
|
4/9
|
||||||
|
>>> m1 = Fraction(-2, 3)
|
||||||
|
>>> m2 = 3
|
||||||
|
>>> m3 = m1**m2
|
||||||
|
>>> print(m3)
|
||||||
|
-8/27
|
||||||
|
>>> m1 = Fraction(-2, 3)
|
||||||
|
>>> m2 = -3
|
||||||
|
>>> m3 = m1**m2
|
||||||
|
>>> print(m3)
|
||||||
|
-27/8
|
||||||
|
"""
|
||||||
|
if isinstance(other, int):
|
||||||
|
if other > 0:
|
||||||
|
numer = self.numer**other
|
||||||
|
denom = self.denom**other
|
||||||
|
return Fraction(numer, denom)
|
||||||
|
elif other == 0:
|
||||||
|
return 1
|
||||||
|
elif other < 0:
|
||||||
|
numer = self.numer**(-other)
|
||||||
|
denom = self.denom**(-other)
|
||||||
|
return Fraction(denom, numer)
|
||||||
|
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
"""
|
"""
|
||||||
Print Fraction Form.
|
Print Fraction Form.
|
||||||
@ -106,4 +142,6 @@ class Fraction:
|
|||||||
>>> print(m4)
|
>>> print(m4)
|
||||||
5/2
|
5/2
|
||||||
"""
|
"""
|
||||||
|
if self.numer == 0:
|
||||||
|
return "0"
|
||||||
return f"{self.numer}/{self.denom}"
|
return f"{self.numer}/{self.denom}"
|
||||||
Loading…
Reference in New Issue
Block a user