Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

You've hit a well-known snag (more precisely a pair of well-known snags...).

> I know that 3รท2=1.5,

Mathematically, yes. But there's more to this in Sage:

First snag : Sage preparses your input and (silently) converts them in terms of Sage objects. For example:

sage: a=3
sage: type(a)
<type 'sage.rings.integer.Integer'>
sage: a.parent()
Integer Ring

In this case, Sage's preparser has read the string "3" as an element of your inut, concluded that it was a numerical integer constant and created a Sage object, element of the Integer ring (i. e $\left({\mathbb Z},\,+,\, .\right)$). The (Python) type of this object is the python representation of a class representing (some) sje objects, and the parent of this object is the Sage representation of this class.

These are different from Python integers which you can get by explicit conversion:

sage: pa=int(3)
sage: type(pa)
<type 'int'>

Such objects do not belong in the hierarchy of Sage objects:

sage: pa.parent()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-11-7df6d5e7f9a4> in <module>()
----> 1 pa.parent()

AttributeError: 'int' object has no attribute 'parent'

The same is true of the preparsing of fractions:

> but how do you write 1.5 in fraction form.

Quite simply:

sage: b=3/2
sage: b
3/2
sage: type(b)
<type 'sage.rings.rational.Rational'>
sage: b.parent()
Rational Field

The latter is the Sage representation of the $\left({\mathbb Q},\,+,\, .\right)$ ring. And Sage's implementation of / for the class Integer somehow manages to compute the quotient of two integers in the Rational field (which is, of course, also a ring). In fact, it's a bit more complicated than that, for technical reasons, but this explanation is in essence correct.

Below the system of Sage classes and methods and their interrelation lies the notion of (mathematical) categories, which justifies this implementation. But this is a large subject, which, IIUC, is usually treated at postgrad level (at least un the US, Europeans may do things a bit differently...).

See the excellent book Computational Mathematics with SageMath for a (much better) introduction...

Of course, you can attempt the division of two Python integers. Which (here's the second snag) results in:

  • a (Python) integer if you're working in a Python 2-based Sage (current standard), where the division is the integer division :

Example

sage: int(3)/int(2)
1
sage: type(int(3)/int(2))
<type 'int'>
  • a (Python) float if you're working in a Python 3-based Sage (soon-to-be standard), where the division is the float division:

Example

sage: type(int(3)/int(2))
<class 'float'>

(One may note that Python 2 objects have a type, while Python 3 objects have a class ; neither have a parent; though...).

This behaviour results from a change of heart by Guido van Rossum between Python 2 and Python 3...