# return simplified tuple of floats

def simplify(n, d):

n, d = map(float, [round(n, 6), d])

if n:

a, b = n, d

while a:

a, b = b%a, a

return n/b, d/b

else:

return n, d

 

if __name__ == '__main__':

print simplify(0.25, 2)

print simplify(1.25, 2)

print simplify(2.25, 2)

print simplify(0.9999999, 2)

print simplify(0.0000001, 2)

print simplify(122, 256)

print simplify(1024, 6656)

 

# Sample Output

>>> (1.0, 8.0)

(5.0, 8.0)

(9.0, 8.0)

(1.0, 2.0)

(0.0, 2.0)

(61.0, 128.0)

(2.0, 13.0)