I am looking for an efficient way to determine the greatest common divisor of two floats with python. The routine should have the following layout
gcd(a, b, rtol=1e-05, atol=1e-08)
"""
Returns the greatest common divisor of a and b
Parameters
----------
a,b : float
two floats for gcd
rtol, atol : float, optional
relative and absolute tolerance
Returns
-------
gcd : float
Greatest common divisor such that for x in [a,b]:
np.mod(x,gcd) < rtol*x + atol
.. _PEP 484:
https://www.python.org/dev/peps/pep-0484/
"""
Example: gcd of rational and irrational number
The gcd(1., np.pi, rtol=0, atol=1e-5)
should return (roughly) 1e-5
, as
In [1]: np.mod(np.pi,1e-5)
Out[1]: 2.6535897928590063e-06
In [2]: np.mod(1.,1e-5)
Out[2]: 9.9999999999181978e-06
I would prefer to use a library implementation and not to write it myself. The fractions.gcd function does not seem appropriate to me here, as I do not want to work with fractions and it (obviously) does not have the tolerance parameters.