Differences between revisions 1 and 11 (spanning 10 versions)
Revision 1 as of 2004-12-08 21:50:30
Size: 354
Editor: cs-lt-04
Comment:
Revision 11 as of 2011-03-26 23:11:37
Size: 961
Editor: PaulBoddie
Comment: Move under the help hierarchy.
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
Describe SubtractionQuestion here. ## page was renamed from SubtractionQuestion
Line 7: Line 7:
Any explanation?
Line 9: Line 8:
Anyone know why?
Line 10: Line 10:
Terry S - tscott@fisher.unco.edu

----

That is what floor division does, it reduces the value to the next lowest whole number. If you want to separate the whole number and fractional part, use math.modf:
{{{
from __future__ import division
import math
print math.modf(25/12)
--> (0.08333, 2.0)
print math.modf(-25/12)
--> (-0.08333, -2.0)

#to just print the whole number as an int:
print int(math.modf(25/12)[1])
}}}

----

Please see the Python Programming FAQ, [[http://www.python.org/doc/faq/programming.html#why-does-22-10-return-3|question 1.3.2]], for an explanation.

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


That is what floor division does, it reduces the value to the next lowest whole number. If you want to separate the whole number and fractional part, use math.modf:

from __future__ import division
import math
print math.modf(25/12)
--> (0.08333, 2.0)
print math.modf(-25/12)
--> (-0.08333, -2.0)

#to just print the whole number as an int:
print int(math.modf(25/12)[1])


Please see the Python Programming FAQ, question 1.3.2, for an explanation.

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

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