Ask Your Question

Revision history [back]

When does 1/2=0 ? (python's integer division Vs Sage's 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 ...

------ ----------

#! /usr/bin/sage -python
# -*- coding: utf8 -*-

from sage.all import *

def MyFunction(x,y):
    print x/y


------ end of ----------

When I launche that script from the command line, I got what Python would give :

18:08:03 ~/script >$ ./

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
sage: sagess.MyFunction(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