On 22 May 2009, at 16:56, Franco Saliola wrote: > On Fri, May 22, 2009 at 10:42 PM, Kevin Horton > <khorto...@rogers.com> wrote: >> >> On 22 May 2009, at 16:07, Robert Bradshaw wrote: >> >>> On May 22, 2009, at 3:36 AM, Kevin Horton wrote: >>> >>>> I am using sage-4.0.rc0 on 32 bit ubuntu 8.10. >>>> >>>> I have discovered a very strange interaction between the >>>> "Integer=int" >>>> declaration, division, and the number of times a worksheet is >>>> evaluated. >>> >>> This is an error due to literal constant extraction, I thought there >>> was a ticket for this on trac but was unable to find it for the >>> moment. >>> >>>> Consider the following worksheet. The very first time it is >>>> evaluated, division is handled as float division, and the results >>>> are >>>> what I hoped for. But, if I evaluate the cell again, I get a >>>> different answer. This time the division seems to be python >>>> integer >>>> division, and I get a different result. I keep getting this second >>>> result until I restart the worksheet, then I get the first result >>>> for >>>> a single evaluation. >>>> >>>> Regardless of what the correct behaviour is, I would hope to get >>>> the >>>> same behaviour every time I evaluate the worksheet. This looks >>>> like a >>>> bug to me. >>>> >>>> ================================ >>>> First evaluation after restarting worksheet: >>>> >>>> Division Error? >>>> system:sage >>>> >>>> {{{id=2| >>>> Integer=int >>>> >>>> print float(500/1000), float(1000/1000) >>>> print float(500/1000.), float(1000/1000.) >>>> /// >>>> >>>> 0.5 1.0 >>>> 0.5 1.0 >>>> }}} >>> >>> To see what is really going on, this gets turned into >>> >>> {{{ >>> print sage.misc.preparser.preparse_file(""" >>> Integer=int >>> >>> print float(500/1000), float(1000/1000) >>> print float(500/1000.), float(1000/1000.) >>> """) >>> /// >>> _sage_const_500 = Integer(500); _sage_const_1000 = Integer(1000); >>> _sage_const_1000p = RealNumber('1000.') >>> Integer=int >>> >>> print float(_sage_const_500 /_sage_const_1000 ), float >>> (_sage_const_1000/_sage_const_1000 ) >>> print float(_sage_const_500 /_sage_const_1000p ), float >>> (_sage_const_1000/_sage_const_1000p ) >>> }}} >>> >>> 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? >> >> Thanks for the info. I still don't understand why the results are >> different the second time the cell is evaluated. But, I'm not a >> developer, so it really isn't important that I understand this. > > In the above preparsed code, you find the line > > _sage_const_500 = Integer(500); > > and it appears before the "Integer = int" definition. This means that > the first time the cell is executed, _sage_const_500 is defined using > the original definition of Integer, whereas the second (and > subsequent) times the cell is executed, _sage_const_500 is defined to > be int(500) because Integer has been redefined. > > So if you execute Integer=int in a different cell, then you won't see > this problem.
Ahh. Now I understand. Thanks. But, this tells me that I have another issue to look for. I had noted that a couple of cells in a large worksheet did not always produce the same result. I was slowly paring down the code, trying to find the specific code and triggers for the problem. I noted the anomaly in this thread, and thought that it could have been the cause for the strange behaviour I was seeing. But this worksheet only has the Integer=int at the very top, so this could not have caused what I saw. I will keep looking. -- Kevin Horton Ottawa, Canada --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---