Pablo Acosta
2014-06-11 01:23:56 UTC
New submission from Pablo Acosta:
The Greatest Common Divisor (gcd) algorithm sometimes breaks because the modulo operation does not always return a strict integer number, but one very, very close to one. This is enough to drive the algorithm astray from that point on.
while b:
a, b = b, int(a%b)
return a
----------
components: Library (Lib)
messages: 220221
nosy: pacosta
priority: normal
severity: normal
status: open
title: fractions.gcd failure
type: behavior
versions: Python 2.7, Python 3.1, Python 3.2, Python 3.3, Python 3.4, Python 3.5
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue21712>
_______________________________________
The Greatest Common Divisor (gcd) algorithm sometimes breaks because the modulo operation does not always return a strict integer number, but one very, very close to one. This is enough to drive the algorithm astray from that point on.
import fractions
fractions.gcd(48, 18)
6
fractions.gcd(2.7, 107.3)
8.881784197001252e-16
when the answer should be 1. The fix seems simple, just cast the mod() operation in the algorithm:fractions.gcd(48, 18)
6
fractions.gcd(2.7, 107.3)
8.881784197001252e-16
while b:
a, b = b, int(a%b)
return a
----------
components: Library (Lib)
messages: 220221
nosy: pacosta
priority: normal
severity: normal
status: open
title: fractions.gcd failure
type: behavior
versions: Python 2.7, Python 3.1, Python 3.2, Python 3.3, Python 3.4, Python 3.5
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue21712>
_______________________________________