Ask Your Question

1.5 as a fraction

asked 2019-10-29 01:48:01 +0100

Cool gravatar image

updated 2022-01-21 21:33:44 +0100

FrédéricC gravatar image

I know that 3÷2=1.5, but how do you write 1.5 in fraction form.

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted

answered 2019-10-30 15:37:07 +0100

Emmanuel Charpentier gravatar image

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
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 :


sage: int(3)/int(2)
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:


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...

edit flag offensive delete link more

answered 2019-11-04 23:05:06 +0100

Sébastien gravatar image

Like this:

sage: QQ(1.5)

because QQ stands for the:

sage: QQ
Rational Field
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2019-10-29 01:48:01 +0100

Seen: 1,546 times

Last updated: Nov 04 '19