| Author |
Message |
< Erlang ~ function_clause error when invoking a function with a float? |
| billrobertson42 |
Posted: Mon Jan 07, 2008 11:41 pm |
|
|
|
User
Joined: 07 Jan 2008
Posts: 13
|
I was going through some starter exercises and experimenting with the factorial function and I ran into this error which I don't understand.
Here is the module
Code:
-module(foo).
-export([fact/1]).
fact(N) when N > 0 -> N * fact(N-1);
fact(0) -> 1;
fact(N) when N < 0 -> {invalid_object, N}.
And here is an example of calling it with the error at the end. Will somebody please explain why this is incorrect?
Code:
1> c(foo).
{ok,foo}
2> foo:fact(0).
1
3> foo:fact(1).
1
4> foo:fact(2).
2
5> foo:fact(20).
2432902008176640000
6> foo:fact(-1).
{invalid_object,-1}
7> foo:fact(5.0).
=ERROR REPORT==== 7-Jan-2008::15:23:25 ===
Error in process <0.98.0> with exit value: {function_clause,[{foo,fact,[0.000000
e+000]},{foo,fact,1},{erl_eval,do_apply,5},{shell,exprs,6},{shell,eval_loop,3}]}
** exited: {function_clause,[{foo,fact,[0.00000e+0]},
{foo,fact,1},
{erl_eval,do_apply,5},
{shell,exprs,6},
{shell,eval_loop,3}]} **
Thanks! |
|
|
| Back to top |
|
| Mazen |
Posted: Tue Jan 08, 2008 10:44 am |
|
|
|
User
Joined: 20 Jul 2006
Posts: 164
Location: London
|
Hi Bill,
Here is a hint:
Code:
1> 0.000000000e+0 == 0.
true
2> 0.000000000e+0 =:= 0.
false
3>
I guess function headers (with all right) use =:= to match.  |
|
|
| Back to top |
|
| billrobertson42 |
Posted: Tue Jan 08, 2008 6:48 pm |
|
|
|
User
Joined: 07 Jan 2008
Posts: 13
|
Thanks. I appreciate the help.
Yes, I see now. So I made that error go away, and experimented more to challenge my low level of understanding.
Now, when passing in a deliberately broken argument. I get a badarith error. It was o.k. with 5.0, but bombs in unexpected way (to me). The shell lets me mix ints and floats in arithmetic.
Any hints on what I don't understand here please? Also, is there a description of how to read the error message? Is it telling me that it failed in line one on the second recursive call of the fact() function?
Thanks!
Code:
-module(foo).
-export([fact/1]).
fact(N) when N > 0 -> N * fact(N-1);
fact(0) -> 1;
fact(0.0) -> 1;
fact(N) when N < 0 -> {invalid_object, N}.
Code:
2> c(foo).
{ok,foo}
3> foo:fact(5.0).
120.000
4> foo:fact(5.2).
=ERROR REPORT==== 8-Jan-2008::13:36:18 ===
Error in process <0.30.0> with exit value: {badarith,[{foo,fact,1},{foo,fact,1},
{erl_eval,do_apply,5},{shell,exprs,6},{shell,eval_loop,3}]}
** exited: {badarith,[{foo,fact,1},
{foo,fact,1},
{erl_eval,do_apply,5},
{shell,exprs,6},
{shell,eval_loop,3}]} **
5> 5.0 - 1.
4.00000
6> 5.2 - 1.
4.20000
7>
|
|
|
| Back to top |
|
| billrobertson42 |
Posted: Tue Jan 08, 2008 7:57 pm |
|
|
|
User
Joined: 07 Jan 2008
Posts: 13
|
I think I know why it is failing now. When it gets to -0.2 it returns the tuple, and then attempts to multiply that by the value it has so far, thus causing the badarith error.
Still would appreciate advice or a link on reading the stack traces though.
Thanks.
*edit*
Yes, that's it.
Code:
-module(foo).
-export([fact/1]).
fact(N) when N > 0 -> N * fact(N-1);
fact(0) -> 1;
fact(0.0) -> 1;
fact(N) when N < 0 -> erlang:error(no_negatives, N).
Code:
15> foo:fact(5.2).
=ERROR REPORT==== 8-Jan-2008::15:00:21 ===
Error in process <0.47.0> with exit value: {no_negatives,[{foo,fact,-8.000000e-0
01},{foo,fact,1},{erl_eval,do_apply,5},{shell,exprs,6},{shell,eval_loop,3}]}
** exited: {no_negatives,[{foo,fact,-0.800000},
{foo,fact,1},
{erl_eval,do_apply,5},
{shell,exprs,6},
{shell,eval_loop,3}]} **
|
|
|
| Back to top |
|
| Mazen |
Posted: Wed Jan 09, 2008 2:00 pm |
|
|
|
User
Joined: 20 Jul 2006
Posts: 164
Location: London
|
When it comes to the error descriptions Erlang has had its fair share of critisism for being a little bit unclear. This is however far from the truth. The error descriptions are pretty easy to understand as long as you know what to look for.
One thing that is important to keep in mind is that even though you get an error, the hard part is to understand how you end up in the faulty state. Lets say that you expected a number but instead got the atom undefined, it is sometimes hard to know where or what the cause is for the undefined to appear (in large systems atleast).
Anyway... enough with my babble the best resource so far is probably this page:
http://www.erlang.org/doc/reference_manual/errors.html
R,
/M |
|
|
| Back to top |
|
| billrobertson42 |
Posted: Fri Jan 11, 2008 5:05 am |
|
|
|
User
Joined: 07 Jan 2008
Posts: 13
|
Erlang's stack trace is nice, although I would have to say Java's stack trace with line numbers is what I am more used to.
I think most of just boils down to familiarity and experience with whatever language and then problem solving in general.
Writing software is the easy part, making it work is another story.
Thanks again for the help. |
|
|
| Back to top |
|
| timrila |
Posted: Tue Jun 12, 2012 9:23 am |
|
|
|
User
Joined: 28 Mar 2012
Posts: 32
|
|
| Back to top |
|
| goods |
Posted: Fri Jun 29, 2012 2:03 am |
|
|
|
User
Joined: 28 Jun 2012
Posts: 14
|
| Christian Louboutin Homme Espadrilles est absolument représentatif de la mode et les loisirs, christian Chaussures Louboutin, est devenu une question de cours le protagoniste populaire de la saison. Nous ferons de notre mieux pour vous satisfaire. Toutes les Louboutin Pas Cher a été travaillé par des artisans et des espadrilles bonne Christian Louboutin pour les Christian Louboutin Homme par la main et c'est le résultat d'une sélection rigoureuse. si vous êtes engagé dans la vogue ou si vous voulez garder le même rythme avec la tendance, Louboutin 2012 chaussures de sport des hommes pourrait être votre meilleur choix. Commandez-le maintenant produits sont expédition libre. |
|
|
| Back to top |
|
| wuji |
Posted: Thu Sep 13, 2012 6:50 am |
|
|
|
User
Joined: 10 Aug 2012
Posts: 654
|
JCPenney and Chrysler, though, decided not to buy airtime during the episode. episode. designer replica *beep* episode. Wendy's decided not to advertise on "The Ellen Show" at all.A
has changed since the day that show first aired.JCPenney has tapped DeGeneres DeGeneres cheap authentic jordans DeGeneres as its spokeswoman and supported its decision even when One Million
a "pro-family advocacy" group, threatened to boycott the national retail chain for for [h3]cheap Ralph Lauren[/h3] for refusing to fire her.In 1996, a Gallup poll showed that the
approval rating for gay marriage was 27 percent. It is more than than [h4]discount designer *beep*[/h4] than 50 percent today.And there are now 34 shows featuring gay characters
leading and supporting roles -- not including reality TV -- compared to to cheap replica *beep* to only 11 shows in 1997.In the "20/20" interview, DeGeneres said she |
|
|
| Back to top |
|
| dongdongwu |
Posted: Wed Sep 19, 2012 7:59 am |
|
|
|
User
Joined: 19 Sep 2012
Posts: 236
|
| Girls would refuse to even leave their replicas behind. specifically when the product beats the complete meaning of the Christian Louboutin men outlet; in conditions of good quality and detailing.If Cinderella was residing in your twenty primary century as opposed to the aged a single then there would not be any 'Happy actually after'. properly appears like girls nowadays are as well fond of the strong;Christian Louboutin Men Shoes to leave them in your center of nowhere.Christian Louboutin for men Shoes would arrive true handy being a excellent handbag using the glimpse and really feel belonging to the authentic but at a very much lesser price tag adding as very much as types picture in your process. |
|
|
| Back to top |
|
|
|
All times are GMT
|
|
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
|
|
|