[erlang-questions] Behaviour of list_to_float(...)

Rudolph van Graan rvg@REDACTED
Tue Jul 3 13:28:55 CEST 2007


Hi all,

list_to_float is not behaving correctly in my view, even though it  
may be behaving according to the erlang documentation

1> list_to_float("0.1").
0.100000
2> list_to_float("0.0").
0.00000e+0
3> list_to_float("0").

=ERROR REPORT==== 3-Jul-2007::13:11:30 ===
Error in process <0.34.0> with exit value: {badarg, 
[{erlang,list_to_float,["0"]},{erl_eval,do_apply,5},{shell,exprs,6}, 
{shell,eval_loop,3}]}

** exited: {badarg,[{erlang,list_to_float,["0"]},
                     {erl_eval,do_apply,5},
                     {shell,exprs,6},
                     {shell,eval_loop,3}]} **

According to IEEE 754 the number 0 is also a float, namely the  
fraction is 0 and the exponent is also zero, albeit with a sign bit.

I Think the correct behaviour should have been:

list_to_float("0") = 0.0 as The code clearly indicates that we want  
the result to be a float.

To me, the behaviour that makes sense is that list_to_float always  
succeeds if the argument is a number so we can avoid having to write  
code like this:

safe_float(S) ->
   try
     list_to_float(S)
   catch
     error:badarg ->
       list_to_integer(S)
   end.

I think it should also work for list_to_float("1") etc...

Can someone please explain the rationale behind this behaviour?

Rudolph van Graan





More information about the erlang-questions mailing list