Discussion:
[erlang-questions] Division by Zero
unknown
13 years ago
Permalink
Hi Erlang Developers,

How do i resolve division by zero errors in Erlang.

Kindest Regards
Lucky
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20121115/8b9315f3/attachment.html>
unknown
13 years ago
Permalink
Did you consider not dividing by zero?

V/
Post by unknown
Hi Erlang Developers,
How do i resolve division by zero errors in Erlang.
Kindest Regards
Lucky
_______________________________________________
erlang-questions mailing list
erlang-questions
http://erlang.org/mailman/listinfo/erlang-questions
unknown
13 years ago
Permalink
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.
Post by unknown
Did you consider not dividing by zero?
V/
Post by unknown
Hi Erlang Developers,
How do i resolve division by zero errors in Erlang.
Kindest Regards
Lucky
_______________________________________________
erlang-questions mailing list
erlang-questions
http://erlang.org/mailman/listinfo/erlang-questions
_______________________________________________
erlang-questions mailing list
erlang-questions
http://erlang.org/mailman/listinfo/erlang-questions
unknown
13 years ago
Permalink
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 <valentin>
To: Lucky Khoza <mrkhoza>
Cc: erlang-questions
Sent: Thursday, November 15, 2012 8:47 AM
Subject: Re: [erlang-questions] Division by Zero

Did you consider not dividing? by zero?

V/
Post by unknown
Hi Erlang Developers,
How do i resolve division by zero errors in Erlang.
Kindest Regards
Lucky
_______________________________________________
erlang-questions mailing list
erlang-questions
http://erlang.org/mailman/listinfo/erlang-questions
_______________________________________________
erlang-questions mailing list
erlang-questions
http://erlang.org/mailman/listinfo/erlang-questions
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20121115/dcf511d2/attachment.html>
unknown
13 years ago
Permalink
Post by unknown
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
unknown
13 years ago
Permalink
Post by unknown
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.

Continue reading on narkive:
Loading...