# Obtaining wrong quotient when dividing by negative number

I'm having a big moment of ignorance, since my math teacher in college showed us that no one from my class knew to divide two integers right, without knowing anything about the knowledge of my class, the second day. And he was right, we don't know how to divide when either dividend or divisor is negative.

I read in the Spanish Wikipedia that the Euclid's algorithm of division says that having $a$ as dividend and $b$ as divisor and $b\neq 0$, the quotient is the greatest number that multiplied by the divisor gives a number less or equal to the dividend.

So, from this I'd find $\frac{5}{-7}$ would give a quotient of $0$, since $(-7)(1)<(-7)(0)<5<(-7)(-1)$, and a remainder of $5$ (that means I didn't divide, but ok...).

Now If I try to check if I'm right with $a=bq+r$ it satisfies the equation $5=(-7)(0)+5$ so supposedly I'm right but I still don't believe it, I'd bet my computer would give me another quotient to this kind of divisions. So I wrote a single Java program to divide $\frac5{-7}$ and it gives me -1. Then tried with Mathematica 8 and the same result.

That means and following Wikipedia statement, that neither Java nor Mathematica 8 is following the rule that quotient*divisor is less or equal to the dividend, since $(-7)(-1)=7$ and $7>5$. Moreover, the remainder I get from both Java and Mathematica8 is $-2$, so it satisfies the equation $5=(-7)(-1)+(-2)$.

So, what is right and wrong? I'm lost from here.

-
There are different conventions. See en.wikipedia.org/wiki/Modulo_operation –  Rahul Jan 24 '13 at 0:36

Simply: Java, Mathematica round down computations of integer division. For 5/7 this gives 0, but for -5/7 or 5/(-7) it gives -1.

This is indeed inconsistent with the Euclidean notion of a quotient as you stated it, but is necessary if you want -5/7 == 5/(-7), because while $5=(-7)\cdot 0+5$, you can also say that $-5 = 7\cdot(-1)+2$.

I don't know whether the mod operator (% or similar) is also consistent with this in the systems you mentioned, I can tell you it is in Python 2.7.

-

Number theorists follow the convention that if $a$ and $b$ are integers, $b\ne0$, then the quotient $q$ and the remainder $r$ when you divide $a$ by $b$ are the unique integers satisfying $$a=bq+r,\qquad0\le r\lt|b|$$

Other people/calculators/software follow different conventions. Inferior conventions, I would say, but, then again, I'm a number theorist.

-
Imagine the horror, though, of trying to locate a bug that stems from the fact that (-5)/7 != 5/(-7)... –  Alfonso Fernandez Jan 24 '13 at 2:05
@Alfonso, you have a point. I'm not sure whether you have found a bug, or a feature.... –  Gerry Myerson Jan 24 '13 at 2:17
From my point of view it would be a bug. I've made a small app for Android to calcuate GCD of two numbers with euclid's algorithm so it shows the table of divisions made along with the GCD. For positive numbers it works fine, but input -5 and 7 gives GCD -1. Since it's Euclidean division, it's wrong. Now I'm asking myself what situations require that (-5)/7 == 5/(-7). Curiously, Mathematica outputs 1 for gcd[-5, 7]. –  Adrián Pérez Jan 24 '13 at 14:16