Erlang/OTP Forums

Author Message

<  Erlang  ~  3.3*3=9.899999999999999

ThirdBorn
Posted: Sat Nov 21, 2009 4:06 am Reply with quote
Joined: 21 Nov 2009 Posts: 3
Hi,every body:

i found that
3.33*3=9.99
3*3=9
but
3.3*3=9.899999999999999

how can i make 3.3*3=9.9 ?

Thank and Regard.

( Eshell V5.7.3 )
View user's profile Send private message
Karalabe
Posted: Sat Nov 21, 2009 9:42 pm Reply with quote
User Joined: 03 Jul 2009 Posts: 11
Hi,

Although Erlang uses infinite precision integers, for floating point numbers it uses the same mechanism as other programming languages (finite size, probably 32 bits but not sure about this).

The problem with 3.3 or 3.33 is that both become continued fractions when converted to binary... they don't have a fixed number of digits like in decimal form, but infinite number. Because of this, there will always be rounding errors when working with (such) floating point numbers... I guess sometimes you are lucky enough to get such a small error, that the result is actually correct, but generally you will have errors.

A whole branch of mathematics/computer science is just studying how such rounding errors can be avoided, or used in such a way as to guarantee *some* limit on the rounding error. (Numerical Analysis).

Bottom line is: if you work with floating point number, don't expect them to be 100% precise, and always take care of cumulative errors during long calculations.

You can read up more about floating point representations on wikipedia: http://en.wikipedia.org/wiki/Floating_point

Have a great day,
Peter
View user's profile Send private message
rvirding
Posted: Sat Nov 21, 2009 11:18 pm Reply with quote
User Joined: 30 Aug 2006 Posts: 452 Location: Stockholm, Sweden
Erlang uses 64 bit standard IEEE representation of floats. The reference given in wikipedia about floats to a paper by Goldberg is very good and should be read by anyone using floats:

What Every Computer Scientist Should Know About Floating-Point Arithmetic
View user's profile Send private message Visit poster's website MSN Messenger
ThirdBorn
Posted: Sun Nov 22, 2009 3:58 pm Reply with quote
Joined: 21 Nov 2009 Posts: 3
Thank and Regard to Karalabe and Rvirding !

I realy did not know it is such an awesome science !
View user's profile Send private message
dsmith
Posted: Tue Nov 24, 2009 5:53 am Reply with quote
User Joined: 08 Aug 2007 Posts: 41 Location: Toronto
I have a linked-in driver backed by the decNumber C library that I use when I need predictable rounding. Code can be found here:

http://github.com/dsmith-to/decimal
or
git clone git://github.com/dsmith-to/decimal.git

Code:

Erlang R13B01 (erts-5.7.2) [source] [smp:2:2] [rq:2] [async-threads:0] [kernel-poll:false]

Running Erlang
Eshell V5.7.2  (abort with ^G)
1> decimal:start().
ok
2> decimal:multiply("3.33","3").
{decimal,'+',{<<"\t\t\t">>,-2}}
3> decimal:to_list(decimal:multiply("3.33","3")).
"9.99"
4> decimal:to_list(decimal:multiply("3.3","3")).
"9.9"
5> decimal:to_list(decimal:multiply("3","3")). 
"9"
View user's profile Send private message
ThirdBorn
Posted: Thu Nov 26, 2009 5:01 pm Reply with quote
Joined: 21 Nov 2009 Posts: 3
Thank you , Dsmith .
I don't know how to join C library into Erlang, but I will learn it next month.
Regard.
View user's profile Send private message
Urbank
Posted: Sun Dec 20, 2009 2:09 pm Reply with quote
Joined: 20 Dec 2009 Posts: 4
From
3.3*3=9.899999999999999
9.899999999999999
you can estimate the proportion of the error
Code:

Eshell V5.7.2  (abort with ^G)
1> A=3.3*3.
9.899999999999999
2> C=9.9.
9.9
3> E=A-C.
-1.7763568394002505e-15
4> RE=E/C.
-1.7942998377780307e-16
5> math:log(abs(RE))/math:log(2).
-52.30742852519225

With the error around bit 52 you can estimate the mantissa length to 52 and so clearly more than 32-bit floats, likely 64-bit.
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