sage: 3%(-1) 0 sage: Rational(3)%Rational(-1) ---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last) .... /usr/local/src/sage-4.1.1/local/lib/python2.6/site-packages/sage/rings/ rational.so in sage.rings.rational.Rational.__mod__ (sage/rings/ rational.c:17130)() .... ZeroDivisionError: Inverse does not exist. --------------------------------------------------------------------------- Line 17130 comes from rational.pyx: 2277 def __mod__(Rational self, other): .... 2295 other = integer.Integer(other) 2296 if not other: 2297 raise ZeroDivisionError, "Rational modulo by zero" 2298 n = self.numer() % other 2299 d = self.denom() % other 2300 _sig_on 2301 d = d.inverse_mod(other) 2302 _sig_off 2303 return (n*d)%other When self.denom() is divisible by other, d is 0 and that causes the error. So why does the following work? sage: Rational(3)%Rational(1) 0 Let's look at integer.pyx: 4705 if mpz_cmp_ui(m.value, 1) == 0: 4706 return zero Thus d.inverse_mod(1) will return 0. However, this contradicts the docstring, which states: - ``x`` - Integer such that x*self = 1 (mod m), or raises ZeroDivisionError. It is not true that 0*d = 1 (mod 1). Lines 4705 and 4706 of integer.pyx look like a kludge that in some situations, such as calculating Rational(3)%Rational(1), makes a wrong algorithm appear to be correct. Why not replace lines range(2298,2304) of rational.pyx by the natural n = floor(self) return n%d + (self-n) In fact, this code is applicable to anything that has the attribute floor() (except SR) and could appear at a higher level. Dirk --~--~---------~--~----~------------~-------~--~----~ To post to this group, send an email to sage-devel@googlegroups.com To unsubscribe from this group, send an email to sage-devel-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-devel URL: http://www.sagemath.org -~----------~----~----~----~------~----~------~--~---