Erlang/OTP Forums

Author Message

<  Erlang  ~  Strange floating point arithmetic behavior

richard
Posted: Thu Oct 18, 2007 10:37 pm Reply with quote
Joined: 18 Oct 2007 Posts: 1
My Erlang is not multiplying correctly using one of the early exercises in the Armstrong book. See the difference between the result obtained from a compiled program, and the one given by the shell itself (which is correct).
Any ideas what could be causing this?
By the way, I entered line 1> in the shell to show that there is no precompiled .beam file somewhere interfering.
Code:
-module(geometry). 
-export([area/1]).
area({rectangle, Width, Ht}) -> Width * Ht;
area({circle, R})            -> 3.14159 * R * R.


Erlang (BEAM) emulator version 5.5.2 [source] [async-threads:0] [kernel-poll:false]
Eshell V5.5.2 (abort with ^G)

1>geometry:area({circle, 1.4}).
** exited: {undef,[{geometry,area,[{circle,1.40000e+0}]},
{erl_eval,do_apply,5},
{shell,exprs,6},
{shell,eval_loop,3}]} **
=ERROR REPORT==== 18-Oct-2007::22:35:21 ===
Error in process <0.31.0> with exit value: {undef,[{geometry,area,[{circle,1.400000e+00}]},{erl_eval,do_apply,5},{shell,exprs,6},{shell,eval_loop,3}]}

2>c(geometry.erl).
{ok,geometry}

3>geometry:area({circle, 1.4}).
-2.09393e+232

4>3.14159 * 1.4 * 1.4.
6.15752e+0

It also is strange that my system is showing results in exp. notation. Wonder if these issues are somehow related?
Thanks for the help,
Richard
View user's profile Send private message
hao
Posted: Fri Oct 19, 2007 8:17 am Reply with quote
User Joined: 20 Aug 2007 Posts: 18 Location: Uppsala, Sweden
Hi, Richard,

I have copied your code into a .erl file and started Erlang shell to follow the steps you did. But what I got is totally correct.

Code:
hao@reeb:~/Hao/work/Erlang/exercise$ erl
Erlang (BEAM) emulator version 5.5.2 [source] [async-threads:0] [kernel-poll:false]

Eshell V5.5.2  (abort with ^G)
1> geometry:area({circle,1.4}).
** exited: {undef,[{geometry,area,[{circle,1.40000}]},
                   {erl_eval,do_apply,5},
                   {shell,exprs,6},
                   {shell,eval_loop,3}]} **

=ERROR REPORT==== 19-Oct-2007::10:03:37 ===
Error in process <0.30.0> with exit value: {undef,[{geometry,area,[{circle,1.400000e+00}]},{erl_eval,do_apply,5},{shell,exprs,6},{shell,eval_loop,3}]}

2> c(geometry.erl).
{ok,geometry}
     
3> geometry:area({circle, 1.4}).
[b]6.15752[/b]

4> 3.14159 * 1.4 * 1.4.
[b]6.15752[/b]


So maybe you need to try deleting everything and starting all from scratch again. I don't think there is anything wrong in your source code or compiling steps in the shell. It is kind of weird that you got the totally different result. Confused


Last edited by hao on Fri Oct 19, 2007 8:28 am; edited 1 time in total
View user's profile Send private message Send e-mail MSN Messenger
Mazen
Posted: Fri Oct 19, 2007 8:22 am Reply with quote
User Joined: 20 Jul 2006 Posts: 164 Location: London
Hmm that is strange... Don't know the cause, just wanted to confirm that it works. Below is output of what I copy pasted from your post. perhaps update your Erlang version? Seams bad though... a computer that can't compute Razz

Code:

Erlang (BEAM) emulator version 5.5.3 [source] [async-threads:0] [kernel-poll:false]

Eshell V5.5.3  (abort with ^G)
1> l(geometry).
{module,geometry}
2> geometry:area({circle,1.4}).
6.15752
3> 3.14159 * 1.4 * 1.4.
6.15752
4>
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