Live data from Hacker News

1/0 = 0 (2018)

hillelwayne.com

121–130 of 245 posts

Re: 1/0 = 0 (2018)

#121

Earlier quoted context omitted.

It's funny, I hold the exact opposite opinion, but from the same example: In the course of my programming career, I've had at least 3 different instances where I crashed stuff in production because I was computing an average and forgot to handle the case of the empty list. Everything would have been just fine if dividing by zero yielded zero. I've learned my lesson since, but still.

What was the problem with crashing? Surely you had Kubernetes/GCP/ECS restart your container, or if you're using a BEAM based language, it would have just restarted > Everything would have been just fine if dividing by zero yielded zero perhaps you weren't making business decisions based on the reported average, just logging it for metrics or something, in which case I can see how a crash/restart would be annoying.

> What was the problem with crashing?

I imagine the problem was that it crashed the whole process, and so the processing of other, completely fine data that was happening in parallel, was aborted as well. Did that lead to that data being dropped on the floor? Who knows — but probably yes.

And process restarts are not instantaneous, just so you know, and that's even without talking about bringing the application into the "stable stream processing" state, which includes establishing streaming connections with other up- and downstream services.

Re: 1/0 = 0 (2018)

#122

Earlier quoted context omitted.

Never have I ever met anybody who would think dividing by zero yields zero O_o If anything it feels natural to yield +/-infinity

It's not about what I think zero division yields I've taken a math class before. It's just about representation within the type system. If division can return infinities we can't safely combine division with other functions that are expecting ints and floats. Most languages throw an error instead, but there are tradeoffs there too. If you've decided not to throw an error you should at least return a usable number and…

NaN is a valid float, so are infinities

Re: 1/0 = 0 (2018)

#123

Earlier quoted context omitted.

1/0 = 0 is usually not a practical thing, it's to satisfy that the output of the division operator stays in the type and you don't want crashes (a "feature" of ponylang and gleam, e.g.). Its kind of a PL wonk thing. It's not at all a good idea for very important practical reasons as I outline in a reply to parent.

I don't want to handle errors after every division and division doesn't crash, both sound rather practical, though.

The original purpose of defining it to be Nan/INF in floating point was exactly that. You'd do all the work and then check if it was Nan/INF at the end without having to check every intermediate result.

Re: 1/0 = 0 (2018)

#124

Earlier quoted context omitted.

i would not expect 1/0 to be zero. as you divide by smaller numbers, the quotient gets bigger, so i can't understand why someone would expect /0 to be zero.

If I have five apples and were to divide them among 0 people then nobody gets anything and I can eat them all, so the proper solution would be 5.

you can't divide the apples among 0 people and then claim to still have them, because in that case you would have divided them among 1 people

Re: 1/0 = 0 (2018)

#125
post #122

Earlier quoted context omitted.

It's not about what I think zero division yields I've taken a math class before. It's just about representation within the type system. If division can return infinities we can't safely combine division with other functions that are expecting ints and floats. Most languages throw an error instead, but there are tradeoffs there too. If you've decided not to throw an error you should at least return a usable number and…

NaN is a valid float, so are infinities

They're valid according to a spec that doesn't mean I want one showing up when I'm trying to calculate the area of a semicircle or whatever. In the context of getting one by surprise in simple arithmetic they are approximately as bad as zero. Either way you have to decide how to handle it and there are tradeoffs of different approaches, as the article discusses. It's not about someone just being ignorant of basic math like the comment I was replying to implied.

Re: 1/0 = 0 (2018)

#127

So, how often do devs actually want a `/` that isn't the inverse of multiplication? Trying to calculate... I don't know, how many 2-disk raid6 groups I need to hold some amount of data is an error , not "lol you don't need any". If my queue consumer can handle 0 concurrent tasks, it will take literally forever to finish, not finish instantly.

To be fair, if my queue consumer can handle 0 concurrent tasks, I’d rather it finishes instantly than never.

Re: 1/0 = 0 (2018)

#128

Earlier quoted context omitted.

i would not expect 1/0 to be zero. as you divide by smaller numbers, the quotient gets bigger, so i can't understand why someone would expect /0 to be zero.

If I have five apples and were to divide them among 0 people then nobody gets anything and I can eat them all, so the proper solution would be 5.

Paraphrasing you: "If I have five apples and were to divide them among 0 people, how many does each person get?" This sums up one approach to this problem, and can be thought of in a more intuitive manner than the limit approach. The answer could be zero. Or 1. Or 37. In fact, any number makes as much sense as the question. Which is why either an exception is raised, (or +- Inf is returned for floats, but that's just the limit approach). But perhaps it would be more fun just to return a random number on divide by zero :)

Re: 1/0 = 0 (2018)

#129

My head-canon with dividing by zero is that 1/0 = undefined and 1/-0 = -undefined, and that's where I leave it because anything less funny than that seems like an impractical answer.

I am fine if isFinite(1/0) returns false.

Re: 1/0 = 0 (2018)

#130

Earlier quoted context omitted.

If I have five apples and were to divide them among 0 people then nobody gets anything and I can eat them all, so the proper solution would be 5.

you can't divide the apples among 0 people and then claim to still have them, because in that case you would have divided them among 1 people

Like everything in life, it depends... For example: Storage has 5 items that need to be processed. 5 items need to be split equaly between available processes. There are currently 0 available processes so 5 / 0 = 0 items to be processed is more correct than either 5 or Nan or infinity.
Post reply on HN