Erlang/Elixir Arbitrary Precision Arithmetic
release:
make up && make rel
or rebar3 as prod release
test:
make tests
or rebar3 as test ct
rebar deps for external projects:
{deps, [
{eapa, "", {git, "https://github.com/Vonmo/eapa.git", {tag, "v0.1.2"}}}
]}.
mix deps (Elixir):
defp deps do
[
{:eapa, git: "https://github.com/Vonmo/eapa.git", tag: "v0.1.2"}
]
end
Most of modern microprocessors are made with a hardware implementation of real number representation in IEEE754. The number length is limited by the format, and rounding modes influence precision. Programmers are often unable to change the behaviour of equipment, or languages implementation. For example, the official implementation of Erlang stores float in 3 words on a 64-bit machine and in 4 words on a 32-bit one.
IEEE754 numbers are an infinite set represented as finite. That's why every operand can be misrepresented in IEEE754.
Most numbers when represented as a finite set have stable minimal relative error. For instance, it is 11,920928955078125e-6% for a float and 2,2204460492503130808472633361816e-14% for a double. Most programmers can afford to neglect such an error, though it should be mentioned that you can be caught in the same trap because the rate of an absolute error might be up to 10^31 and 10^292 for a float and a double respectively. It can be troublesome for your computing.
EAPA is a NIF extension written on Rust. So far, in EAPA repository there is an eapa_int interface. It is as simple as possible, as well as the most convenient. This interface is used for working with fixed-point numbers. Among its main features are:
- No effects of IEEE754 encoding
- Big numbers support
- Customized precision up to 126 decimal places (current realization)
- Autoscaling
- Support of all main numerical operations
- More or less complete testing, including property based one
with_val/2
– conversion from a float into a fixed-point representation which can be safely used with json and xml.to_float/2
– conversion from a fixed-point number into a float with precision required.to_float/1
– conversion from a fixed-point number into a float.add/2
– sum of two numberssub/2
– differencemul/2
– multiplicationdivp/2
– divisionmin/2
– min numbermax/2
– max numbereq/2
– equalitylt/2
– less thanlte/2
– less than or equals togt/2
– greater thangte/2
– greater than or equals to
You can play with EAPA in Docker enviroment using make
:
make tests
- run testsmake lint
- lintermake xref
- xref analysismake prod
- generate release for target