|
|
| Author |
Message |
< Erlang ~ Floating point error |
| jls |
Posted: Sun Apr 19, 2009 5:23 pm |
|
|
|
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 |
|
|
| Back to top |
|
| uwiger |
Posted: Sun Apr 19, 2009 8:49 pm |
|
|
|
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 |
|
|
| Back to top |
|
| rvirding |
Posted: Sun Apr 19, 2009 8:51 pm |
|
|
|
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. |
|
|
| Back to top |
|
| jls |
Posted: Mon Apr 20, 2009 11:01 am |
|
|
|
Joined: 19 Apr 2009
Posts: 2
Location: Seville, Spain
|
|
| 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
|
|
|