GCD in Fractions

Terry Reedy tjreedy at udel.edu
Tue Sep 23 21:31:39 CEST 2014


On 9/23/2014 4:16 AM, blindanagram wrote:
> What is the rationale for gcd(x, y) in Fractions returning a negative
> value when y is negtive?

> For example gcd(3, -7) returns -1,

Since the doc says "the result will have the same sign as b", this is 
intentinal.  However, I consider this a *design* bug.  Both -1 and +1 
are common divisors, and 1 is the greatest.  So I consider the previous 
line "Calculate the Greatest Common Divisor of a and b" to be incorrect.

> which means that a co-prime test that
> would work in many other languages 'if gcd(x, y) == 1' will fail in
> Python for negative y.
>
> And, of course, since -|x| is less than |x|, returning -|x| rather than
> |x| is not returning the greatest common divisor of x and y when y is
> negative.
>


-- 
Terry Jan Reedy




More information about the Python-list mailing list