I discovered some strange behavior of the python modulo operation. The command
a = 1.0 % 0.1
yields
a == 0.09999999999999995
which is quite a big error that causes me problems when calculating the greatest common divisor of two float numbers. I suppose the error is related to the non-representatilty of 0.1 and 1.0 in binary (http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems). Does a workaround for this problem exist or can someone point me at a reliably function to compute the gcd of two numbers? The code that I have been using so far for computing the gcd is
def gcd(a, b):
a, b = np.broadcast_arrays(a, b)
a = a.copy()
b = b.copy()
pos = np.nonzero(b)[0]
while len(pos) > 0:
b2 = b[pos]
a[pos], b[pos] = b2, a[pos] % b2
pos = pos[b[pos]!=0]
return a
I have the same problem when I use the fractions module.