Packages
Arbitrary precision decimal arithmetic.
Security advisory:
This version has known vulnerabilities.
View advisories
Current section
Files
Jump to
Current section
Files
README.md
# Decimal[](https://travis-ci.org/ericmj/decimal)Arbitrary precision decimal arithmetic.Documentation: https://hexdocs.pm/decimal/## UsageAdd Decimal as a dependency in your `mix.exs` file.```elixirdef deps do [{:decimal, "~> 1.0"}]end```After you are done, run `mix deps.get` in your shell to fetch and compile Decimal. Start an interactive Elixir shell with `iex -S mix`.```elixiriex> alias Decimal, as: Dniliex> D.add(D.new(6), D.new(7))#Decimal<13>iex> D.div(D.new(1), D.new(3))#Decimal<0.333333333>```## Examples### Using the contextThe context specifies the maximum precision of the result of calculations andthe rounding algorithm if the result has a higher precision than the specifiedmaximum. It also holds the list of set of trap enablers and the currently setflags.The context is stored in the process dictionary, this means that you don't haveto pass the context around explicitly and the flags will be updatedautomatically.The context is accessed with `Decimal.get_context/0` and set with`Decimal.set_context/1`. It can also be temporarily set with`Decimal.with_context/2`.```elixiriex> D.get_context%Decimal.Context{flags: [:rounded, :inexact], precision: 9, rounding: :half_up, traps: [:invalid_operation, :division_by_zero]}iex> D.with_context %D.Context{precision: 2}, fn -> IO.inspect D.get_context end%Decimal.Context{flags: [], precision: 2, rounding: :half_up, traps: [:invalid_operation, :division_by_zero]}%Decimal.Context{flags: [], precision: 2, rounding: :half_up, traps: [:invalid_operation, :division_by_zero]}iex> D.set_context(%D.Context{D.get_context | traps: []}):okiex> Decimal.get_context%Decimal.Context{flags: [:rounded, :inexact], precision: 9, rounding: :half_up, traps: []}```### Precision and roundingThe precision is used to limit the amount of decimal digits in the coefficient:```elixiriex> D.set_context(%D.Context{D.get_context | precision: 9}):okiex> D.div(D.new(100), D.new(3))#Decimal<33.3333333>iex> D.set_context(%D.Context{D.get_context | precision: 2}):okiex> D.div(D.new(100), D.new(3))#Decimal<33>```The rounding algorithm specifies how the result of an operation shall be roundedwhen it get be represented with the current precision:```elixiriex> D.set_context(%D.Context{D.get_context | rounding: :half_up}):okiex> D.div(D.new(31), D.new(2))#Decimal<16>iex> D.set_context(%D.Context{D.get_context | rounding: :floor}):okiex> D.div(D.new(31), D.new(2))#Decimal<15>```### Flags and trap enablersWhen an exceptional condition is signalled its flag is set in the context and ifif the trap enabler is set `Decimal.Error` will be raised.```elixiriex> D.set_context(%D.Context{D.get_context | rounding: :floor, precision: 2}):okiex> D.get_context.traps[:invalid_operation, :division_by_zero]iex> D.get_context.flags[]iex> D.div(D.new(31), D.new(2))#Decimal<15>iex> D.get_context.flags[:inexact, :rounded]````:inexact` and `:rounded` were signaled above because the result of theoperation was inexact given the context's precision and had to be rounded to fitthe precision. `Decimal.Error` was not raised because the signals' trap enablersweren't set. We can, however, set the trap enabler if we what this condition toraise.```elixiriex> D.set_context(%D.Context{D.get_context | traps: D.get_context.traps ++ [:inexact]}):okiex> D.div(D.new(31), D.new(2))** (Decimal.Error)```The default trap enablers, such as `:division_by_zero` can be unset:```elixiriex> D.get_context.traps[:invalid_operation, :division_by_zero]iex> D.div(D.new(42), D.new(0))** (Decimal.Error)iex> D.set_context(%D.Context{D.get_context | traps: [], flags: []}):okiex> D.div(D.new(42), D.new(0))#Decimal<Infinity>iex> D.get_context.flags[:division_by_zero]```### Mitigating rounding errorsTODO## License Copyright 2013 Eric Meadows-Jönsson Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.