| Author |
Message |
< Erlang ~ 3.3*3=9.899999999999999 |
| ThirdBorn |
Posted: Sat Nov 21, 2009 4:06 am |
|
|
|
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 ) |
|
|
| Back to top |
|
| Karalabe |
Posted: Sat Nov 21, 2009 9:42 pm |
|
|
|
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 |
|
|
| Back to top |
|
| rvirding |
Posted: Sat Nov 21, 2009 11:18 pm |
|
|
|
User
Joined: 30 Aug 2006
Posts: 452
Location: Stockholm, Sweden
|
|
| Back to top |
|
| ThirdBorn |
Posted: Sun Nov 22, 2009 3:58 pm |
|
|
|
Joined: 21 Nov 2009
Posts: 3
|
Thank and Regard to Karalabe and Rvirding !
I realy did not know it is such an awesome science ! |
|
|
| Back to top |
|
| dsmith |
Posted: Tue Nov 24, 2009 5:53 am |
|
|
|
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"
|
|
|
| Back to top |
|
| ThirdBorn |
Posted: Thu Nov 26, 2009 5:01 pm |
|
|
|
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. |
|
|
| Back to top |
|
| Urbank |
Posted: Sun Dec 20, 2009 2:09 pm |
|
|
|
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. |
|
|
| Back to top |
|
|
|