Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'negative.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; '-1,': 0.16; 'constructor.': 0.16; 'rationale': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'wrote:': 0.18; 'passing': 0.19; '>>>': 0.22; 'example': 0.22; 'header:User-Agent:1': 0.23; 'fraction': 0.24; "shouldn't": 0.24; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'am,': 0.29; 'received:132': 0.31; 'languages': 0.32; 'guess': 0.33; 'implemented': 0.33; 'common': 0.35; 'test': 0.35; 'but': 0.35; 'returning': 0.36; 'useful': 0.36; 'to:addr :python-list': 0.38; 'rather': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'called': 0.40; 'greatest': 0.60; 'negative': 0.60; "'if": 0.84; 'hand,': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Wolfgang Maier Subject: Re: GCD in Fractions Date: Tue, 23 Sep 2014 13:53:49 +0200 References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: px1.ruf.uni-freiburg.de User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.1.1 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 34 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1411473203 news.xs4all.nl 2948 [2001:888:2000:d::a6]:56034 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:78206 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. > 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. > On the other hand, it allows: >>> Fraction(3, -7) Fraction(-3, 7) and: >>> Fraction(3, -7) == Fraction(-3, 7) True 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. > 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. > however, you can always use abs on y before passing it to gcd. Wolfgang