In some circumstances, "Stein's trick" may help [1]:

class myint:
    def eval(self, s, globals, locals):
        _temp = locals['Integer']
        locals['Integer'] = float
        ans = python.eval(preparse(s),  globals, locals)
        locals['Integer'] = _temp
        return ans

Examples, in separate cells:

%myint()
print type(45)
print 500 / 1000, 1000 / 1000
print 500 / 1000., 1000 / 1000.

    <type 'float'>
    0.5 1.0
    0.500000000000000 1.00000000000000

print type(45)
print 500 / 1000, 1000 / 1000
print 500 / 1000., 1000 / 1000.

    <type 'sage.rings.integer.Integer'>
    1/2 1
    0.500000000000000 1.00000000000000

Modular arithmetic, Python 'int', and matplotlib.pyplot examples at [2].
 I don't why the extra point at the origin shows up.

[1] https://groups.google.com/group/sage-devel/msg/e53caae140cef7df
[2] http://www.sagenb.org/home/pub/563/


Robert Bradshaw wrote:
> On May 22, 2009, at 2:00 PM, Nick Alexander wrote:
> 
>>> We do this to avoid the (large) overhead of re-creating constants in
>>> the bodies of loops, functions, etc. Perhaps we need to detect the
>>> Integer=xxx line explicitly?
>> Doing this accurately is equivalent to the halting problem.
> 
> Yep. Detecting an assignment to Integer (RealNumber, ...) should be  
> sufficient to eliminate most headaches, especially as this simple  
> assignment is what we tell users to do.
> 
>> This is what pragmas are for, but do we really want to start  
>> supporting all
>> these compiler features?  When is sage no longer python?
> 
> A long time ago, e.g. when we started using Integer(...) to wrap  
> integer literals. Despite my work on the preparser, I'm all for  
> keeping it minimal.
> 
> - Robert
> 
> 
> > 

--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to sage-devel@googlegroups.com
To unsubscribe from this group, send email to 
sage-devel-unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-devel
URLs: http://www.sagemath.org
-~----------~----~----~----~------~----~------~--~---

Reply via email to