Arithmetic of arbitrary precision in Erlang
Even schoolchildren are aware of the existence of various number systems and the fact that not every finite decimal fraction is a finite fraction in a binary number system. Few think that because of this fact, operations on float and double are not precise. 3r33460.
If we talk about Erlang, then it, like many other languages, implements the IEEE754 standard for float, while the standard type Integer in Erlang is implemented using arbitrary precision arithmetic. However, I would like to have not only bigint, but also the possibility of operating with rational, complex and floating point numbers with the necessary accuracy. 3r33460.
The article presents a minimal overview of the theory of coding floating-point numbers and the most vivid examples of the emerging effects. The solution, which provides the necessary accuracy of operations through the transition to a fixed-point representation, is designed as an EAPA (Erlang Arbitrary Precision Arithmetic) library designed to meet the needs of financial applications developed at Erlang /Elixir. 3r33460. https://github.com/Vonmo/eapa 3r33460.
When should I use eapa_int? For example, if your application works with money or you need to conveniently and accurately perform computational operations on numbers like ???? you can safely use EAPA. 3r33460.
Like any solution, EAPA is a compromise. We obtain the necessary accuracy by sacrificing memory and computation speed. Performance tests and statistics collected on real systems show that most operations are performedare in the range of 3–30 µs. This point also needs to be considered when choosing an interface with a fixed point EAPA. 3r33460.
Of course, it’s not always necessary to solve such problems on Erlang or Elixir, but when a problem arises and a suitable tool is not found, you have to invent a solution.
This article is an attempt to share with the community a tool and experience, in the hope that for someone this library will be useful and help save time.
How do you count money in Erlang? 3r33460.
P.S. Working with rational and complex numbers, as well as native access to Integer, Float, Complex, Rational types of arbitrary precision will be covered in the following publications. Do not switch! 3r33460.
3r33466. Floating-Point Arithmetic
3r33471. What you need to know about floating-point arithmetic
3r33476. IEEE Standard 754 Floating Point Numbers
IEEE 754 is a standard for binary arithmetic 3r3482.
with floating point 3r33490.