[erlang-questions] Strange float / tuple problem

zxq9 zxq9@REDACTED
Sat Jun 4 05:58:14 CEST 2016


On 2016年6月3日 金曜日 23:11:16 Donald Steven wrote:
> Hi all,
> 
> I'm having a problem I don't understand.
> 
> I'm creating some X and Y coordinates in a program.  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.  There are many other {X,Y} tuples in the list 
> and all of them are correct, including earlier values of X which were 
> also 0.0 and print correctly as 0.0.
> 
> =====
> 
> Your thoughts would be very welcome.

Life with floats can be a bit ヽ( ̄д ̄;)ノ

4.440892098500626e-16 == 0.000000000000000444089209850

Pretty darn close to zero.

This 0 is almost certainly the result of some computation and is a
float -- so it is not really a 0, it is some fractional value that
probably cannot be represented in binary directly.

So you have something *very close* to, but not quite, 0.0 left over.

-Craig



More information about the erlang-questions mailing list