mirror of
https://github.com/Sosokker/Calculator-for-Matrix-and-Algebra.git
synced 2025-12-19 05:04:06 +01:00
Add reduce_frac (Turn Frac into reduce form)
This commit is contained in:
parent
1e6a977102
commit
22b41cb4b6
@ -1,3 +1,5 @@
|
||||
from math import gcd
|
||||
|
||||
class Fraction:
|
||||
def __init__(self, numer, denom):
|
||||
if numer < 0 and denom < 0:
|
||||
@ -126,6 +128,18 @@ class Fraction:
|
||||
denom = self.denom**(-other)
|
||||
return Fraction(denom, numer)
|
||||
|
||||
def reduce_frac(self):
|
||||
"""
|
||||
Turn Fraction into reduce form.
|
||||
>>> m1 = Fraction(2, 4)
|
||||
>>> m1.reduce_frac()
|
||||
>>> print(m1)
|
||||
1/2
|
||||
"""
|
||||
temp = gcd(self.numer, self.denom)
|
||||
self.numer = self.numer // temp
|
||||
self.denom = self.denom // temp
|
||||
|
||||
def __str__(self) -> str:
|
||||
"""
|
||||
Print Fraction Form.
|
||||
|
||||
Loading…
Reference in New Issue
Block a user