Velocity Reviews > Integer math question

# Integer math question

Frank
Guest
Posts: n/a

 01-03-2004
Hi,

can anybody help with the following problem?

In C++

i = 5 / 10 and
i = -5 / 10 both have the same result 0.

In python

i = 5 / 10 gives me 0 as expected, but
i = -5 / 10 gives -1 as result.

Is this a feature or a bug? I remember Delphi gave me the same result as
C++.

TIA,
Frank

John Roth
Guest
Posts: n/a

 01-03-2004

"Frank" <(E-Mail Removed)-bremen.de> wrote in message
news:(E-Mail Removed) om...
> Hi,
>
> can anybody help with the following problem?
>
> In C++
>
> i = 5 / 10 and
> i = -5 / 10 both have the same result 0.
>
> In python
>
> i = 5 / 10 gives me 0 as expected, but
> i = -5 / 10 gives -1 as result.
>
> Is this a feature or a bug? I remember Delphi gave me the same result as
> C++.

That's a feature. Integer division is explicitly defined in
Python to do exactly that.

The basic thing to remember is that the *correct*
mathematical result of dividing one integer by
another integer is a rational. Python does not
have a rational data type, so it has to pick one
of the multitude of possible ways of rounding a
non-integral result to an integer.

There is no universally right answer to this process:
the "right" answer to any rounding problem is
what the customer wants it to be.

John Roth

>
> TIA,
> Frank

Sean Ross
Guest
Posts: n/a

 01-03-2004
"Frank" <(E-Mail Removed)-bremen.de> wrote in message
news:(E-Mail Removed) om...
> Hi,
>
> can anybody help with the following problem?
>
> In C++
>
> i = 5 / 10 and
> i = -5 / 10 both have the same result 0.
>
> In python
>
> i = 5 / 10 gives me 0 as expected, but
> i = -5 / 10 gives -1 as result.
>
> Is this a feature or a bug? I remember Delphi gave me the same result as
> C++.
>
> TIA,
> Frank

Using the division algorithm:

Let a,b be integers with b>0. Then there exist unique integers q and r such
that:

a = bq + r and 0<=r<b [1]

If we let a=5 and b=10, then a/b is represented by [1] as

a = bq + r
5 = (10) q + r

.... skipping some steps, we find that q=0 and r=5
5 = 10(0) + 5
5 = 0 + 5
5 = 5
LHS = RHS

so, 5/10 = 0 in integer division (with no remainder).

Now, let a = -5 and b = 10

-5 = (10)q + r

If we have q = -1, then

-5 = (10)(-1) + r
-5 = -10 + r

Recall [1] above, 0 <= r < b. r must be nonnegative.
In this case r=5,

-5 = -10 + 5
-5 = -5
LHS = RHS

So, q = -1, and r=5, in which case -5/10 = -1 in integer division (without
remainder).

Suppose we let q=0 for the second problem above. Then

-5 = (10)(0) + r
-5 = 0 + r
-5 = r
or
r = -5

But, by [1], r must be nonnegative. So, we have a contradiction. In which
case we can say that q cannot equal 0 in the algorithm above.

"This is a feature - Python does it properly, where the others do not."

HTH
Sean

Sean Ross
Guest
Posts: n/a

 01-03-2004

"Sean Ross" <(E-Mail Removed)> wrote in message
news:11EJb.20461\$(E-Mail Removed) ...
>then a/b is represented by [1] as

'represented' is the wrong word here, but hopefully, you get the idea ...

Sean Ross
Guest
Posts: n/a

 01-03-2004
Here's an interactive Python example to help clarify the previous response:

>>> a = 5
>>> b = 10
>>> q , r = divmod(a,b)
>>> q

0
>>> r

5
>>> a = -a
>>> a

-5
>>> q , r = divmod(a,b)
>>> q

-1
>>> r

5
>>> b*q + r # division algorithm

-5
>>> -5 == a

True
>>>

Rainer Deyke
Guest
Posts: n/a

 01-03-2004
Sean Ross wrote:
> a = bq + r and 0<=r<b [1]

But 0 <= r < b is a contradiction when b < 0.

--
Rainer Deyke - http://www.velocityreviews.com/forums/(E-Mail Removed) - http://eldwood.com

Sean Ross
Guest
Posts: n/a

 01-03-2004
"Rainer Deyke" <(E-Mail Removed)> wrote in message
news:kTEJb.724242\$HS4.5376202@attbi_s01...
> Sean Ross wrote:
> > a = bq + r and 0<=r<b [1]

>
> But 0 <= r < b is a contradiction when b < 0.
>

Right. But, the division algorithm states "Let a,b be integers with b>0"
(which I mentioned in that post).

Sean Ross
Guest
Posts: n/a

 01-03-2004
"Rainer Deyke" <(E-Mail Removed)> wrote in message
news:kTEJb.724242\$HS4.5376202@attbi_s01...
> Sean Ross wrote:
> > a = bq + r and 0<=r<b [1]

>
> But 0 <= r < b is a contradiction when b < 0.
>
> --
> Rainer Deyke - (E-Mail Removed) - http://eldwood.com
>
>

Hmm....

>>> a = 5
>>> b = -10
>>> q,r = divmod(a,b)
>>> q

-1
>>> r

-5
>>>

Here, the division algorithm does not apply (b is a negative integer).
Perhaps there's some other theorem for this case?
b<r<=0, when b < 0? I don't know.

Sean Ross
Guest
Posts: n/a

 01-03-2004

"Sean Ross" <(E-Mail Removed)> wrote in message
news:5sFJb.20923\$(E-Mail Removed) ...
> "Rainer Deyke" <(E-Mail Removed)> wrote in message
> news:kTEJb.724242\$HS4.5376202@attbi_s01...
> >>> a = 5
> >>> b = -10
> >>> q,r = divmod(a,b)
> >>> q

> -1
> >>> r

> -5
> >>>

>
> Here, the division algorithm does not apply (b is a negative integer).
> Perhaps there's some other theorem for this case?
> b<r<=0, when b < 0? I don't know.
>

I think you're supposed to do something like this
a = bq + r, 0<= r < |b|
5 = (-10)q + r
-5 = -(-10)q - r
-5 = 10q - r

But, then, q would be 0 and r would be 5. <shrug>

Elaine Jackson
Guest
Posts: n/a

 01-04-2004
C rounds toward the nearest integer and Python rounds down. The behavior is
consistent in each case.

"Frank" <(E-Mail Removed)-bremen.de> wrote in message
news:(E-Mail Removed) om...
| Hi,
|
| can anybody help with the following problem?
|
| In C++
|
| i = 5 / 10 and
| i = -5 / 10 both have the same result 0.
|
| In python
|
| i = 5 / 10 gives me 0 as expected, but
| i = -5 / 10 gives -1 as result.
|
| Is this a feature or a bug? I remember Delphi gave me the same result as
| C++.
|
| TIA,
| Frank