Decimals -> Fraction strings, my solution
Peter Schneider-Kamp
petersc at stud.ntnu.no
Mon May 22 06:18:26 EDT 2000
"David C. Ullrich" wrote:
>
> Why does Python's implementation of modulo make
> this a good idea? Is there a problem with the "original"
>
> def gcd(a, b):
> while a:
> a, b = b % a, a
> return b
>
> that I've just never found?
Hei David!
Actually the only problem I see is that the outcome is
negative. return abs(b) would of course also solve that.
Additionally I wrote "while a > 0:" which of course
makes problems for negative numbers... :-7
> The reason the counter-quibble seems worth
> mentioning is that the abs makes it much less generic:
> Without the abs you can use the function for any
> objects with a __mod__ method (assuming that a has
> to eventually become false for some reason). For
> example you can use it to find the gcd of two
> polynomials a and b, if you give your polynomials
> an appropriate __mod__. (I don't think there's any
> such thing as an appropriate __abs__ for polynomials...)
Good point. As all the important number theoretical
relations still hold there is no problem with that.
sometimes-missing-something-obious-ly y'rs
Peter
--
Peter Schneider-Kamp ++47-7388-7331
Herman Krags veg 51-11 mailto:peter at schneider-kamp.de
N-7050 Trondheim http://schneider-kamp.de
More information about the Python-list
mailing list