| Author |
Message |
< Erlang ~ IEEE 754 (Standard for Binary Floating-Point Arithmetic) lib |
| bkl |
Posted: Sun Aug 17, 2008 10:09 am |
|
|
|
User
Joined: 17 Aug 2008
Posts: 19
|
I am new to Erlang, and try to port a numeric analysis application to Erlang. Is there a IEEE 754 math library (like the "decimal" in Python) in Erlang. For my application, I need to control the "error" in the numeric computation.
By the way, what's the precision in Erlang's float; it is 32 or 64 bit?
Thanks,
Bkl |
|
|
| Back to top |
|
| seanmc |
Posted: Tue Aug 19, 2008 9:29 am |
|
|
|
User
Joined: 03 Aug 2007
Posts: 10
|
Hi Bkl,
There is no specific module for handling floating point numbers in Erlang, however the basic mathematical functions are available in the shell so it's easy enough to construct your own more complex functions as needed.
Erlang uses IEEE 754 64-bit format.
//Sean. |
|
|
| Back to top |
|
| bkl |
Posted: Tue Aug 19, 2008 10:45 pm |
|
|
|
User
Joined: 17 Aug 2008
Posts: 19
|
Sean,
Thanks for the reply.
What I want is a way to execute fixed precision numerical computation. Below is a simple Erlang program to illustrate the problem.
If I add an rational number (1/3) one hundred times, then subtract the sum with the same (1/3) number one hundred times. Even though mathematically I should get 0, but in Erlang floating point operation, I will end up with a very small non-zero (3.33067e-16)!
Looks like there is no standard library in Erlang that can have fixed precision operation like the "decial" package in Python :-<... I wonder if any people run into this same problem and may have a "personal" solution to it.
Thanks again,
bkl
====================================
1> c(test).
{ok,test}
2> X=1/3.
0.333333
3> test:sub(100,test:add(100,0.0,X),X).
3.33067e-16
-module(test).
-export([add/3, sub/3]).
add(N,S,X) ->
if N > 0 -> add(N-1,S+X, X);
true -> S
end.
sub(N,S,X) ->
if N > 0 ->
sub(N-1,S-X,X);
true -> S
end. |
|
|
| Back to top |
|
| dsmith |
Posted: Fri Aug 22, 2008 2:36 pm |
|
|
|
User
Joined: 08 Aug 2007
Posts: 41
Location: Toronto
|
The python Decimal must be a fixed-point implementation. Floating-point IEEE 754-1985 will always give the results you are seeing.
If there is no fixed-point erlang module it might be a nice little project for someone with the proper skill-set. It would be interesting to see how the bit-syntax could be used to parse the fixed-point binary. |
|
|
| Back to top |
|
| dsmith |
Posted: Tue Aug 26, 2008 6:08 am |
|
|
|
User
Joined: 08 Aug 2007
Posts: 41
Location: Toronto
|
I wrote a simple fixed-point module; source attached. It still requires some work, and a lot of testing. It works for your example.
Code:
Eshell V5.6.1 (abort with ^G)
1> X = fixed:divd(1, 3).
{fixed,6148914691236517205}
2> fixed:to_list(test:add(100,0,X)).
"33.3333333333333333"
3> fixed:to_list(test:sub(100,test:add(100,0,X),X)).
"0.0"
4>
--------------------
-module(test).
-export([add/3, sub/3]).
add(N,S,X) ->
if N > 0 -> add(N-1,fixed:add(S, X), X);
true -> S
end.
sub(N,S,X) ->
if N > 0 -> sub(N-1,fixed:subt(S, X),X);
true -> S
end.
-----------------
|
|
|
| Back to top |
|
| dsmith |
Posted: Tue Aug 26, 2008 10:13 pm |
|
|
|
User
Joined: 08 Aug 2007
Posts: 41
Location: Toronto
|
| I clearly forgot my 2's complement arithmetic. Calculations are wrong for negative numbers. I'll repost to contributions when this is fixed. |
|
|
| Back to top |
|
|
|