ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 17 Dec 2011 08:07:01 -0600[Solved] Difference about divide operator in python2 and sagehttps://ask.sagemath.org/question/8574/solved-difference-about-divide-operator-in-python2-and-sage/I am writing a package (I want to use "import" to import it into sage).
I found that in the package (say, test.py), if I write
from sage.all import *
def test():
var('a')
print -1/2, -a/2, (-1/2)*a
and in sage type
import test; test.test()
I got "-1 -1/2*a -a"
This is different from directly run the above commands from sage.
I know in python 2, -1/2 returns -1. But is there a way to write "minus one half" (-1/2) in a package, say, test.py?
I know I could use load or attach to load a .sage package. However, they don't have namespace and I hope to have namespace as in python.
I could also use 1/2.0, which returns 0.5. But I would want an exact number instead of a float point approximation, for further use.
To summarize, in this case, how to write -1/2 in test.py? Or is there a way to load a sage package with namespace? Thanks!Sat, 17 Dec 2011 08:00:38 -0600https://ask.sagemath.org/question/8574/solved-difference-about-divide-operator-in-python2-and-sage/Answer by tririver for <p>I am writing a package (I want to use "import" to import it into sage).</p>
<p>I found that in the package (say, test.py), if I write</p>
<pre><code>from sage.all import *
def test():
var('a')
print -1/2, -a/2, (-1/2)*a
</code></pre>
<p>and in sage type</p>
<p>import test; test.test()</p>
<p>I got "-1 -1/2*a -a"</p>
<p>This is different from directly run the above commands from sage.</p>
<p>I know in python 2, -1/2 returns -1. But is there a way to write "minus one half" (-1/2) in a package, say, test.py?</p>
<p>I know I could use load or attach to load a .sage package. However, they don't have namespace and I hope to have namespace as in python.</p>
<p>I could also use 1/2.0, which returns 0.5. But I would want an exact number instead of a float point approximation, for further use.</p>
<p>To summarize, in this case, how to write -1/2 in test.py? Or is there a way to load a sage package with namespace? Thanks!</p>
https://ask.sagemath.org/question/8574/solved-difference-about-divide-operator-in-python2-and-sage/?answer=13035#post-id-13035(Let me answer myself instead of deleting the question, for other people to refer to:)
Use
from sage.all import *
def test():
var('a')
print -1/Integer(2)
Integer(number) is a sage object, thus don't have the python 2 feature.
Sat, 17 Dec 2011 08:07:01 -0600https://ask.sagemath.org/question/8574/solved-difference-about-divide-operator-in-python2-and-sage/?answer=13035#post-id-13035