| 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 ---------- ------ end of sagess.py ---------- When I launche that script from the command line, I got what Python would give : But when I launch it from the sage'terminal it is less clear how it works : 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 |
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 On the other hand, to get 1/2, use Sage integers: rewrite your file as This will give the right answer with integer arguments, but it will raise an error if you pass non-integer arguments. |
| Ok, so the point is that when making an "interactive" import, the imported module is not preparsed ? 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 : This works fine. Have a good night Laurent |
Asked: Feb 01 '11
Seen: 2,278 times
Last updated: Feb 01 '11
powered by ASKBOT version 0.7.22
Copyright Sage, 2010. Some rights reserved under creative commons license.