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.Sun, 27 Mar 2016 12:57:40 +0200range and division : unexpected behaviorhttps://ask.sagemath.org/question/32891/range-and-division-unexpected-behavior/Consider the following snippet :
N=5
# code 1
for n in range(N,N+1):
for k in range(0,n):
print k/n
print '-'*10
# code 2
n=N
for k in range(0,n):
print k/n
I was expecting code 1 and code 2 to print the same output. This is not the case :
0
0
0
0
0
----------
0
1/5
2/5
3/5
4/5
In the first case, `k/n` is Python-evaluated as an integer division, in the second case, `k/n` is Sage-evaluated as a fraction. Can someone elaborate please ?
I only notice that substituting `srange(N,N+1)` to `range(N,N+1)` fixes the problem.
Sun, 27 Mar 2016 11:28:01 +0200https://ask.sagemath.org/question/32891/range-and-division-unexpected-behavior/Answer by ndomes for <p>Consider the following snippet :</p>
<pre><code>N=5
# code 1
for n in range(N,N+1):
for k in range(0,n):
print k/n
print '-'*10
# code 2
n=N
for k in range(0,n):
print k/n
</code></pre>
<p>I was expecting code 1 and code 2 to print the same output. This is not the case :</p>
<pre><code>0
0
0
0
0
----------
0
1/5
2/5
3/5
4/5
</code></pre>
<p>In the first case, <code>k/n</code> is Python-evaluated as an integer division, in the second case, <code>k/n</code> is Sage-evaluated as a fraction. Can someone elaborate please ?</p>
<p>I only notice that substituting <code>srange(N,N+1)</code> to <code>range(N,N+1)</code> fixes the problem.</p>
https://ask.sagemath.org/question/32891/range-and-division-unexpected-behavior/?answer=32892#post-id-32892There is a difference between Sage and Python (2.x) concerning the division operator **/** .
In Python 2.x the operator **/** returns the floor of the result of division if the operands are integers (Python ints).
In Sage the operator **/** returns the result as rational number (if so) if the operands are **Sage** integers.
The Python function `range` returns a list of Python integers. In your nested for loops all operands are Python ints, so you get the floor of the division result.
In your second code example **N** (and therefor **n**) is an Sage integer, so you get the rational numbers as result.
The Sage function `srange` returns a list of Sage integers. With `srange(N,N+1)` in your first code **n** becomes a Sage integer.
You can see the difference by placing some `print type(k)` and `print type(n)` commands inside the loops.Sun, 27 Mar 2016 12:33:33 +0200https://ask.sagemath.org/question/32891/range-and-division-unexpected-behavior/?answer=32892#post-id-32892Comment by candide for <p>There is a difference between Sage and Python (2.x) concerning the division operator <strong>/</strong> .
In Python 2.x the operator <strong>/</strong> returns the floor of the result of division if the operands are integers (Python ints).
In Sage the operator <strong>/</strong> returns the result as rational number (if so) if the operands are <strong>Sage</strong> integers.
The Python function <code>range</code> returns a list of Python integers. In your nested for loops all operands are Python ints, so you get the floor of the division result.
In your second code example <strong>N</strong> (and therefor <strong>n</strong>) is an Sage integer, so you get the rational numbers as result.</p>
<p>The Sage function <code>srange</code> returns a list of Sage integers. With <code>srange(N,N+1)</code> in your first code <strong>n</strong> becomes a Sage integer.</p>
<p>You can see the difference by placing some <code>print type(k)</code> and <code>print type(n)</code> commands inside the loops.</p>
https://ask.sagemath.org/question/32891/range-and-division-unexpected-behavior/?comment=32893#post-id-32893Thanks, now the benefits of using `srange` is more apparent.Sun, 27 Mar 2016 12:57:40 +0200https://ask.sagemath.org/question/32891/range-and-division-unexpected-behavior/?comment=32893#post-id-32893