Revision 8 as of 2005-01-09 14:12:48

Clear message

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])```

-3 is the correct answer to the expression -25/12

Here is why.

Consider the statement print 25/12 and print 25%12 {{{>>> print 25/12 2 >>> print 25%12 1}}}

This is because 25 = 2 * 12 + 1

So print -25/12 and print -25%12 gives you {{{>>> print -25/12 -3 >>> print -25%12 11}}}

Because -25 = -3 * 12 + 11