Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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.

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.