On Mon, 02 May 2011 at 04:28PM -0700, KvS wrote: > I am staring already for half an hour at the following. This piece of > code: > > reset() > print 'Loop 1:' > B=-2 > for A in range(-3,-1): > C=-A*B/(A+B) > print "A:",A,"B:",B,"C:",C,"A*B+C*(A+B)=",A*B+C*(A+B) > > print 'Loop 2:' > for A in range(-3,-1): > for B in range(-3,-1): > C=-A*B/(A+B) > print "A:",A,"B:",B,"C:",C,"A*B+C*(A+B)=",A*B+C*(A+B) > > should in both loops (obviously) always assign that value to C such > that A*B+C*(A+B) is 0. This indeed happens in the first loop, but not > in the second. Here is the output (Kubuntu 11.04, Sage v. 4.6.2):
I think this is a preparsing issue. In loop 1, when you do B=-2, Sage
preparses it so that B is a "Sage integer". In loop 2, the outputs from
range() are Python integers.
The difference is that Sage integers become rationals when divided, but
Python integers do truncated division. Try changing loop 2 to:
for B in [-3..-2]:
or changing the assignment to
C = -A * B / ZZ(A + B).
Dan
--
--- Dan Drake
----- http://mathsci.kaist.ac.kr/~drake
-------
signature.asc
Description: Digital signature
