views:

161

answers:

4

hi.

does the following integer arithmetic property hold?

(m/n)/l == m/(n*l)

At first I thought I knew answer (does not hold), but now am not sure. Does it hold for all numbers or only for certain conditions, i.e. n > l?

the question pertains to computer arithmetic, namely q = n/m, q*m != n, ignoring overflow.

A: 

It holds in all cases, they're equivalent

Michael Mrozek
do you know how to prove it?I tried myself, but my number theory is weak.This is not homework by the way
aaa
It's kind of...self-evident, isn't it? You could rewrite both sides as m * (1/n) * (1/l) if that helps
Michael Mrozek
ugh, I was referring to computer integer arithmetic (with truncation)
aaa
Oh. Specifying that helps
Michael Mrozek
I assumed integer was understood to be computer related. Down vote is not from me btw
aaa
@Michael, @aaa - the downvote was from me, I assumed integer math meant truncation and overflow. Now the egg's on my face because my downvote is too old to undo. My apologies to you Michael.
mtrw
+7  A: 
case1 assume m = kn+b (b<n)
left = (m/n)/l = k/l
right = (kn+b)/(n*l) = k/l + b/(n*l) = k/l (because b<n)

case2 assume m = kn...obviously correct
sza
Not true if n*l overflows the bound of the integer type.
mtrw
@mtrw to be fair, that goes without saying
aaa
@ziang, @aaa - I downvoted this thinking that overflow was an important part of the question. Now my downvote is too old to undo. Sorry ziang.
mtrw
@mtrw, not a problem at all. We learn and discuss things here, who cares about the votes :)
sza
@mtrw: we'll just make sure to upvote it to compensate :)
Matthieu M.
A: 

It holds in all cases where it's defined, namely any time when n and l do not equal zero. It's more readable in LaTeX:

MathBin: Divison Equalities

baddox
+3  A: 

Are you talking about mathematical integers? Or fixed-width integers within a programming language?

The two equations are identical with mathematical integers, but the two functions have different overflow behaviors if you are using fixed-width integers.

For example, suppose integers are 32-bit

(1310720000/65536)/65537 = 20000/65537 = 0

However, 65536 * 65537 will overflow a 32-bit integer, and will equal 65536, so

1310720000/(65536*65537) = 1310720000/65536 = 20000
Chi
+1 for beating me to it. And if I could, another +1 for apparently being the only responder to catch the word integer!
mtrw