By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,235 Members | 1,057 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,235 IT Pros & Developers. It's quick & easy.

Integer division, surprising results

P: n/a
As an old C programmer, I'm surprised by some results I'm getting with
integer division. For example:
-1/1000 -1 -9/2

-5

I expect the results of these expressions to be 0 and -4,
respectively.

I've looked at faqs and documentation, and nothing jumped out at me. Can
anyone explain the reasoning for this?

Thanks,
Michael Cornelius
Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
> I've looked at faqs and documentation, and nothing jumped out at me. Can
anyone explain the reasoning for this?


According to my math book integer division is defined as such:
a/b = q, where a = b*q + r and r is the remainder.

A remainder (ei a modulo) is always positive, effectively flooring all
the division operations.
Jul 18 '05 #2

P: n/a
Rory Geoghegan wrote:
I've looked at faqs and documentation, and nothing jumped out at me. Can
anyone explain the reasoning for this?

According to my math book integer division is defined as such:
a/b = q, where a = b*q + r and r is the remainder.

A remainder (ei a modulo) is always positive, effectively flooring all
the division operations.


Remainders are always positive, but modulo can be
defined as either positive or negative (for negative
arguments). There are pros and cons for doing it either
way, although the Python way seems to have more pros
than cons.
--
Steven D'Aprano
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.