Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

This isn't a sagetex-related issue: it's because randint returns a Python int:

sage: c = randint(14, 20)
sage: d = randint(5, 10)
sage: c
17
sage: d
7
sage: type(c), type(d)
(<type 'int'>, <type 'int'>)

and division of Python ints is truncating:

sage: c / d
2

The reason that the second one behaves is because the 3 and 1 in -3 and +1 are Sage Integers, and operations involving them and Python ints produce Sage objects (Integers, Rationals, etc), so things work the way they should:

sage: (c-3)/(d+1)
7/4
sage: type(_)
<type 'sage.rings.rational.Rational'>

If you wrap the randint call in Integer, it should behave, although I can't do a sagetex test at the moment:

sage: c = Integer(randint(14, 20))
sage: c
15
sage: type(c), parent(c)
(<type 'sage.rings.integer.Integer'>, Integer Ring)

This isn't a sagetex-related issue: it's because randint returns a Python int:

sage: c = randint(14, 20)
sage: d = randint(5, 10)
sage: c
17
sage: d
7
sage: type(c), type(d)
(<type 'int'>, <type 'int'>)

and division of Python ints is truncating:

sage: c / d
2

The reason that the second one behaves is because the 3 and 1 in -3c-3 and +1d+1 are Sage Integers, and operations involving them and Python ints produce Sage objects (Integers, Rationals, etc), so things work the way they should:

sage: (c-3)/(d+1)
7/4
sage: type(_)
<type 'sage.rings.rational.Rational'>

If you wrap the randint call in Integer, it should behave, although I can't do a sagetex test at the moment:

sage: c = Integer(randint(14, 20))
sage: c
15
sage: type(c), parent(c)
(<type 'sage.rings.integer.Integer'>, Integer Ring)