File 0338-doc-Better-description-of-problems-with-float-repres.patch of Package erlang
From c70b2f1eb555087798092500d0dc4495c4c929df Mon Sep 17 00:00:00 2001
From: Kjell Winblad <kjellwinblad@gmail.com>
Date: Thu, 1 Oct 2020 11:57:52 +0200
Subject: [PATCH 38/39] doc: Better description of problems with float
representation
Co-authored-by: Lucas Larsson <garazdawi@gmail.com>
Co-authored-by: Hans Bolinder <hasse@erlang.org>
---
erts/doc/src/erlang.xml | 10 +++--
system/doc/reference_manual/data_types.xml | 43 ++++++++++++----------
2 files changed, 30 insertions(+), 23 deletions(-)
diff --git a/erts/doc/src/erlang.xml b/erts/doc/src/erlang.xml
index bef39344c2..db3303c0a5 100644
--- a/erts/doc/src/erlang.xml
+++ b/erts/doc/src/erlang.xml
@@ -1996,10 +1996,12 @@ true</pre>
"7.12"
> <input>float_to_list(7.12, [{scientific, 3}]).</input>
"7.120e+00"
-> <input>float_to_list(0.1+0.2)</input></pre>
+> <input>float_to_list(0.1+0.2)</input>
+"3.00000000000000044409e-01"
+</pre>
<p>In the last example, <c>float_to_list(0.1+0.2)</c> evaluates to
<c>"3.00000000000000044409e-01"</c>. The reason for this is that
-<seeguide marker="system/reference_manual:data_types#float_represenation_problem"><c>0.1</c> and
+<seeguide marker="system/reference_manual:data_types#float_representation_problem"><c>0.1</c> and
<c>0.2</c> cannot be represented exactly as float
values</seeguide>.</p>
</desc>
@@ -6359,7 +6361,7 @@ true</pre>
<p>In the last example, <c>round(36028797018963969.0)</c>
evaluates to <c>36028797018963968</c>. The reason for this is
that <seeguide
- marker="system/reference_manual:data_types#float_represenation_problem"><c>36028797018963969.0</c>
+ marker="system/reference_manual:data_types#float_representation_problem"><c>36028797018963969.0</c>
cannot be represented exactly as a float value</seeguide>.</p>
<p>Allowed in guard tests.</p>
</desc>
@@ -12127,7 +12129,7 @@ improper_end</pre>
36028797018963968</pre>
<p>In the last example, <c>trunc(36028797018963969.0)</c> evaluates to
<c>36028797018963968</c>. The reason for this is that <seeguide
-marker="system/reference_manual:data_types#float_represenation_problem"><c>36028797018963969.0</c>
+marker="system/reference_manual:data_types#float_representation_problem"><c>36028797018963969.0</c>
cannot be represented exactly as a float value</seeguide>.</p>
<p>Allowed in guard tests.</p>
</desc>
diff --git a/system/doc/reference_manual/data_types.xml b/system/doc/reference_manual/data_types.xml
index b3f1162209..f3a85df39b 100644
--- a/system/doc/reference_manual/data_types.xml
+++ b/system/doc/reference_manual/data_types.xml
@@ -79,25 +79,30 @@
11> <input>1_234.333_333</input>
1234.333333
</pre>
-<marker id="float_represenation_problem" /><note>
- <p>Float literals cannot always be
- represented exactly as floats. This is due to the fact that floats
- are represented by a fixed number of bits in a base-2 system while
- float literals are represented with a base-10 system. Here are
- examples of this phenomenon:</p>
- <pre>
- > <input>0.1+0.2.</input>
- 0.30000000000000004
- > <input>36028797018963969.0.</input>
- 36028797018963970.0
- </pre>
- <p>See external resources for a detailed description of why this is
- the case (for example <url
- href="https://floating-point-gui.de/">https://floating-point-gui.de/</url>
- and <url
- href="https://0.30000000000000004.com/">https://0.30000000000000004.com/</url>)</p>
-</note>
-
+<marker id="float_representation_problem" />
+ <section>
+ <title>Representation of Floating Point Numbers</title>
+ <p>When working with floats you may not see what you expect when printing or
+ doing arithmetic operations. This is because floats
+ are represented by a fixed number of bits in a base-2 system while
+ printed floats are represented with a base-10 system. Here are
+ examples of this phenomenon:
+ </p>
+ <pre>
+> <input>0.1+0.2.</input>
+0.30000000000000004
+> <input>36028797018963969.0.</input>
+36028797018963970.0
+ </pre>
+ <p>For more information see <url href="https://floating-point-gui.de/">
+ What Every Programmer Should Know About Floating-Point Arithmetic</url>
+ and <url href="https://0.30000000000000004.com/">0.30000000000000004.com/</url>.
+ </p>
+ <p>If you need to work with decimal fractions, for instance if you need to represent money,
+ then you should use a library that handles that or work in cents instead of euros so
+ that you do not need decimal fractions.
+ </p>
+ </section>
</section>
<section>
--
2.26.2