range() is a list of integers. In a Python file, (-1)**k and factorial(k) are Python ints, so the quotient is C division.
On the Sage command line, -1 is preparsed to a Sage integer. So (-1)**k is a Sage integer and factorial(k) is a Python int, and coercion computes the quotient in QQ. On Sunday, December 28, 2014 11:27:45 AM UTC+1, Ralf Stephan wrote: > > Hello, > while working on a ticket I encountered different behaviour of python sum > in Sage regarding whether the sum (over a symbolic expression applied to a > range) > was given on the command line vs. in Sage-lib (python file) within a > function definition. > > Specifically, applying this one-line patch on top of develop: > diff --git a/src/sage/rings/arith.py b/src/sage/rings/arith.py > --- a/src/sage/rings/arith.py > > +++ b/src/sage/rings/arith.py > @@ -5336,6 +5336,7 @@ def subfactorial(n): > > - Jaap Spies (2007-01-23) > """ > + from sage.functions.other import factorial > return factorial(n)*sum(((-1)**k)/factorial(k) for k in range(n+1)) > > will show on the command line: > sage: subfactorial(8) > -120960 > which is wrong but when I define the same function there: > sage: from sage.functions.other import factorial > sage: def subf(n): > return factorial(n)*sum(((-1)**k)/factorial(k) for k in range(n+1 > )) > ....: > sage: subf(8) > 14833 > > This effect only happens with Python sum/prod. So, what difference does > using Python sum/prod on the command line make vs. in a py file? > > Please help, > > > -- You received this message because you are subscribed to the Google Groups "sage-devel" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/sage-devel. For more options, visit https://groups.google.com/d/optout.
