[erlang-questions] Division by Zero

29 views
Skip to first unread message

Lucky Khoza

unread,
Nov 15, 2012, 8:42:42 AM11/15/12
to erlang-q...@erlang.org
Hi Erlang Developers,

How do i resolve division by zero errors in Erlang.

Kindest Regards
Lucky

Valentin Micic

unread,
Nov 15, 2012, 8:47:26 AM11/15/12
to Lucky Khoza, erlang-q...@erlang.org
Did you consider not dividing by zero?

V/
> _______________________________________________
> erlang-questions mailing list
> erlang-q...@erlang.org
> http://erlang.org/mailman/listinfo/erlang-questions

_______________________________________________
erlang-questions mailing list
erlang-q...@erlang.org
http://erlang.org/mailman/listinfo/erlang-questions

Eric Newhuis

unread,
Nov 15, 2012, 9:03:04 AM11/15/12
to Valentin Micic, erlang-q...@erlang.org
LOL

I suggest three things.

1. If you are wrestling with really a higher order problem of dealing with abstract things like infinity then I suggest that you define your own number type and provide your own math functions that are well behaved in such situations, return 'infinity' for example from a custom divide(X, Y) function or some such ilk,

2. Use a guard to detect the 0 as early as possible in your code, preferably at the first point you can determine that a 0 would lead to disaster. That way the div0 would be a function clause error and you could spot the problem earlier.

3. Wrap code in a try-catch. Icky.

Thomas Allen

unread,
Nov 15, 2012, 9:03:43 AM11/15/12
to erlang-q...@erlang.org
On Thu, Nov 15, 2012 at 03:42:42PM +0200, Lucky Khoza wrote:
> How do i resolve division by zero errors in Erlang.

You can catch errors like this with the try/catch construct, documented
here:

http://www.erlang.org/doc/reference_manual/expressions.html#id78820

An example for your specific case:

1> try 1 / 0 catch error:badarith -> io:format("Impossible!~n") end.
Impossible!
ok

Note above that division by zero results in a 'badarith' error, for "bad
arithmetic."

You ought to refer to tutorials and the manual for basic things like these.

Thomas

James Rosenblum

unread,
Nov 15, 2012, 9:07:45 AM11/15/12
to Valentin Micic, Lucky Khoza, erlang-q...@erlang.org
Valentin's very perceptive comment aside, you could use one of the two error handling mechanisms as in:

case catch A/N of
{'EXIT', {badarith,_}} ->
   io:format("Some error statement or whatever!~n",[]);
Normal -> Normal
    end.

or

try A/N 
    catch
error:badarith ->
   io:format("some statement or whatever ~p/~p~n",[A,N]);
Normal -> Normal
    end.

I'm not positive, but it seems like you might not be aware of these error handling mechanisms 


From: Valentin Micic <vale...@pixie.co.za>
To: Lucky Khoza <mrk...@gmail.com>
Cc: erlang-q...@erlang.org
Sent: Thursday, November 15, 2012 8:47 AM
Subject: Re: [erlang-questions] Division by Zero

Richard O'Keefe

unread,
Nov 15, 2012, 5:16:27 PM11/15/12
to Lucky Khoza, erlang-q...@erlang.org

On 16/11/2012, at 2:42 AM, Lucky Khoza wrote:

> Hi Erlang Developers,
>
> How do i resolve division by zero errors in Erlang.

Could you please tell us what you actually mean?
For example, I "resolve" division by zero errors in C
by making sure they simply never happen in the first place.
Come to think of it, that's how I "resolve" division by
zero errors in Java, Smalltalk, Lisp, Fortran, AWK,
Python, and Erlang as well.

Some errors are symptoms of bad programming and just plain
_shouldn't_ be caught by handlers in your code lest you
hide mistakes. Part of Erlang design philosophy is that
it is much better for a process to crash promptly than to
keep on going with imaginary or otherwise incorrect data.

If your question is "given that a division by zero has
NOT been prevented, and has occurred, how do I catch it
and how do I tell that a division by zero is the thing
that happened?"

So have you read chapter 10 of the Erlang reference manual?
http://www.erlang.org/doc/reference_manual/errors.html
This lists the predefined exit reasons in table 10.2 with
their causes. It says in section 10.3 how to handle such
errors.

But I repeat: it is better to *prevent* errors rather than
catch them, if an error that you *could* have prevented is
caught, you will probably regret it, and you should never
handle an exception unless you really can honestly recover
*fully* and continue sanely.
Reply all
Reply to author
Forward
0 new messages