I've written this code to calculate the continued fraction expansion of a rational number N using the Euclidean algorithm:
from __future__ import division
def contFract(N):
while True:
yield N//1
f = N - (N//1)
if f == 0:
break
N = 1/f
If say N is 3.245 the function is never ending as apparently f never equals 0. The first 10 terms of the expansion are:
[3.0, 4.0, 12.0, 3.0, 1.0, 247777268231.0, 4.0, 1.0, 2.0, 1.0]
Which is clearly an error since the actual expansion is only:
[3;4,12,3,1] or [3;4,12,4]
What's causing the problem here? Is it some sort of rounding error?