diff options
author | Kjell Winblad <kjellwinblad@gmail.com> | 2020-10-01 11:57:52 +0200 |
---|---|---|
committer | Kjell Winblad <kjellwinblad@gmail.com> | 2020-10-27 11:46:23 +0100 |
commit | c70b2f1eb555087798092500d0dc4495c4c929df (patch) | |
tree | 319ab659c012c87e8e63971de4779a9c8b64ec6a /system | |
parent | 31425a5c83757ff3459b080bac3c6b061b3ff839 (diff) | |
download | erlang-c70b2f1eb555087798092500d0dc4495c4c929df.tar.gz |
doc: Better description of problems with float representation
Co-authored-by: Lucas Larsson <garazdawi@gmail.com>
Co-authored-by: Hans Bolinder <hasse@erlang.org>
Diffstat (limited to 'system')
-rw-r--r-- | system/doc/reference_manual/data_types.xml | 43 |
1 files changed, 24 insertions, 19 deletions
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> |