ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 11 Apr 2016 23:08:04 +0200Possible bug of sum functionhttps://ask.sagemath.org/question/33068/possible-bug-of-sum-function/Consider the following code:
def s(n):
return sum(k/n for k in range(n + 1))
L0 = s(2)
for n in range(2, 3):
L1 = s(n)
The answers are (using sage 7.1):
print L0, L1
(3/2, 1)
In the second case sage is using the usual integer division of python 2.x while in the first one it is using the exact representation of rationals. Is this a known bug?
Mon, 11 Apr 2016 19:15:50 +0200https://ask.sagemath.org/question/33068/possible-bug-of-sum-function/Answer by tmonteil for <p>Consider the following code:</p>
<pre><code>def s(n):
return sum(k/n for k in range(n + 1))
L0 = s(2)
for n in range(2, 3):
L1 = s(n)
</code></pre>
<p>The answers are (using sage 7.1):</p>
<pre><code>print L0, L1
(3/2, 1)
</code></pre>
<p>In the second case sage is using the usual integer division of python 2.x while in the first one it is using the exact representation of rationals. Is this a known bug? </p>
https://ask.sagemath.org/question/33068/possible-bug-of-sum-function/?answer=33069#post-id-33069It is not a bug. When you write `s(2)`, Sage preparse the `2` so that it is the `Sage Integer` 2, not the `Python int` 2. With such Sage integers, the division leads to a `Sage Rational`. Now, when you use the `range` function, it produces Python ints, which explains your problem. If you want to generate a list of Sage Integers, you should use the `srange` function instead of `range`.
Mon, 11 Apr 2016 20:32:15 +0200https://ask.sagemath.org/question/33068/possible-bug-of-sum-function/?answer=33069#post-id-33069Comment by jllb for <p>It is not a bug. When you write <code>s(2)</code>, Sage preparse the <code>2</code> so that it is the <code>Sage Integer</code> 2, not the <code>Python int</code> 2. With such Sage integers, the division leads to a <code>Sage Rational</code>. Now, when you use the <code>range</code> function, it produces Python ints, which explains your problem. If you want to generate a list of Sage Integers, you should use the <code>srange</code> function instead of <code>range</code>.</p>
https://ask.sagemath.org/question/33068/possible-bug-of-sum-function/?comment=33070#post-id-33070Thank you, however I find this behavior at least strange. If n is a Sage Integer, I understand range(n) produces a list of Python integers. So in both cases the sum runs over a list of Python integers. However, in one case they are converted into Sage integers and in the other case they aren't. I don't understand why.Mon, 11 Apr 2016 22:28:21 +0200https://ask.sagemath.org/question/33068/possible-bug-of-sum-function/?comment=33070#post-id-33070Comment by nbruin for <p>It is not a bug. When you write <code>s(2)</code>, Sage preparse the <code>2</code> so that it is the <code>Sage Integer</code> 2, not the <code>Python int</code> 2. With such Sage integers, the division leads to a <code>Sage Rational</code>. Now, when you use the <code>range</code> function, it produces Python ints, which explains your problem. If you want to generate a list of Sage Integers, you should use the <code>srange</code> function instead of <code>range</code>.</p>
https://ask.sagemath.org/question/33068/possible-bug-of-sum-function/?comment=33072#post-id-33072In your call `L0=s(2)`, `s` gets called with a Sage integer, and dividing a sage integer by a python integer is defined by converting the python integer to a sage integer and then doing the division
In your second example, `n in range(2,3)`, we have that `n` is a python integer, so when `s` executed, two python integers are divided. We never get a chance to adjust the result. It's indeed strange and confusing, but we can't do anything about it unless we heavily patch python. If we ever change to Python3, it will be slightly better (you'll get a float rather than a python integer back)Mon, 11 Apr 2016 23:08:04 +0200https://ask.sagemath.org/question/33068/possible-bug-of-sum-function/?comment=33072#post-id-33072