Integer division, surprising results

M

Michael Cornelius

As an old C programmer, I'm surprised by some results I'm getting with
integer division. For example:
-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
 
R

Rory Geoghegan

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.
 
S

Steve

Rory said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top