Erlang/OTP Forums

Author Message

<  Erlang  ~  Floating point error

jls
Posted: Sun Apr 19, 2009 5:23 pm Reply with quote
Joined: 19 Apr 2009 Posts: 2 Location: Seville, Spain
Hi all,

I am a newbie in Erlang and I have found a wrong behaviour in floating point operations.

1> 0.1*0.2.
0.020000000000000004
2> 0.1+0.2.
0.30000000000000004

Could anybody tell me about it?

Thanking you in advance.

jls
View user's profile Send private message
uwiger
Posted: Sun Apr 19, 2009 8:49 pm Reply with quote
User Joined: 03 Jul 2006 Posts: 604 Location: Sweden
jls wrote:
I am a newbie in Erlang and I have found a wrong behaviour in floating point operations.

1> 0.1*0.2.
0.020000000000000004
2> 0.1+0.2.
0.30000000000000004


This is a characteristic of standard floating-point arithmetic, and not specific to Erlang:

Code:
~$ python
Python 2.4.4 (#2, Oct 22 2008, 19:52:44)
...
>>> 0.1*0.2
0.020000000000000004
>>> 0.1+0.2
0.30000000000000004
>>>


A good source for understanding the quirks of floating-point arithmetic is http://docs.sun.com/source/806-3568/ncg_goldberg.html

BR,
Ulf W
View user's profile Send private message Visit poster's website
rvirding
Posted: Sun Apr 19, 2009 8:51 pm Reply with quote
User Joined: 30 Aug 2006 Posts: 452 Location: Stockholm, Sweden
This is correct, but the float value is printed with greater precision than floats have so the last digits are basically junk. This is not specific to Erlang but to floating point in general.
View user's profile Send private message Visit poster's website MSN Messenger
jls
Posted: Mon Apr 20, 2009 11:01 am Reply with quote
Joined: 19 Apr 2009 Posts: 2 Location: Seville, Spain
Ok. Thank you both.

jls
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