Live data from Hacker News

How to handle division by zero in a language that doesn't support exceptions?

programmers.stackexchange.com

31–40 of 66 posts

Re: How to handle division by zero in a language that doesn't support exceptions?

#31

How do you specify a value that is neither greater than nor less than in no more than 2 bits?

It would be equal. (to what?)

It could also be fuzzy!

In combinatorial game theory, the star game is fuzzy with zero: not less or greater, but not equal either.

The zero game is (iirc) the game where neither the left nor the right player have any moves, so whoever's turn it is loses.

The star game (*) is iirc the game where both players have the move to move to the zero game, so whoever starts wins.

But the star game is fuzzy with the zero game, less than the 1 game, and more than the -1 game.

Iirc.

Re: How to handle division by zero in a language that doesn't support exceptions?

#34

Earlier quoted context omitted.

Yeah, but they usually just say that the result is undefined. At least that's what C and C++ do. And it's useful for optimizers, because they can assume more about basic arithmetic in signed types and do some neat tricks.

Calling it undefned is a cute meta-exception, but the compiler (or runtime) still does something when hitting that code.

Yeah, the compiler typically assumes it works, and encodes assumptions that it does work into the surrounding code. On some systems you will get SIGFPE. On other systems, the result is something defined like 0. On some systems, the result is whatever happened to be in some register or whatever.

Re: How to handle division by zero in a language that doesn't support exceptions?

#35

A really sly option would be to, effectively, allow infinitesimals. So 10/0 would return 10/0 (perhaps a kind of NaN but with more information retained), and if you multiplied it by 0 elsewhere you'd get 10. Probably a bad idea for novice programmers but interesting to consider.

No, this is wrong.

In order to work with infinitesimals in any reasonable way, you have to define how they work. There is a reasonable way to extend the real numbers in a larger field containing infinitesimals. Such a field is non-Archimedean and has very surprising properties: for example, you can construct a geometry in which Euclid's fifth postulate is false, and yet the angles on a triangle still always add up to 180. These fields also allow you to formalize your intuition about the way infinitesimals like "dx" and "dy" in calculus and analysis work: you can take an equation and multiply both sides by dx, for example. This is ordinarily meaningless, since the term dy/dx is ordinarily just notation that does not actually signify a fraction. In non-standard analysis, it IS a fraction. I think it's a shame that analysis classes in college don't use non-Archimedean fields because the proofs are so much simpler.

However, even in these fields which admit infinitesimals, there is no definition for x/0. You can prove it for yourself.

Suppose x = 10/0.

    0*x = 10
    (0+0)*x = 10
    0*x + 0*x = 10
    10 + 10 = 10
    20 = 10
So this is just an all-around bad idea, because it permits us to say that 10 = 20. And all we had to say that 10/0 had a value... we didn't even say what that value was.

P.S. Although infinitesimals exist, non-standard analysis still does not permit you to, say, plug ∞ into an equation, because there are many different infinite numbers. However, other systems (projective geometry) admit ∞ as a value.

Re: How to handle division by zero in a language that doesn't support exceptions?

#37
post #31

Earlier quoted context omitted.

It would be equal. (to what?)

It could also be fuzzy! In combinatorial game theory, the star game is fuzzy with zero: not less or greater, but not equal either. The zero game is (iirc) the game where neither the left nor the right player have any moves, so whoever's turn it is loses. The star game (*) is iirc the game where both players have the move to move to the zero game, so whoever starts wins. But the star game is fuzzy with the zero game,…

Yes. To elaborate, this construction is called the "surreal numbers", and it is the largest ordered field. All ordered fields are isomorphic to subsets of the surreal numbers.
Post reply on HN