Erlang/OTP Forums

Author Message

<  Erlang  ~  Newbie badarith problem

cgr
Posted: Sat Sep 15, 2007 6:17 pm Reply with quote
Joined: 15 Sep 2007 Posts: 5
I've attached a copy of my .erl file to this msg

The problem I'm trying to solve is here: http://acm.uva.es/p/v1/100.html

Basically, everything compiles fine, but whenever I call prob100/2 w/ something like prob100:prob100(1,10), I get a badarith error like this:

Code:
{badarith,[{prob100,cycle_len,2},
           {lists,map,2},
           {prob100,prob100,2},
           {erl_eval,do_apply,5},
           {shell,exprs,6},
           {shell,eval_loop,3}]}


Since the only arithmetic thing I have in cycle_len/2 there is the modulo operation in the case, I'm sure that's what's causing it. Also, if I uncomment that even/1 function and put it in place of (Num rem 2) and change the case guards to true and false, I get the same badarith error w/ the top stacktrace func being even/1. I would appreciate any help on this problem since it's such an easy idiom and I'm new to erlang and can't get it to show me what arg value killed the process.

Thanks

_________________
-cgr
View user's profile Send private message
wolfgke
Posted: Sun Sep 16, 2007 10:31 am Reply with quote
User Joined: 16 Sep 2007 Posts: 16
I'll explain you how I came to the correct solution:

"hen I try

Code:
prob100:cycle_len(5).


the following error occurs:
Quote:

=ERROR REPORT==== 16-Sep-2007::12:23:32 ===
Error in process <0.116.0> with exit value: {badarith,[{prob100,cycle_len,2},{shell,exprs,6},{shell,eval_loop,3}]}

** exited: {badarith,[{prob100,cycle_len,2},
{shell,exprs,6},
{shell,eval_loop,3}]} **


So it seems that the problem lies in cycle_len/2.

Looking at the code there I saw

Code:
0 -> cycle_len(Num / 2, Acc + 1);


Now it was clear to me: 1/2 delivers 0.500000

So it has to be changed to

Code:
0 -> cycle_len(Num div 2, Acc + 1);


then it works.
View user's profile Send private message
cgr
Posted: Sun Sep 16, 2007 2:08 pm Reply with quote
Joined: 15 Sep 2007 Posts: 5
Thanks for figuring that out!

One thing though, explain to me why that particular case clause was doing that when it should never match w/ an odd number. In other words, you said you ran

Code:
prob100:cycle_len(5).


and got that error, but 5 should've made the case go to the second clause

Code:
1 -> cycle_len(3 * Num + 1, Acc + 1)


since 5 rem 2 is 1. Why did the other case clause care if it didn't match the result?

Otherwise, great job, the whole thing works perfectly now!

_________________
-cgr
View user's profile Send private message
wolfgke
Posted: Mon Sep 17, 2007 7:04 pm Reply with quote
User Joined: 16 Sep 2007 Posts: 16
That is very easy.

Type in your Erlang console the following line and see what happens:

Code:

1.5 rem 2.


Quote:

=ERROR REPORT==== 17-Sep-2007::21:03:22 ===
Error in process <0.31.0> with exit value: {badarith,[{erlang,rem,[1.500000e+000
,2]},{erl_eval,do_apply,5},{shell,exprs,6},{shell,eval_loop,3}]}

** exited: {badarith,[{erlang,'rem',[1.50000,2]},
{erl_eval,do_apply,5},
{shell,exprs,6},
{shell,eval_loop,3}]} **


Now have a look at your own code and see the line:

Code:

case (Num rem 2) of


Then you should know what causes the problem.
View user's profile Send private message
cgr
Posted: Mon Sep 17, 2007 7:59 pm Reply with quote
Joined: 15 Sep 2007 Posts: 5
Wow, I'm an idiot...

I knew that particular expression in the case header was the root of the problem, but I was so focused on it that I totally forgot what I was passing to it during each recursive iteration.

Thanks for explaining that.

_________________
-cgr
View user's profile Send private message
wuji
Posted: Wed Aug 15, 2012 8:23 am Reply with quote
User Joined: 10 Aug 2012 Posts: 654
so hot.""I didn't feel that was a come-on," Hunter said, said, Cheap Ralph Lauren Shirts said, adding that the comment "just flew out of my
Hunter said the couple has been worn down by the the cheap designer *beep* the scrutiny and pressure brought on by their high profile
that began while Edwards was running for the 2008 presidential presidential cheap Ralph Lauren Polo presidential nomination."For me, for my part in it, it's because
no longer interested in hiding, hiding our relationship," she said. said. cheap Ralph Lauren said. "I don't know if you've noticed, but we've had
lot of media scrutiny. It's complicated and it's hard. hard. cheap polo shirts hard. It wears you down after a while."Hunter, 48,
say whether one of them made the break."That's private. We We [h4]discount designer *beep*[/h4] We decided together to end it. It's hard.
painful," she said.She rejected a suggestion that the relationship may may cheap replica *beep* may have been a mistake."I know many things in the
View user's profile Send private message
dongdongwu
Posted: Thu Sep 20, 2012 5:24 am Reply with quote
User Joined: 19 Sep 2012 Posts: 236
His good friend Diane said: "Christian Louboutin Men Shoes was a magician, he make shoes is immediately put his female people with legs and advantage. He understands women wanted to do and can make them into beautiful Cinderella." Madonna often in its concert wearing Christian louboutin high heels , and some famous superstar like Angelina jolie, mariah Carey, beyonce Knowles, the famous Japanese singer YaYouMei Hamasaki helps Christian Louboutin Men Shoes set up its powerful position. The youngest customers will count Tom cruise's daughter sully cruz. Louboutin made for only a pair of handmade Christian Louboutin high heel Shoes! Want to be more fashion? Put on Christian Louboutin Outlet !
Candy colors of the chalaza high-heeled shoes with lolita type allure, set full finely gem blue "neon shoes" is to need to use "sexy" to describe. Each pair are worth careful appreciation of lithe and graceful fairy ludaoli, what kind of most let you move?Christian Louboutin Men Shoes that one brush red is always cannot resist the temptation, Christian Louboutin men outlet continue to use the days of high 8cm above slender heel proclaim the sexy and luxuriant. The bowknot on black pointed high-heeled shoes with sharp rivet concomitant, wild python met enchanting color printing grain, It is that pairs of high-heeled shoes lets Carrie more feminine flavour. Like Christian Louboutin for men her word.
View user's profile Send private message

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