float_to_list(X)

Jesper Louis Andersen jesper.louis.andersen@REDACTED
Wed May 5 18:12:52 CEST 2021


On Wed, May 5, 2021 at 5:58 PM Themba Jonga <themba.jonga@REDACTED> 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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20210505/18c65f48/attachment.htm>


More information about the erlang-questions mailing list