Skip to content

Instantly share code, notes, and snippets.

@RK11
Created July 11, 2012 19:37
Show Gist options
  • Select an option

  • Save RK11/3092731 to your computer and use it in GitHub Desktop.

Select an option

Save RK11/3092731 to your computer and use it in GitHub Desktop.
PyFraction provides fraction for Python
class fraction:
"""This class provides fraction representation and basic operation (+, -, * and /). Operations can be done with fractions and whole numbers)"""
def __init__(self, numerator, denominator = 1):
"""To create a fraction, you can use either numerator and denominator, or only the numerator.
If you give only one decimal number, the find_fraction method will find the appropriate fraction."""
self.numerator = numerator
self.denominator = denominator
if str(self.numerator).find(".") != -1:
self.find_fraction()
def __repr__(self):
"""Used to print fraction in a x/x format"""
return("{}/{}".format(self.numerator, self.denominator))
def __str__(self):
"""Same as __repr__ but for str()"""
return("{}/{}".format(self.numerator, self.denominator))
def __add__(self, other_fraction):
"""Used for adding two fractions. The result is simplified"""
if str(other_fraction).find("/") == -1:
other_fraction = fraction(other_fraction)
n = (self.numerator * other_fraction.denominator) + (self.denominator * other_fraction.numerator)
d = (self.denominator * other_fraction.denominator)
result = fraction(n, d)
result.simplify()
return result
def __sub__(self, other_fraction):
"""Used for subtracting two fractions. The result is simplified"""
if str(other_fraction).find("/") == -1:
other_fraction = fraction(other_fraction)
n = (self.numerator * other_fraction.denominator) + (self.denominator * -1 * other_fraction.numerator)
d = (self.denominator * other_fraction.denominator)
result = fraction(n, d)
result.simplify()
return result
def __mul__(self, other_fraction):
"""Used for multiplying two fractions. The result is simplified"""
if str(other_fraction).find("/") == -1:
other_fraction = fraction(other_fraction)
result = fraction(self.numerator * other_fraction.numerator, self.denominator * other_fraction.denominator)
result.simplify()
return result
def __div__(self, other_fraction):
"""Used for dividing two fraction. The result is simplified"""
if str(other_fraction).find("/") == -1:
other_fraction = fraction(other_fraction)
result = fraction(self.numerator * other_fraction.denominator, self.denominator * other_fraction.numerator)
result.simplify()
return result
def __pow__(self, power): #No fraction power yet, only whole numbers
if str(power).find("/") == -1:
power = fraction(power)
result = fraction(self.numerator ** power.digits(), self.denominator ** power.digits())
result.simplify()
return result
def simplify(self):
"""Method to simplify the faction using the greatest common divisor"""
a = self.numerator
b = self.denominator
r = 1
while r != 0:
r = a % b
a, b = b, r
self.numerator /= a
self.denominator /= a
def inverse(self):
"""Inverse the fraction"""
self.numerator, self.denominator = self.denominator, self.numerator
def digits(self):
"""Returns the fraction as a decimal number"""
return self.numerator*1.0/self.denominator
def find_fraction(self):
"""Will find the exacot or approximate fraction for a decimal number"""
nbr = self.numerator
for a in range(1000000):
try:
b = a/nbr
check = str(b)[str(b).find(".")+1:str(b).find(".")+4]
if check == "000" or check == "999" or check == "001" or check == "0":
if str(1.0*a/(int(b)))[:8] == str(nbr)[:8]:
self.numerator, self.denominator = a, int(a/nbr)
break
except ZeroDivisionError:
check = ""
@mouuff
Copy link

mouuff commented Jul 11, 2012

pour appeller une methode de la classe depuis cette classe utilise self.methode(), il y a des problemes un peu partout mais j'imagine que c'est une beta

@RK11
Copy link
Author

RK11 commented Jul 11, 2012

Des problèmes ? lesquels ?

@RK11
Copy link
Author

RK11 commented Jul 12, 2012

J'ai ajouté une autodétection des fraction, par exemple fraction(3.141592) fera 355/113.

@mouuff
Copy link

mouuff commented Jul 12, 2012

cool

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment