Well, there is "real division" and "integer division". They are different.
Calculators do real division. They take a real number, divides it by another real number and return a real number.
In number theory we only work with integers. So we have the integer division. When you divide two numbers, you have the quotient and the remainder.
10 div 3 = 3 (quotient) 10 mod 3 = 1 (remainder)
3 * 3 + 1 = 10
The definition of integer division is: given two numbers a and b, the quotient and remainder or the division of a and b are defined by the formulas:
b*q+r=a and 0<=r<b (or 0<=|r|<|b| if dealing with negative numbers)
Perl (as far as I know) does not have a integer division (quotient) operator, but it has a remainder operator. C, Pascal and Python have both operations. But they behave diffent when dealing with negative numbers.
In the definition above, there are two possible values of r, one negative and one positive. In math we always take the positive one, but the languages sometimes take the negative.
C and Pascal (gcc and freepascal to be more specific) truncate the division and then calculate the remainder. Python and Perl, on the other hand, use the floor function instead of truncating. This causes differences in the quotient and remainder when dealing with negative integer division. For example, dividing 3 by (-2):
a = 3, b = -2
b*q+r=a
(-2) * q + r = 3
0<=|r|<2
Two possible solutions:
q=-2 r=-1 (-2) * (-2) + (-1) = 3
or
q=-1 r=1 (-2) * (-1) + (1) = 3
Both are correct. In this case, Perl and Python choose the first one (negative remainder) but Pascal and C choose the second one (positive remainder). On the other hand, if you make a=-3 and b=2, Perl and Python choose the positive r, Pascal and C choose the negative r.
Comment
Well, there is "real division" and "integer division". They are different.
Calculators do real division. They take a real number, divides it by another real number and return a real number.
In number theory we only work with integers. So we have the integer division. When you divide two numbers, you have the quotient and the remainder.
10 div 3 = 3 (quotient)
10 mod 3 = 1 (remainder)
3 * 3 + 1 = 10
The definition of integer division is: given two numbers a and b, the quotient and remainder or the division of a and b are defined by the formulas:
b*q+r=a
and
0<=r<b
(or 0<=|r|<|b| if dealing with negative numbers)
Perl (as far as I know) does not have a integer division (quotient) operator, but it has a remainder operator. C, Pascal and Python have both operations. But they behave diffent when dealing with negative numbers.
In the definition above, there are two possible values of r, one negative and one positive. In math we always take the positive one, but the languages sometimes take the negative.
C and Pascal (gcc and freepascal to be more specific) truncate the division and then calculate the remainder. Python and Perl, on the other hand, use the floor function instead of truncating. This causes differences in the quotient and remainder when dealing with negative integer division. For example, dividing 3 by (-2):
a = 3, b = -2
b*q+r=a
(-2) * q + r = 3
0<=|r|<2
Two possible solutions:
q=-2
r=-1
(-2) * (-2) + (-1) = 3
or
q=-1
r=1
(-2) * (-1) + (1) = 3
Both are correct. In this case, Perl and Python choose the first one (negative remainder) but Pascal and C choose the second one (positive remainder). On the other hand, if you make a=-3 and b=2, Perl and Python choose the positive r, Pascal and C choose the negative r.