float_to_list(X)

85 views
Skip to first unread message

Themba Jonga

unread,
May 5, 2021, 11:58:37 AM5/5/21
to erlang-q...@erlang.org
Hi All

I have the following situation which I am trying to understand (screenshot below) ;
  • The float N has a number of digits after the decimal point. However when converted to a list N's value is reduced, albeit by a miniscule amount.
  • The contrary is true for the float P which has its' value increased, also by a tiny amount.
Is there any way to obtain a float_to_list() result which is the exact same as the original float, where the float variable has an arbitrary number of decimal places?

image.png

I am teaching myself Erlang. However I have no formal coding training and I come from an accounting background.

Regards
 
Themba Jonga
 

Virus-free. www.avg.com

Jesper Louis Andersen

unread,
May 5, 2021, 12:13:41 PM5/5/21
to Themba Jonga, Erlang (E-mail)
On Wed, May 5, 2021 at 5:58 PM Themba Jonga <themba...@gmail.com> wrote:
Hi All

I have the following situation which I am trying to understand (screenshot below) ;
  • The float N has a number of digits after the decimal point. However when converted to a list N's value is reduced, albeit by a miniscule amount.
  • The contrary is true for the float P which has its' value increased, also by a tiny amount.
Is there any way to obtain a float_to_list() result which is the exact same as the original float, where the float variable has an arbitrary number of decimal places?


Floating point numbers are inexact. They use this slight lack of precision as a means by which computations with them become very fast (because you have efficient implementations in hardware). However, it means your numbers as you enter them are slightly off. The numbers you entered are not representable in the underlying (bit)-representation, so a number close to the number you entered is chosen, and it is hoped that rounded to fewer decimal places will account for the inconsistency.

The classic document about IEEE754 floating point arithmetic is this: https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html -- but it isn't the most digestible document out there.
A far more approachable guide is: https://floating-point-gui.de/ -- but of course that might leave out some details you'd like to know.
If you want the background Knuth's "The Art of Computer Programming Vol 2 - Seminumerical Algorithms" is one of the best expositions I've read on how they work.
If you want the hardware-centric view, look up books by Hennesey & Patterson.

As a general advice: getting some familiarity with floating point numbers is always good. Both in Erlang and other languages.

2QdxY4Rz...@potatochowder.com

unread,
May 5, 2021, 12:20:14 PM5/5/21
to erlang-q...@erlang.org
On 2021-05-05 at 17:20:15 +0200,
Themba Jonga <themba...@gmail.com> wrote:

> Hi All
>
> I have the following situation which I am trying to understand (screenshot
> below) ;
>
> - The float N has a number of digits after the decimal point. However
> when converted to a list N's value is reduced, albeit by a miniscule amount.
> - The contrary is true for the float P which has its' value increased,
> also by a tiny amount.
>
> Is there any way to obtain a float_to_list() result which is the exact same
> as the original float, where the float variable has an arbitrary number of
> decimal places?

In short, no. Floating point numbers in computers are inexact, and
nearly all computations (including transformations to and from decimal
notation) involve rounding errors. As an example, how would you (the
person) write the decimal form of 1/3? Remember, computers don't have
an infinite amount of storage for all those threes, but when you
translate any finite number of threes back to a floating point number,
you'll never quite get 1/3. Yes, people can use math tricks to avoid
the problem; equivalent computer and Erlang tricks depend on what,
exactly, you're trying to do.

Much computer science is devoted to this topic; search engines are your
friend, now that you know what you're looking for.

> [image: image.png]

For short excerpts from a shell, go ahead and copy/paste the actual
text. That way, if necessary, "we" can copy/paste it into our shell
instead of risking errors by having to retype it.

> I am teaching myself Erlang. However I have no formal coding training
> and I come from an accounting background.

For accounting purposes, you may find that using integers (which are
exact) representing pennies (or your smallest unit of currency) work
better than floating point numbers. When it comes time to display them
to a user, then you can insert the appropriate decimal point or comma.

Scott Ribe

unread,
May 5, 2021, 12:38:02 PM5/5/21
to 2QdxY4Rz...@potatochowder.com, erlang-q...@erlang.org
> On May 5, 2021, at 10:20 AM, 2QdxY4Rz...@potatochowder.com 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...)


Themba Jonga

unread,
May 5, 2021, 3:14:24 PM5/5/21
to erlang-q...@erlang.org
Many thanks to all for your prompt and useful responses to my email on the above subject matter. 

Regards
Themba Jonga

Themba Jonga

unread,
May 6, 2021, 4:56:04 AM5/6/21
to erlang-q...@erlang.org
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
 


Virus-free. www.avg.com

Virus-free. www.avg.com

Richard O'Keefe

unread,
May 7, 2021, 6:27:03 AM5/7/21
to Themba Jonga, Erlang Questions
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.

Eckard Brauer

unread,
May 7, 2021, 8:00:59 AM5/7/21
to erlang-q...@erlang.org
For that purpose it makes maybe more sense to show a sort of "hybrid"
representation of the form floats are usually represented in memory:

s 1.mmmm e xxxx

where

