View Source Rational (qrational v1.0.1)

Elixir library implementing rational numbers and math.

The library adds new type of rational/0 numbers and basic math operations for them. Rationals can also interact with integers and floats. Actually this library expands existing functions, so they can work with rationals too. Number operations available:

  • addition
  • subtraction
  • multiplication
  • division
  • power

some-examples

Some examples

iex> use Rational
Kernel

iex> ~n(2/12)
2.0/12.0

iex> ~n(1/4) * ~n(2/3)
1/6

iex> ~n(1/6) + ~n(4/7)
31/42

iex> ~n(7/9) ** 2
49/81

iex> ~n(33/7) - 5
-2/7

Link to this section Summary

Functions

Returns true if term is a rational, otherwise returns false.

Handles the sigil ~n for rationals.

Link to this section Types

@type operator() :: :+ | :- | :* | :/ | :**
@type rational() :: %Rational{denom: number(), num: number()}

Link to this section Functions

Link to this macro

is_rational(term)

View Source (macro)
@spec is_rational(term()) :: boolean()

Returns true if term is a rational, otherwise returns false.

Allowed in guard tests.

@spec op(number(), number(), operator()) :: number()
@spec op(number(), rational(), operator()) :: rational() | number()
@spec op(rational(), number(), operator()) :: rational() | number()
@spec op(rational(), rational(), operator()) :: rational() | number()
@spec sigil_n(String.t(), list()) :: rational()

Handles the sigil ~n for rationals.

It returns a rational/0 number.

examples

Examples

iex> ~n(1/4)
1.0/4.0

iex> ~n(-3.1/5)
-3.1/5.0