Differences between revisions 3 and 4
Revision 3 as of 2004-12-08 21:54:28
Size: 354
Editor: wxpstewartb
Comment:
Revision 4 as of 2004-12-08 22:40:32
Size: 1037
Editor: pcp07851797pcs
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
Line 11: Line 10:

----

Yeah this is one of the big stumbling blocks for novices. What you're seeing is a side-effect of C's int division. 7/4 is 1.75, but 1.75 is a float, not an int. ints can only be whole numbers.

Fortunately, the developers of python recognized this problem, but they couldn't simply change it right away because it might break existing python programs. So, to enable "real-world" division where 7/4 = 1.75, add this line to the top of your python script and everything will magically work:
{{{
from __future__ import division
}}}

See also this for more info about this and other stumbling blocks:
http://developers.coedit.net/BeginnerErrorsWithPythonProgramming

I discovered this when my students were creating a fraction class. 7/4 = 1 however -7/4 = -2 It seems to me the second result should be -1 It appears it is using the floor function but it isn't what my students wanted nor can I think of when anyone would want this result. Anyone know why? Thanks for any help Terry S - tscott@fisher.unco.edu


Yeah this is one of the big stumbling blocks for novices. What you're seeing is a side-effect of C's int division. 7/4 is 1.75, but 1.75 is a float, not an int. ints can only be whole numbers.

Fortunately, the developers of python recognized this problem, but they couldn't simply change it right away because it might break existing python programs. So, to enable "real-world" division where 7/4 = 1.75, add this line to the top of your python script and everything will magically work:

from __future__ import division

See also this for more info about this and other stumbling blocks: http://developers.coedit.net/BeginnerErrorsWithPythonProgramming

Asking for Help/SubtractionQuestion (last edited 2011-03-26 23:11:56 by PaulBoddie)

Unable to edit the page? See the FrontPage for instructions.