Hacker News new | ask | show | jobs
by nullc 702 days ago
When performing integer "division" of x by y, you're finding a solution {q,r} to the equation y * q = x - r. When y=0 any choice of q works, and using 0,x is a perfectly reasonable and intuitive way of defining things.

Computer integers aren't the real numbers you learned about in gradeschool. INT_MAX+1 is not greater than INT_MAX. :)

x/0 = program explodes is also a justifiable choice. It is not however more or less fundamentally correct than making the result 0.

Floating point division by zero doesn't (typically) crash programs the way integer division by zero does (it typically returns a NaN-- and the programmer is free to turn nans to zeros if they like :)).

2 comments

>Computer integers aren't the real numbers you learned about in gradeschool

Why would anyone think computer integers are real numbers? Anyone who's given it a modicum of thought will know intuitively they're a subset of "real life" integers, not reals.

>using 0,x is a perfectly reasonable and intuitive way of defining things.

I would argue it's neither reasonable nor intuitive. If you want to create some special data type then have at it, but if the behavior of `int` doesn't approximate the behavior of IRL integers, call your data type something else.

x+1 is sometimes less than x ... is "IRL" integers? Division of any integer by any number (other than zero) is an integer ... is "IRL" integers?

Seems to me that ship has sailed!

Are you unfamiliar with the meaning of the term "approximation"?
> When performing integer "division" of x by y, you're finding a solution {q,r} to the equation y * q = x - r. When y=0 any choice of q works, and using 0,x is a perfectly reasonable and intuitive way of defining things.

Nope. You forgot that there's a requirement that r < q. Otherwise I could say e.g. 5/2 = 1, which is certainly no less valid than saying that 5/0 = 0, but not something that anyone sane wants.

I think not, because in the y=0 case the only sensible r is x, because remove the mod 0 subset you have the unmodified set of integers itself-- the remainder can only sensibly be an identity in that case.
> in the y=0 case the only sensible r is x

No, x is an utterly non-sensible value of r.

> remove the mod 0 subset you have the unmodified set of integers itself-- the remainder can only sensibly be an identity in that case.

What are you talking about? There are no possible remainders mod 0 (just as there is one possible remainder mod 1 and there are 2 possible remainders mod 2) and there is no sensible definition of the remainder function; defining it to be identity is no less silly than defining it to be, IDK, 7.