# fraction.py: implement fractions
import sys
# num = top = numerator; den = bottom = denomiator
def gcd(m,n): # m can be negative
while m%n != 0:
oldm = m
oldn = n
m = oldn
n = oldm%oldn
return n
class Fraction:
def __init__(self,top,bottom):
self.num = top
self.den = bottom
#sys.stdout.write("New: ")
#Fraction.show(self)
#sys.stdout.write("\n")
def __str__(self):
return str(self.num)+"/"+str(self.den)
def show(self):
sys.stdout.write(str(self.num) + "/" +
str(self.den))
def __add__(self,other):
newnum = self.num*other.den + \
self.den*other.num
newden = self.den * other.den
com = gcd(newnum,newden)
return Fraction(newnum//com,newden//com)
def __sub__(self,other):
newnum = self.num*other.den - \
self.den*other.num
newden = self.den * other.den
com = gcd(newnum,newden)
return Fraction(newnum//com,newden//com)
def __mul__(self, fract):
newnum = self.num*fract.num
newden = self.den*fract.den
common = gcd(newnum, newden)
return Fraction(newnum//common,
newden//common)
def __div__(self, other):
sign = 1
if other.num == 0:
return None # divide by 0
if other.num < 0:
sign = -1
othernum = -other.num
else:
othernum = other.num
newnum = self.num*other.den
newden = self.den*othernum
common = gcd(newnum, newden)
return Fraction(sign*newnum//common,
newden//common)
def __eq__(self, other):
firstnum = self.num * other.den
secondnum = other.num * self.den
return firstnum == secondnum
def __ne__(self, other):
return not self.__eq__(other)
def __lt__(self, other):
firstnum = self.num * other.den
secondnum = other.num * self.den
return firstnum < secondnum
def __le__(self, other):
return self.__lt__(other) or \
self.__eq__(other)
def __ge__(self, other):
return not self.__lt__(other)
def __gt__(self, other):
return not self.__le__(other)
def float(self):
return float(self.num) / float(self.den)
def getNum(self):
return self.num
def getDen(self):
return self.den
|
# test_fraction.py: test fraction class
import sys
from mpmath import * # arbitrary precision
from fraction import Fraction
def partA():
u = Fraction(1, 2)
v = Fraction(2, 3)
w = Fraction(3, 7)
x = Fraction(-4, 11)
y = Fraction(-7, 13)
z = Fraction(-2, 5)
sys.stdout.write("x.getNum(): " +
str(x.getNum()) + "\n")
sys.stdout.write("x.getDen(): " +
str(x.getDen()) + "\n")
sys.stdout.write("u != v: " + str(u != v) + "\n")
sys.stdout.write("u < v: " + str(u < v) + "\n")
sys.stdout.write("u <= v: " + str(u <= v) + "\n")
sys.stdout.write("u == v: " + str(u == v) + "\n")
sys.stdout.write("u > v: " + str(u > v) + "\n")
sys.stdout.write("u >= v: " + str(u >= v) + "\n")
sys.stdout.write("x != y: " + str(x != y) + "\n")
sys.stdout.write("x < y: " + str(x < y) + "\n")
sys.stdout.write("x <= y: " + str(x <= y) + "\n")
sys.stdout.write("x == y: " + str(x == y) + "\n")
sys.stdout.write("x > y: " + str(x > y) + "\n")
sys.stdout.write("x >= y: " + str(x >= y) + "\n")
sys.stdout.write("u + v: " + str(u + v) + "\n")
sys.stdout.write("u - v: " + str(u - v) + "\n")
sys.stdout.write("w + x: " + str(w + x) + "\n")
sys.stdout.write("w - x: " + str(w - x) + "\n")
sys.stdout.write("u + y: " + str(u + y) + "\n")
sys.stdout.write("u - y: " + str(u - y) + "\n")
sys.stdout.write("x + y: " + str(x + y) + "\n")
sys.stdout.write("x - y: " + str(x - y) + "\n")
sys.stdout.write("w * x: " + str(w * x) + "\n")
sys.stdout.write("y * z: " + str(y * z) + "\n")
sys.stdout.write("w * y: " + str(w * y) + "\n")
sys.stdout.write("u / v: " + str(u / v) + "\n")
sys.stdout.write("w / x: " + str(w / x) + "\n")
sys.stdout.write("x / z: " + str(x / z) + "\n")
sys.stdout.write("y / v: " + str(y / v) + "\n")
def partB():
c1 = Fraction(1, 1)
c2 = Fraction(2, 1)
c3 = Fraction(3, 1)
c7 = Fraction(7, 1)
c14 = Fraction(14, 1)
c15 = Fraction(15, 1)
c292 = Fraction(292, 1)
pi1 = c3 + c1 / (c7 + c1 / (c15 + c1))
pi2 = c3 + c1 / (c7 + c1 / (c15 + c1 / (c1 +
c1 / c292)))
pi3 = c3+c1/(c7+c1/(c15+c1/(c1+c1/(c292+c1/(c1+
c1/(c1+c1/(c1+c1/(c2+c1/(c1+c1/(c3+c1/(c1+
c1/c14)))))))))))
sys.stdout.write("pi1: " + str(pi1) + "\n")
sys.stdout.write("pi1: %20.16f\n" %
(float(pi1.getNum()) / float(pi1.getDen())) )
sys.stdout.write("pi2: " + str(pi2) + "\n")
sys.stdout.write("pi2: %20.16f\n" %
(float(pi2.getNum()) / float(pi2.getDen())) )
sys.stdout.write("pi3: " + str(pi3) + "\n")
sys.stdout.write("pi3: %20.16f\n" %
(float(pi3.getNum()) / float(pi3.getDen())) )
def partC(j):
sys.stdout.write("\nCall C(" + str(j) + ")\n")
c1 = Fraction(1,1)
c2 = Fraction(2,1)
c4 = Fraction(4,1)
fi = Fraction(j, 1)
ft = Fraction(2*j+1, 1)
while fi >= c1:
ft = (fi * fi) / ft
ft = ((c2 * fi) - c1) + ft
fi = fi - c1
ft = c4 / ft
sys.stdout.write(str(ft.getNum()) + " /\n")
sys.stdout.write(str(ft.getDen()) + " \n")
mp.dps = j # set precision = j digits
sys.stdout.write("High precision: " +
str(mp.dps) + "\n")
n = mpf(ft.getNum()) # hi precision num
d = mpf(ft.getDen()) # hi precision den
sys.stdout.write("Fraction:" + str(n/d) + "\n")
sys.stdout.write("Pi: " + str(pi) + "\n")
sys.stdout.write("Part A:\n")
partA()
sys.stdout.write("\nPart B:\n")
partB()
sys.stdout.write("\nPart C:\n")
partC(15)
partC(20)
partC(50)
partC(100)
|