0

I am not very familiar with Sage so maybe it is obvious.

Input to Sage:

i=3;
j=2;
print(i/j);
for i in range(3,4):
      for j in range(2,3):
            print(i/j);

Output from Sage:

3/2
1

First output is as expected $3/2$, but why the second output is not also $3/2$ but it is rounded to $1$?

I want to loop over rationals - over numerators and over denominators, but the rounding makes it impossible.

EDIT: Solved using srange instead of range.

Input to Sage:

i=3;
j=2;
print(i/j);
for i in srange(3,4):
      for j in srange(2,3):
            print(i/j);

Output from Sage:

3/2
3/2
azerbajdzan
  • 1,166
  • 1
    Looks like a bug to me, but I could be wrong. That is, incidentally, valid Python code as well, which yields 1 and 1. You can force floating-point if you throw in something like print(i*1.0/j). One guess is that the output of the range function is not the same type, precisely, as the result of i=3;. – Adrian Keister Nov 19 '19 at 22:20
  • @Adrian Keister: I solved the problem - see edit of my question. And, yes, it was problem with the type of variables $i$ and $j$ inside and outside of the loops. – azerbajdzan Nov 19 '19 at 23:45
  • 1
    To be explicit, range gives Python ints which divide to zero in Python 2; in Py3 they will give different output so watch out in the future! – kcrisman Nov 26 '19 at 14:17
  • By the way, you don't need semicolons for Sage/Python. Glad you figured it out on your own, nice work! – kcrisman Nov 26 '19 at 14:17

0 Answers0