[erlang-questions] Strange float / tuple problem

Donald Steven t6sn7gt@REDACTED
Sat Jun 4 16:07:11 CEST 2016


Thanks Matthias and Craig.  I see the issue and I've got a work around.  
BTW, is there a way to truncate a float to a given # of places, not just 
0 -- not just a formatted output, but a true truncation?

Don

On 06/04/2016 04:31 AM, Matthias Lang wrote:
>> When I print them via io:format("~nX: ~f   Y: ~f ", [X,Y]),
>>
>> I get:    X: 0.000000   Y: -5.000000
>>
>> which is correct.
>>
>> ======
>>
>> When I make a tuple of them, add them to a list and print them via:
>> io:format("~nL: ~p~n", [L]),
>>
>> I get:    {4.440892098500626e-16,-5.0},
>>
>> which is very strange.
> Does this clear things up?
>
>    1> X = 4.44E-16.
>    4.44e-16
>    2> io:fwrite("With twiddle-f: ~f With twiddle-p: ~p\n", [X, X]).
>    With twiddle-f: 0.000000 With twiddle-p: 4.44e-16
>
> With the ~f format string, the default precision is 6, which is
> why you see 0.000000 in that case.
>
> Here's a site which talks about the general problem without getting
> unnecessarily abstract: http://floating-point-gui.de/
>
> Matt




More information about the erlang-questions mailing list