[erlang-questions] Strange float / tuple problem

Matthias Lang matthias@REDACTED
Sat Jun 4 10:31:21 CEST 2016


> 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