Numerical divider function

def div(num, den):
	s = str(num); o = ""
	x = 0; l = len(s)
	f = 0; t = []
	while (x < l):
		d = int(s[x]); p = o
		while (d < den):
			if ((x + 1) < l):
				d = ((d * 10) + int(s[x + 1]))
				x += 1
			else:
				if (p == ""):
					p += "0"
				if (not "." in p):
					p += "."
					f = len(p)
				d = (d * 10)
				if (d < den):
					p += "0"
		i = (d / den)
		r = (i * den)
		if (f > 0):
			if (d in t):
				p = o[f+t.index(d):]
				break
			else:
				t.append(d)
		o = (p + str(i)); p = ""
		if (r != d):
			s = str(d - r)
			x = 0; l = len(s)
		else:
			x += 1
	if (p == ""):
		p = "0"
	print(str(num) + " / " + str(den) + " = " + o + " (" + p + ") ")
div(2, 4)
div(7, 22)
div(1, 999)
div(22, 7)
div(1, 3)

2 / 4 = 0.5 (0) 
7 / 22 = 0.318 (18) 
1 / 999 = 0.001 (001) 
22 / 7 = 3.142857 (142857) 
1 / 3 = 0.3 (3) 
Advertisements
Numerical divider function

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s