float_to_list(X)

Thomas Depierre depierre.thomas@REDACTED
Fri May 7 13:57:44 CEST 2021


That is not true.

The formatting used in format ~p is the shortest round trip one. This will
be different for every single binary floating point number but also be far
shorter and readable. There is work in progress to bring this as an option
in float_to_list/2 thanks to the recent work in Ryu and Dragonbox.

Having a fixed precision already exist in float_to_list/2. This works as
printf %f. I strongly advise to not use it as this precision does not
translate well to the binary format precision loss.

I understand that float to string and string to float conversion are
thought as niche topic, so i will be happy to provide all the bibliography
you may need to explore it if you are surprised by my explanations.

Kindly,

Thomas Depierre


On Fri, 7 May 2021, 12:27 Richard O'Keefe, <raoknz@REDACTED> wrote:

> No, making float_to_list/1 a wrapper for format... does not make sense.
> float_to_list/1 is meant to give you an accurate representation of the
> value.  Two different floats, no matter how small the difference,
> should result in different lists.  It's meant for converting a float
> to text that you can send to another machine and reconstitute the
> same value there.  This will usually result in more digits than a human
> would normally want to see.
>
> If you *want* format ~p, use it!
>
> Now perhaps there might be an argument to be made for
> float_to_list(X: number(), N: integer()) -> string()
> returning a string that represents X as accurately as
> possible using N digits.  But that is another discussion.
>
> On Thu, 6 May 2021 at 20:56, Themba Jonga <themba.jonga@REDACTED> wrote:
>
>> Hi, again.
>>
>> Following a response I got from Dmitry Klionsky, perhaps it makes sense
>> for *float_to_list(N)* to be made into a wrapper for
>> *lists:flatten(io_lib:format("~p",[N]))?*
>>
>> Eshell V11.1  (abort with ^G)
>> 1> N = 4.4445.
>> 4.4445
>> 2> lists:flatten(io_lib:format("~p",[N])).
>> "4.4445"
>> 3> P = 5.4443.
>> 5.4443
>> 4> lists:flatten(io_lib:format("~p",[P])).
>> "5.4443"
>> 5> Q = 1.2345678.
>> 1.2345678
>> 6> lists:flatten(io_lib:format("~p",[Q])).
>> "1.2345678"
>> 7> float_to_list(Q).
>> "1.23456779999999999298e+00"
>> 8>
>>
>> Regards
>>
>> *Themba Jonga*
>>
>>
>>
>>
>> <http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail> Virus-free.
>> www.avg.com
>> <http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
>> <#m_6571645775151075267_m_-2114829433146319840_m_1222824999368714802_DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>
>>
>> On Wed, 5 May 2021 at 18:38, Scott Ribe <scott_ribe@REDACTED>
>> wrote:
>>
>>> > On May 5, 2021, at 10:20 AM, 2QdxY4RzWzUUiLuE@REDACTED wrote:
>>> >
>>> > In short, no.
>>>
>>> Although many (most, actually nearly all) real numbers cannot be
>>> represented precisely as a float, any float can be round-tripped to text
>>> and back exactly. This is part of the IEEE floating point standard,
>>> actually. (And it's not easy...)
>>>
>>>
>>>
>>
>> <http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail> Virus-free.
>> www.avg.com
>> <http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
>> <#m_6571645775151075267_m_-2114829433146319840_DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20210507/62b03010/attachment.htm>


More information about the erlang-questions mailing list