News for hackers. Stuff that matters.
Re:1/0 = 0 by spooneybarger on Friday August 10, 2018 @05:09PM (#17736755)
Hi,

I'm on the Pony core team. I will be writing in more detail about this decision. A few short notes until then:

1) no one on the team has ever been happy with ending up here, understanding why the decision was made involved understand how partial functions (one that can produce errors like division by zero) are handled in Pony and interesting ergonomic issues that can result that is a large part of what my post will be about.

2) this applies only to integer division wherein division by zero is undefined. 1.0/0.0 uses the available Infinity value.

3) Partial function math is coming to Pony that includes 1/0 as partial (ie error producing) as well as handling integer overflow and underflow for operations: `+`, `-`, and `*`.

4) It's very straightforward even without that those operations to define your own division that will be partial (ie return an error) for division by zero.

Edit to include link to the comment below because it contains a good bit of the decisions that have to make when dealing with integer math:

https://news.ycombinator.com/item?id=17736637

Which is part of what I will touch on in my post about the decision process that Pony went through to reach to state we are currently at (which I want to highlight, is one we intend to improve- see #3 above).

Re:1/0 = 0 by thanatos_dem on Friday August 10, 2018 @05:37PM (#17736920)
> 2) this applies only to integer division wherein division by zero is undefined. 1.0/0.0 uses the available Infinity value.

Oh jeeze... to me that almost nullifies all of the points in the article. It does an alright job explaining how 1/0 = 0 is at least consistent with other notions in the language, but to hear that the same logic can’t be applied to floats is just... well, objectively it’s a mess.

Re:1/0 = 0 by spooneybarger on Friday August 10, 2018 @05:44PM (#17736967)
Hillel is talking about math. Unfortunately computers don't really do "math". For example, math doesn't have overflow and underflow to deal with.

The floating point standard says that division by 0.0 should be Infinity and provides a value for it. The integer math, all possible values are used for numbers, so division by zero is undefined behavior.

And from there, every language is potentially going to have a mess of inconsistency to deal with.

You can make 1/0 = Infinity so long as you box all your integers. That is, its not using the machine type, but I type that is a wrapper that can be the machine type or Infinity. And every user takes a large performance hit even if they will never divide by zero.

For some languages boxing integers is not a bad thing because they already do it. Why do they do it? Usually, because of another not math problem. Overflow and underflow:

What is 0 - 1? Well, its -1 right? Except of course if you have an unsigned integer, then its the max value of the integer. And what if you had 1 to the max value that the computer can represent? You wrap around. Some languages make all integers protect you from this and take a performance hit to be able to box and be able to represent numbers better as we expect them to work. In return, you can not do integer math as fast as you could.

Computers and numbers... its an inconsistent mess and a series of tradeoffs for any language of performance and various possible errors.

I'm going to talk about this and more in my post.

Re:1/0 = 0 by starmole on Friday August 10, 2018 @11:14PM (#17738077)
Integers are perfectly fine in math. But they are not a field. Integers modulo something are also well understood in math - but again not a field.
Re:1/0 = 0 by spacehome on Saturday August 11, 2018 @01:05AM (#17738445)
Integers modulo any prime are a field.
Re:1/0 = 0 by ummonk on Saturday August 11, 2018 @02:17AM (#17738618)
Powers of 2 are not a prime though, unless the power is 1 (i.e. we are dealing with single-bit integers).
Re:1/0 = 0 by nanimo on Saturday August 11, 2018 @12:23AM (#17738333)
Underflow and overflow and 2's complement are all just modular arithmetic, which is definitely math.
Re:1/0 = 0 by gowld on Saturday August 11, 2018 @01:22AM (#17738486)
Computers is the math you get, not the math you want.
Re:1/0 = 0 by spooneybarger on Friday August 10, 2018 @06:04PM (#17737057)
Here's another good one for you...

What does your favorite programming language return for 3/2 as compared to 3.0/2.0? Many will return different values.

EDIT:

for clarity of my point: integer math that uses machine types is pretty surprising compared to what we would expect from "math" in general for division.

Re:1/0 = 0 by jshap70 on Friday August 10, 2018 @06:14PM (#17737092)
`3/2 = 1` is a consistent answer as long as the programmer understands modulo. The way you're phrasing it makes it sound like there are languages out there that say "`3/2 = 2` because 1.5 rounds up to 2".

just so long as you don't expect a consistent answer between python2.7 and 3.x... >.>

Re:1/0 = 0 by Zarel on Friday August 10, 2018 @11:21PM (#17738102)
The phrasing is relatively clear to me as saying that some languages say 3/2 = 1.5 (e.g. JavaScript) and some languages say 3/2 = 1 (e.g. C).
Re:1/0 = 0 by std_throwaway on Friday August 10, 2018 @06:12PM (#17737083)
Did you mean 2/3?
Re:1/0 = 0 by spooneybarger on Friday August 10, 2018 @06:15PM (#17737096)
No. But most programming languages I know of return different results for 2/3 vs 2.0/3.0.
Re:1/0 = 0 by jlouis on Saturday August 11, 2018 @08:04AM (#17739361)
In the mathematical sense, a Field is a Commutative Ring with the added constraint that the multiplicative operation has inverses for every non-zero element.

Floating point doesn't even form a Ring, because the operation is not associative[0]. Coq, for instance, defines the operation for QArith, which are the rational numbers. The representation is a pair (Z, positive), where Z is an integer and positive is a positive number (i.e., 1,2,...). QArith does form a field in the usual sense.

Integers are not a Field (unless you pick Z/p for some prime p), and machine words, signed or unsigned are even less of a mathematical object with the usual operations. So you can do almost anything you want to them from a programming perspective, including 1/0 = 0.

As Hillel writes, what to do will make sense in some settings, but not in others. You may want MAX_INT in some situations, and 0 in others. And this requires a check, partiality or not. Rejection of the case x/0 has more to do with the notion that it is usually a corner case where you want the programmer to think about what the result should be.

[0] The reason is that any addition can incur information loss, and the further away from 0 you are, the more information is lost. Thus, the order in which you add values matter.

Re:1/0 = 0 by daveFNbuck on Saturday August 11, 2018 @12:11AM (#17738298)
As pointed out by others, floats work differently from ints in programming languages. Division in ints is usually defined such that if x = d * y + m, x / y = d and x % y = m. You can make this work by choosing the right m for every d, but when y is not 0, there are a few conventions you can choose to pick a sensible d and m. When y = 0, the m = a and d is completely free, so there really isn't a right answer from how division works.

Division with floats doesn't usually have a definition like that, so you don't end up in a similar situation. Plus, floating point math can have denominators get really close to 0 and have the result of the division tend toward the infinity value. Ints don't have values close to 0 or an infinity to represent what that division would tend toward.

Re:1/0 = 0 by js8 on Saturday August 11, 2018 @03:35AM (#17738760)
As an aside, I have thought recently that integers should also have NaN value, just like floating point values have. It's useful in statistics to differentiate between not having a value and value of 0.

And I was thinking that perhaps 0x80000000 (in two's complement arithmetic) would be a good value for integer NaN. This value is already fixed point with respect to negation, so why not make it into an exception on its own right?

But it would mean to rethink all the hardware which I don't think is an option.

In any case, I think to set 1/0 to 0 is a bad idea, but I could imagine setting x/0 to 0x80000000 (interpreted as NaN) to make more sense.

Re:1/0 = 0 by bluecalm on Saturday August 11, 2018 @10:03AM (#17739699)
I like the idea of having integer NaN but I think unsigned numbers are problematic as there isn't any convenient way to encode it.
Re:1/0 = 0 by ummonk on Saturday August 11, 2018 @02:16AM (#17738614)
>2) this applies only to integer division wherein division by zero is undefined. 1.0/0.0 uses the available Infinity value.

Ah, that seems rather reasonable. For whatever reason, the OP had me thinking that Pony was replacing the ordinary semantics of IEEE 754 floating point division (probably because he talked about fields and multiplicative inverses).

Having 1/0 = 0 seems like a very reasonable choice, especially if you have 1%0 = 1 as well. Of course, if anyone thinks they will actually encounter division by 0 in their code, and needs to treat it as an error, they can always test for it.

Re:1/0 = 0 by ArtWomb on Friday August 10, 2018 @05:42PM (#17736952)
Having 1/0 = 0 and 1.0f / 0.0f = +inf makes sense in two applications I can think of. The first would be currency systems in which prices down to the pre-determined fractional component are represented as unsigned integers. In calculating margin requirements division by zero equating to zero would be the correct answer. And two, for double precision continuous variables, any application involving 3D geometrical transforms. An aspect ratio approaching zero in one axis, would approach positive infinity in the other.

So you may be saving some developers time in operator overloading, etc. More important than the actual binary decision itself is having well-documented behavior. Have not tried Pony yet, but will take a look. Good discussion, keep it up!

Re:1/0 = 0 by spooneybarger on Friday August 10, 2018 @06:28PM (#17737153)
We are over in #ponylang on freenode if you ever want to stop by and chat.
Re:1/0 = 0 by starmole on Friday August 10, 2018 @11:41PM (#17738185)
This should be the top comment. INTEGERS are not a field. But that kind of makes the 0 choice worse? Even cpus always throw on integer divide by 0, so i don't really see the low level optimization. Does it enable some higher level optimization? Why not x/0 = x?
Re:1/0 = 0 by simonbyrne on Friday August 10, 2018 @06:48PM (#17737248)
What about I64.min_value() / (-1)? Why does that give 0? If you're doing wrap-around arithmetic it should give I64.min_value().

https://tio.run/##K8jPq/z/PzG5JL9IwTcxM49LAQjyUssVkotSE0tSNV...

Re:1/0 = 0 by spooneybarger on Friday August 10, 2018 @07:00PM (#17737309)
That might be an LLVM bug.

`I64.max_value() + 1` gives the expected result.

I'm going to have to look into that.

Thanks.

Re:1/0 = 0 by simonbyrne on Friday August 10, 2018 @07:16PM (#17737370)
Apologies, I realise my comment was a bit snarky. Pony looks like a really neat language: I'll be interested to see what you do with it.

Unfortunately LLVM considers division overflow undefined: http://llvm.org/docs/LangRef.html#id109

If it's any use, you can see what Julia ended up doing here: https://github.com/JuliaLang/julia/issues/8188

Re:1/0 = 0 by spooneybarger on Friday August 10, 2018 @07:20PM (#17737387)
I didn't take it as snarky. I was genuinely surprised. Thank you. If you intended it to be snarky, I didn't notice.

And thank you for the additional info, I've updated the issue I opened accordingly:

https://github.com/ponylang/ponyc/issues/2858

Re:1/0 = 0 by mcguire on Friday August 10, 2018 @10:48PM (#17737994)
I'd like to jump in here and point out that Pony ergonomics are unlike that if any other language; it is significantly different from any other language.

If 1/0 = 0 sounds unusable to you, Pony's garbage collection scheme (which is brilliant, by the way) is going to make you think the language is insane.

Re:1/0 = 0 by behringer on Friday August 10, 2018 @06:35PM (#17737185)
1/0 is not infinity either...
Re:1/0 = 0 by optimuspaul on Friday August 10, 2018 @06:52PM (#17737272)
If we assume that we got to 0 because of a rounding error we do know that it should be some positive integer. I think that 1/0 = infinity is a reasonable substitute for an actual value. 1/0 = 0 seems absurd. 1/(a number approaching 0) produces ever increasing integers.

I don't know, does it even matter? What happens when you try to divide a physical object into 0 parts? It doesn't create infinite pieces. It doesn't make the object disappear. Seems like nothing happened. Maybe when we divide by 0 in a program it should halt and catch fire?

I hate logical arguments, I get sucked in and start arguing all the positions.

Re:1/0 = 0 by ummonk on Saturday August 11, 2018 @02:25AM (#17738626)
>If we assume that we got to 0 because of a rounding error we do know that it should be some positive integer.

That's assuming we had rounding error from a positive number, not a negative number. Of course, if you are concerned about rounding, you shouldn't be using integer arithmetic.

>1/(a number approaching 0) produces ever increasing integers.

No. It produces a bunch of 0s, except when the denominator is 1 (where it produces 1) or the denominator is -1 (where it produces -1). Unless you're using a language like Python, in which case it produces -1 for all negative denominators.

Re:1/0 = 0 by jschwartzi on Saturday August 11, 2018 @10:23AM (#17739783)
Actually if your embedded system will stop keeping a patient alive if you crash and the zero value doesn't cause any harm then you don't crash.

There are lots of situations where it makes sense to keep computing even if you suspect the input data is wrong. It's not really good practice to always be crashing. You need to decide how to handle the bad input on a case-by-case basis.

Re:1/0 = 0 by nemetroid on Saturday August 11, 2018 @12:19PM (#17740264)
I think "crash" in these discussions generally refers to "abort the current action and return to a safe state".
Re:1/0 = 0 by someone_is_here on Friday August 10, 2018 @08:01PM (#17737536)
If you divide something 12 feet long into 3 parts, you don't get 4 parts, you get 3 parts that are each 4 feet long. So, if dividing a number by 0 yields infinity, you would expect that dividing something physical into 0 parts would yield 0 parts, each of infinite size, whatever that means.

Sorry to be pedantic.

Re:1/0 = 0 by behringer on Friday August 10, 2018 @10:55PM (#17738016)
It's fine to be pedantic in math. You can't divide something 0 times, there's no solution for that. You can divide something by infinite times, but infinity is not 0. This might help explain things:

https://www.math.utah.edu/~pa/math/0by0.html

Re:1/0 = 0 by spooneybarger on Friday August 10, 2018 @06:39PM (#17737204)
but with floating point units, 1.0/0.0 is infinity.
Re:1/0 = 0 by behringer on Friday August 10, 2018 @10:57PM (#17738021)
That's not correct mathematically though. You can't divide something 0 times and get any number. Adding zero + zero + zero infinite times does not equal 1.
Re:1/0 = 0 by dahart on Saturday August 11, 2018 @03:58AM (#17738823)
> That’s not correct mathematically though.

Floating point infinity is not equal to math infinity. Floating point infinity means there was overflow of the representation we are using, not that the number is larger than any known number.

> Adding zero + zero + zero infinite times does not equal 1.

Sometimes it does. That’s what an integral is.

Re:1/0 = 0 by ordu on Saturday August 11, 2018 @07:59AM (#17739351)
Nope. Integral is the sum of infinitesimal values, not zeroes. If you dig into definition of Riemann integral, then you'll see that the are no zeroes in the sum at any step. Except for zeroes of function being integrated, of course.

For example Riemann integral is just limit of sums built on tagged partitions of a closed interval with largest sub-interval approaches zero. But no single sub-interval is equal to zero, their lengths just approach zero while being strictly positive values.

Calculus does not divide by zero, calculus explores what happens when we move to zero as close as we can and even closer.

Re:1/0 = 0 by jibal on Saturday August 11, 2018 @04:35AM (#17738915)
"That's not correct mathematically though."

Yes, certainly it is.

"You can't divide something 0 times and get any number. Adding zero + zero + zero infinite times does not equal 1."

The discussion is about mathematics, not elementary school arithmetic.

Re:1/0 = 0 by behringer on Wednesday August 15, 2018 @06:45PM (#17770403)
Feel free to give me a proof that adds zeros and gets a number greater than 0.
Re:1/0 = 0 by Zarel on Friday August 10, 2018 @11:55PM (#17738237)
It's plenty correct mathematically; the linked article even explains this.

Generally, your viewpoint comes from an incorrect understanding of mathematics, so let's explore what math actually is:

When we say "10 / 2 = 5", the result "5" doesn't come out of nowhere. It comes out of the definition of division.

There are several ways you could define division. You could interpret "10 / 2 = 5" as meaning any of:

• "If you had a 10-inch stick and you divided it into 2 equal parts, each part would be 5 inches."

• "If you had the equation 2 * X = 10, X = 5 would be a solution to it."

• "If you had 10 Pa of pressure in an enclosed box, and you increased the volume of the box to 2 times its previous volume, its new pressure would be 5 Pa."

• "A wave of wavelength 2cm would have a frequency of 5 per 10cm."

These definitions all lead to the same result – in other words, they're equivalent. When this happens, mathematicians usually don't particularly care which one is the "real" definition.

This applies to many different mathematical axioms. For instance compare the definition of the Parallel Postulate on Wikipedia:

"If a line segment intersects two lines, the two lines will intersect on the side that their angles with the line segment sum up to less than 180°"

– https://en.wikipedia.org/wiki/Parallel_postulate

with the definition on Wolfram MathWorld:

"If you have a straight line and a point not on it, there exists only one straight line which passes through that point and never intersects the first line."

– http://mathworld.wolfram.com/ParallelPostulate.html

These are different definitions. But since they lead to the same result, mathematicians don't really care to argue over which is the "real" definition.

Some definitions only apply over a subset of numbers. For instance, originally, "5 - 10" was undefined. Then negative numbers were invented, to expand the definition. The same thing happened with square roots and complex numbers.

That's the same thing with division. 1 / 0 was originally undefined. But you can come up with new number systems that define it, while preserving all the other properties that make division what it is. A common definition is 1 / 0 = Infinity, which is done in the Riemann sphere number system:

https://en.wikipedia.org/wiki/Riemann_sphere

There's nothing mathematically incorrect about doing any of this.

Re:1/0 = 0 by saudioger on Saturday August 11, 2018 @01:02AM (#17738437)
thanks for such a ginuwine response