GCD in Fractions

blindanagram noone at nowhere.net
Tue Sep 23 14:48:22 CEST 2014


On 23/09/2014 12:53, Wolfgang Maier wrote:
> On 09/23/2014 10:16 AM, blindanagram wrote:
>> What is the rationale for gcd(x, y) in Fractions returning a negative
>> value when y is negtive?
>>
> 
> I guess it is implemented this way because its main use is in the
> Fraction constructor.

This is not guaranteed once it is exposed as a function that is
available to all Python users.

>> For example gcd(3, -7) returns -1, 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.

[snip]
> Given that the implementation is particularly useful for Fraction() it
> is debatable whether the function shouldn't be called _gcd instead of
> gcd, but otherwise it makes sense.

As you imply, it would make sense if it hadn't been exposed for more
general use.

However, I am less sure that it is sensible to have a gcd function
exposed for end user use when it conflicts with the function's usual
definition.




More information about the Python-list mailing list