Erlang/OTP Forums

Author Message

<  Erlang  ~  [newbie] I can't understand compiler warning 'badarith'

breetai
Posted: Wed Oct 17, 2007 11:33 pm Reply with quote
Joined: 17 Oct 2007 Posts: 2 Location: Southern Arizona, USA
I get the warning :this expression would cause a 'badarith' exception at run-time
on line 22, the spawn() in start/1

Code:
%%-----8<-----------------
-module(guessgame).
-compile(export_all).


loop(Target) ->
        receive
                {Pid, Guess} when Guess == Target ->
                        Pid ! {self(), correct},
                        loop(Target);
                {Pid, Guess} when Guess < Target ->
                        Pid ! {self(), low},
                        loop(Target);
                {Pid, _ } ->
                        Pid ! {self(), high},
                        loop(Target)
        end.



start(Target) ->
    io:format("Target = ~p~n",[Target]),
    spawn(guessgame, loop/1, [Target]).

mcguess(Pid, What) ->
    rpc(Pid, What).

rpc(Pid, Request) ->
    Pid ! {self(), Request},
    receive
        {Pid, Response} ->
            Response
    end.
%%-----8<-----------------


I don't understand the error above. Please help.
View user's profile Send private message Send e-mail
hao
Posted: Thu Oct 18, 2007 8:09 am Reply with quote
User Joined: 20 Aug 2007 Posts: 18 Location: Uppsala, Sweden
I think the problem is that the second argument of function 'spawn(guessgame, loop/1, [Target])' on line 22 is wrong. That causes the compile warning of 'badarith' exception at run-time. The correct one should be 'spawn(guessgame, loop, [Target])'.

Please refer to 'spawn' function in erlang module. You should not write in the format of 'func/arity' when that function is used as the parameter of another caller function. 'func/arity' format is to be used in '-export([func/arity, ...])' in the beginning of a module.
View user's profile Send private message Send e-mail MSN Messenger
hao
Posted: Thu Oct 18, 2007 8:18 am Reply with quote
User Joined: 20 Aug 2007 Posts: 18 Location: Uppsala, Sweden
One more thing is that you can refer to Erlang Reference Manual about errors and error handling for detailed explanation. Here is the link: http://www.erlang.org/doc/reference_manual/errors.html

badarith: Argument is of wrong type in an arithmetic expression.

Wink
View user's profile Send private message Send e-mail MSN Messenger
breetai
Posted: Thu Oct 18, 2007 12:14 pm Reply with quote
Joined: 17 Oct 2007 Posts: 2 Location: Southern Arizona, USA
Thanks, I'll change it and have a look. Can you explain why using 'fun' the way I did would cause a 'badarith' error? I was looking at how I used '[Target]', because that's the only place I put a number, I thought.
View user's profile Send private message Send e-mail
hao
Posted: Thu Oct 18, 2007 2:44 pm Reply with quote
User Joined: 20 Aug 2007 Posts: 18 Location: Uppsala, Sweden
No problem. Smile

Well, the reason why your way of using "function/arity" in the original code would cause a "badarith" error is:

1) spawn(Module, Function, Args) -> pid()

Types:
Module = Function = atom()
Args = [term()]

Returns the pid of a new process started by the application of Module:Function to Args. The new process created will be placed in the system scheduler queue and be run some time later.


So the second argument "Function" should be an exact function name without appending "/arity". Otherwise, it will cause an run-time error because the spawn function cannot find the specified function to spawn.

2) What's more, the second argument "Function" in spawn function should be the type of atom(). if you look at what is atom data type in Erlang, you will find this:

An atom is a literal, a constant with name. An atom should be enclosed in single quotes (') if it does not begin with a lower-case letter or if it contains other characters than alphanumeric characters, underscore (_), or @.

Examples:

hello
phone_number
'Monday'
'phone number'


So in the spawn function of your original code, loop/1 is not a valid atom. Because spawn function must have checked the types of its input arguments, when the type is not correct, it will return a "badarith" error. You can try is_atom(loop/1) in the Erlang shell, it will exactly return a "badarith" error.

Therefore, it is not how you used "[Target]" that causes the "badarith" error. "Target" here could be of any type since it is a term(). So the way you used it should be correct. Wink
View user's profile Send private message Send e-mail MSN Messenger
Mazen
Posted: Fri Oct 19, 2007 8:12 am Reply with quote
User Joined: 20 Jul 2006 Posts: 164 Location: London
breetai wrote:
I was looking at how I used '[Target]', because that's the only place I put a number, I thought.


Actually, you should look for operators, not numbers when you get badarith. badarith, basically just says that the expression can not be computed (you can not divide an atom by 1 in this case) E.g. 'hello'/'world' doesn't work either, but will still cause a badarith crash Smile So look for the operator, not the numbers. Smile
View user's profile Send private message
hao
Posted: Fri Oct 19, 2007 8:26 am Reply with quote
User Joined: 20 Aug 2007 Posts: 18 Location: Uppsala, Sweden
Quote:
Actually, you should look for operators, not numbers when you get badarith. badarith, basically just says that the expression can not be computed (you can not divide an atom by 1 in this case) E.g. 'hello'/'world' doesn't work either, but will still cause a badarith crash Smile So look for the operator, not the numbers. Smile


Thanks, Mazen. I did not realize '/' could be recognized as a division operator in "function/arity" format. I agree with your point that we should focus on the operator not number when the 'badarith' occurs. Smile
View user's profile Send private message Send e-mail MSN Messenger

Display posts from previous:  

All times are GMT
Page 1 of 1
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum