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. I think it is a very bad situation if a worksheet cell yields different results depending on how many times it has been evaluated. In my opinion this is a big bug. The conclusion I have drawn for the moment is that I need to stop using Integer=int and Float=float. Instead I will have to wrap each problematic call to matplotlib.pyplot in float() or int(). I will also look deeper into sage's plotting, to see if there is a better way to make my plots, rather than using matplotlib.pyplot. -- 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 -~----------~----~----~----~------~----~------~--~---