s is signum ("-", "+" or empty),
mmmm is mantissa, in hex, oct or bin to effectively express all
relevant digit positions (with removed trailing zeroes),
xxxx is exponent with possible "-" and also all digits needed.

That would be both human readable and provide some metric, while still
keeping some compactness (maybe not really true for the binary form)
and unambiguousness.

Just my 2 cents.

E.

Am Fri, 7 May 2021 22:26:39 +1200
schrieb "Richard O'Keefe" <rao...@gmail.com>:

Scott Ribe

unread,
May 7, 2021, 8:37:58 AM5/7/21
to Eckard Brauer, erlang-q...@erlang.org
> On May 7, 2021, at 6:00 AM, Eckard Brauer <eckard...@gmx.de> wrote:
>
> For that purpose it makes maybe more sense to show a sort of "hybrid"
> representation of the form floats are usually represented in memory:
>
> s 1.mmmm e xxxx
>
> where
>
> s is signum ("-", "+" or empty),
> mmmm is mantissa, in hex, oct or bin to effectively express all
> relevant digit positions (with removed trailing zeroes),
> xxxx is exponent with possible "-" and also all digits needed.
>
> That would be both human readable and provide some metric, while still
> keeping some compactness (maybe not really true for the binary form)
> and unambiguousness.
>
> Just my 2 cents.

This is not necessary. If float_to_list is meant to transfer values between machines, then it should use the appropriate algorithms and return enough digits to exactly recover the value. Whether it does or does not do that now should probably be documented. If it does not, then perhaps it should be changed, or an option added.

Bob Ippolito

unread,
May 7, 2021, 8:39:20 AM5/7/21
to Richard O'Keefe, Erlang Questions
~p is a lossless format for floats that uses a conservative number of digits (since R12B02). See OTP-7084. The implementation was based on mochinum:digits/1 from mochiweb.

Thomas Depierre

unread,
May 7, 2021, 11:13:54 AM5/7/21
to Richard O'Keefe, Erlang Questions
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

Peer Stritzinger

unread,
May 8, 2021, 6:27:35 PM5/8/21
to themba...@gmail.com, Erlang Questions
I think for the sake of the OP "round trip" needs explaining:

It does not mean that “arbitrary float string X” -> float -> “string identical to X” is true.

It does however mean: float Y -> string representation -> float Y.

One problem is that many fractions like e.g. the simple example of decimal 0.1 are representable as a finite binary fraction:

0.1 base 10 is 0.00011001100110011… base 2 (where … means its a infinite repetition of the digits 0011)

So converting seemingly harmless base 10 floating point strings result in infinite binary fractions.  Then it depends on how they are rounded if conversion back to base 10 string gives you the same string or a rounding error shows.

Cheers,
-- Peer 

Peer Stritzinger

unread,
May 8, 2021, 6:37:50 PM5/8/21
to themba...@gmail.com, Erlang Questions
Just reading that you come from a accounting background: this messiness I described is the reason its a quite bad idea to represent monetary values as floats because its also the reason that:

This is not exactly what was requested by the current tutorial step. Feel free to play with the shell, anyway.

> 0.1 + 0.1 + 0.1+ 0.1+ 0.1+ 0.1+ 0.1+ 0.1+ 0.1+ 0.1.
0.9999999999999999

No Erlangs floating point implementation isn’t broken (at least not in this way). All floating point implementation based on binary fractions behave like this.

Thats why some languages used for finance have decimal fractions and in languages that don’t the only sane way to represent monetary values are using a integer multiple of some fixed fraction of the currency involved (like pico euro). Its nice if you have a non overflowing integer representation like in Erlang for that BTW.

Now imagine JavaScript has no integers but only floats (if they just added integers recently I take that back partially ;-) and are used to represent money quite a bit -> joy!

Cheers,
-- Peer
>> Virus-free. www.avg.com
>>
>> On Wed, 5 May 2021 at 18:38, Scott Ribe <scott...@elevated-dev.com> wrote:
>> > On May 5, 2021, at 10:20 AM, 2QdxY4Rz...@potatochowder.com 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...)
>>
>>
>>
>> Virus-free. www.avg.com
>

Bob Ippolito

unread,
May 8, 2021, 8:05:43 PM5/8/21
to Peer Stritzinger, Erlang Questions
Modern JavaScript does have BigInt. No syntax support though, so it is not very ergonomic. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt

Many (most?) modern float-to-string functions will use the minimal number of digits that can represent the float exactly (round-trip without loss of precision). Erlang's float_to_list/1 is a relic that does not, possibly for backwards compatibility reasons. I bet changing it would at least break some brittle tests :) Fortunately the functionality is available in OTP these days, in a roundabout way.

Thomas Depierre

unread,
May 9, 2021, 4:34:13 AM5/9/21
to Bob Ippolito, Erlang Questions
Small point: float_to_list/2 will get it soon, (OTP25 at most) and i hope we can make it the default in float_to_list/1.

Something worth pointing out, a lot of these shortest roundtrip without loss of precision are slightly broken. The main reason it is not present in most language is that glibc printf cannot do it (no %g is not the same) and that until fairly recently (2018) it was not known how to make it fast and not need Big Integers.
Reply all
Reply to author
Forward
0 new messages