ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 01 Feb 2011 06:22:57 -0600When does 1/2=0 ? (python's integer division Vs Sage's exact fractions)http://ask.sagemath.org/question/7917/when-does-120-pythons-integer-division-vs-sages-exact-fractions/Hi all !
I know that python evaluates 1/2 to zero because it is making an integer division. Well. I was expecting Sage not to do so.
It does more or less ...
------ sagess.py ----------
#! /usr/bin/sage -python
# -*- coding: utf8 -*-
from sage.all import *
def MyFunction(x,y):
print x/y
MyFunction(1,2)
------ end of sagess.py ----------
When I launche that script from the command line, I got what Python would give :
18:08:03 ~/script >$ ./sagess.py
0
But when I launch it from the sage'terminal it is less clear how it works :
18:16:04 ~/script >$ sage
----------------------------------------------------------------------
| Sage Version 4.5.3, Release Date: 2010-09-04 |
| Type notebook() for the GUI, and license() for information. |
----------------------------------------------------------------------
sage: import sagess
0
sage: sagess.MyFunction(1,2)
1/2
At the import, it returns zero, but when I reask the same computation, it gives the correct 1/2
I know that writing
float(x)/y
"fixes" the problem. But it looks weird and I want to keep exact values.
What do I have to write in my scripts in order to make understand to Sage that I always want 1/2 to be 1/2 ?
Thanks
Have a good night !
Laurent
Tue, 01 Feb 2011 05:24:55 -0600http://ask.sagemath.org/question/7917/when-does-120-pythons-integer-division-vs-sages-exact-fractions/Answer by John Palmieri for <p>Hi all !</p>
<p>I know that python evaluates 1/2 to zero because it is making an integer division. Well. I was expecting Sage not to do so.</p>
<p>It does more or less ...</p>
<p>------ sagess.py ----------</p>
<pre><code>#! /usr/bin/sage -python
# -*- coding: utf8 -*-
from sage.all import *
def MyFunction(x,y):
print x/y
MyFunction(1,2)
</code></pre>
<p>------ end of sagess.py ----------</p>
<p>When I launche that script from the command line, I got what Python would give :</p>
<pre><code>18:08:03 ~/script >$ ./sagess.py
0
</code></pre>
<p>But when I launch it from the sage'terminal it is less clear how it works :</p>
<pre><code>18:16:04 ~/script >$ sage
----------------------------------------------------------------------
| Sage Version 4.5.3, Release Date: 2010-09-04 |
| Type notebook() for the GUI, and license() for information. |
----------------------------------------------------------------------
sage: import sagess
0
sage: sagess.MyFunction(1,2)
1/2
</code></pre>
<p>At the import, it returns zero, but when I reask the same computation, it gives the correct 1/2</p>
<p>I know that writing
float(x)/y
"fixes" the problem. But it looks weird and I want to keep exact values. </p>
<p>What do I have to write in my scripts in order to make understand to Sage that I always want 1/2 to be 1/2 ?</p>
<p>Thanks
Have a good night !
Laurent</p>
http://ask.sagemath.org/question/7917/when-does-120-pythons-integer-division-vs-sages-exact-fractions/?answer=12053#post-id-12053 sage: import sagess
0
sage: sagess.MyFunction(1,2)
1/2
The reason for the above behavior is that in the second call, the arguments 1 and 2 are Sage Integers, not Python ints: when run interactively, Sage preparses the input, turning all integers into the type `Integer`. You could reproduce the effect of your original call of `MyFunction(1,2)` in sagess.py by this:
sage: sagess.MyFunction(int(1), int(2))
On the other hand, to get 1/2, use Sage integers: rewrite your file as
#! /usr/bin/sage -python
# -*- coding: utf8 -*-
def MyFunction(x,y):
from sage.rings.all import Integer
print Integer(x)/Integer(y)
MyFunction(1,2)
This will give the right answer with integer arguments, but it will raise an error if you pass non-integer arguments.Tue, 01 Feb 2011 05:41:06 -0600http://ask.sagemath.org/question/7917/when-does-120-pythons-integer-division-vs-sages-exact-fractions/?answer=12053#post-id-12053Answer by Laurent Claessens for <p>Hi all !</p>
<p>I know that python evaluates 1/2 to zero because it is making an integer division. Well. I was expecting Sage not to do so.</p>
<p>It does more or less ...</p>
<p>------ sagess.py ----------</p>
<pre><code>#! /usr/bin/sage -python
# -*- coding: utf8 -*-
from sage.all import *
def MyFunction(x,y):
print x/y
MyFunction(1,2)
</code></pre>
<p>------ end of sagess.py ----------</p>
<p>When I launche that script from the command line, I got what Python would give :</p>
<pre><code>18:08:03 ~/script >$ ./sagess.py
0
</code></pre>
<p>But when I launch it from the sage'terminal it is less clear how it works :</p>
<pre><code>18:16:04 ~/script >$ sage
----------------------------------------------------------------------
| Sage Version 4.5.3, Release Date: 2010-09-04 |
| Type notebook() for the GUI, and license() for information. |
----------------------------------------------------------------------
sage: import sagess
0
sage: sagess.MyFunction(1,2)
1/2
</code></pre>
<p>At the import, it returns zero, but when I reask the same computation, it gives the correct 1/2</p>
<p>I know that writing
float(x)/y
"fixes" the problem. But it looks weird and I want to keep exact values. </p>
<p>What do I have to write in my scripts in order to make understand to Sage that I always want 1/2 to be 1/2 ?</p>
<p>Thanks
Have a good night !
Laurent</p>
http://ask.sagemath.org/question/7917/when-does-120-pythons-integer-division-vs-sages-exact-fractions/?answer=12054#post-id-12054Ok, so the point is that when making an "interactive" import, the imported module is not preparsed ?
def MyFunction(x,y):
from sage.rings.all import Integer
print Integer(x)/Integer(y)
I cannot do that in my real live idea. My purpose was to write a function that convert a point (x,y) into polar coordinates (radius,angle) using atan(y/x).
(the aim was to show the algorithm to some students)
Instead, you gave me the idea to write this one :
def MyFunction(x,y):
x=SR(x)
print x/y
This works fine.
Have a good night
Laurent
Tue, 01 Feb 2011 06:22:57 -0600http://ask.sagemath.org/question/7917/when-does-120-pythons-integer-division-vs-sages-exact-fractions/?answer=12054#post-id-12054