[New-bugs-announce] [issue21712] fractions.gcd failure

Pablo Acosta report at bugs.python.org
Wed Jun 11 03:23:56 CEST 2014


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.

Example:

>> import fractions
>> fractions.gcd(48, 18)
>> 6

Which is corret, but:

>> 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:

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>
_______________________________________


More information about the New-bugs-announce mailing list