1.5 as a fraction
I know that 3÷2=1.5, but how do you write 1.5 in fraction form.
I know that 3÷2=1.5, but how do you write 1.5 in fraction form.
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 int
egers 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:
Example
sage: int(3)/int(2)
1
sage: type(int(3)/int(2))
<type 'int'>
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...
Like this:
sage: QQ(1.5)
3/2
because QQ
stands for the:
sage: QQ
Rational Field
Please start posting anonymously - your entry will be published after you log in or create a new account.
Asked: 2019-10-29 01:48:01 +0100
Seen: 1,839 times
Last updated: Nov 04 '19
finding rational points on an elliptic curve
Cast expression to rational number
Enumerating lattice points in rational polytopes
How do I save a rational matrix for Mathematica and Matlab to read?
Solving system of polynomial equations over rationals
How to import rings in python module
Degree of a rational map and the corresponding map between function fields