Numbers

hex.pm versionBuild StatusInline docs

Numbers is a tiny Elixir package that facilitates the creation of libraries that want to be able to use any kind of Numeric type.

Some known custom numeric types that implement the Numeric behaviour:

How does it work?

This is done by writing a Behaviour called Numeric, which standardizes the names of the following common mathematical operations:

The Numbers module then dispatches these functions to YourStructModule when called with a %YourStructModule{} struct as one or both of its arguments. If the other argument is a Float or Integer, then it is first converted to an instance of YourStruct by calling YourStructModule.new(the_int_or_float) on it.

Examples:

Using built-in numbers:

iex> alias Numbers, as: N

iex> N.add(1, 2)
3

iex> N.mul(3,5)
15

iex> N.mul(1.5, 100)
150.0

Using Decimals: (requires the Decimal library.)

iex> d = Decimal.new(2)
iex> N.div(d, 10)
#Decimal<0.2>
iex> small_number = N.div(d, 1234)
#Decimal<0.001620745542949756888168557536>
iex> N.pow(small_number, 100)

Installation

The package can be installed as:

  1. Add number to your list of dependencies in mix.exs:
```elixir
def deps do
  [{:number, "~> 2.0.0"}]
end
```
  1. Ensure number is started before your application:
```elixir
def application do
  [applications: [:number]]
end
```

Changelog