View Source Rational (qrational v1.1.0)
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
.
Parses a string into a rational.
Handles the sigil ~n
for rationals.
Link to this section Types
@type operator() :: :+ | :- | :* | :/ | :**
Link to this section Functions
Returns true
if term
is a rational, otherwise returns false
.
Allowed in guard tests.
Parses a string into a rational.
If successful, returns either a rational/0
or number/0
; otherwise returns :error
.
Handles the sigil ~n
for rationals.
It returns a rational/0
or number/0
.
examples
Examples
iex> ~n(1/4)
1.0/4.0
iex> ~n(-3.1/5)
-3.1/5.0