Muscat.Fraction (muscat v0.2.0) View Source
This module provides some simple operations for fraction.
Link to this section Summary
Functions
Return the absolute value of fraction.
Fraction +
operation without reduction.
Compare two fractions with returning :eq
, :lt
and :gt
.
Fraction /
operation without reduction.
Compare two fractions and returns true
if they are equal, otherwise false
.
Give the fraction reciprocal.
Fraction -
operation without reduction.
Fraction *
operation without reduction.
Same to opposite/1
Creates a fraction from integer value or tuple.
Creates a fraction with numerator and denominator.
Give the opposite fraction
Same to inverse/1
Reduce the fraction to the simplest.
Round a fraction to an arbitrary number of fractional digits.
Link to this section Types
Specs
fraction_tuple() :: {numerator :: integer(), denominator :: neg_integer() | pos_integer()}
Specs
Link to this section Functions
Specs
Return the absolute value of fraction.
Specs
Fraction +
operation without reduction.
Fraction.new(1, 2)
|> Fraction.add(Fraction.new(1, 3))
#=> %{numerator: 5, denominator: 6, sign: :positive}
Fraction.new(2, 4)
|> Fraction.add(Fraction.new(1, 3))
#=> %{numerator: 10, denominator: 12, sign: :positive}
Specs
Compare two fractions with returning :eq
, :lt
and :gt
.
fraction1 = Fraction.new(1280, 2560)
fraction2 = Fraction.new(1, 2)
Fraction.equal?(fraction1, fraction2)
#=> :eq
Specs
Fraction /
operation without reduction.
Fraction.new(1, 3)
|> Fraction.divide(Fraction.new(1, 2))
#=> %{numerator: 2, denominator: 3, sign: :positive}
Fraction.new(2, 4)
|> Fraction.divide(Fraction.new(1, 2))
#=> %{numerator: 4, denominator: 4, sign: :positive}
Specs
Compare two fractions and returns true
if they are equal, otherwise false
.
Fractions will be reduced first and then compared. It means 1/2
is equal to 2/4
.
fraction1 = Fraction.new(1280, 2560)
fraction2 = Fraction.new(1, 2)
Fraction.equal?(fraction1, fraction2)
#=> true
Specs
Give the fraction reciprocal.
If the given numerator is 0
, then raise ArithmeticError
.
Fraction.new(1, 2)
|> Fraction.inverse()
#=> %{numerator: 2, denominator: 1, sign: :positive}
Specs
Fraction -
operation without reduction.
Fraction.new(1, 3)
|> Fraction.minus(Fraction.new(1, 2))
#=> %{numerator: 1, denominator: 6, sign: :negative}
Fraction.new(5, 6)
|> Fraction.minus(Fraction.new(1, 6))
#=> %{numerator: 4, denominator: 6, sign: :positive}
Specs
Fraction *
operation without reduction.
Fraction.new(1, 3)
|> Fraction.multi(Fraction.new(1, 2))
#=> %{numerator: 1, denominator: 6, sign: :positive}
Fraction.new(2, 3)
|> Fraction.multi(Fraction.new(1, 6))
#=> %{numerator: 2, denominator: 18, sign: :positive}
Specs
Same to opposite/1
Specs
new(integer() | fraction_tuple()) :: t()
Creates a fraction from integer value or tuple.
Fraction.new(2)
#=> %{numerator: 2, denominator: 1, sign: :positive}
Fraction.new(0)
#=> %{numerator: 0, denominator: :any, sign: :positive}
Fraction.new({1, 2})
#=> %{numerator: 1, denominator: 2, sign: :positive}
Specs
new(numerator :: integer(), denominator :: neg_integer() | pos_integer()) :: t()
Creates a fraction with numerator and denominator.
Both numerator and denominator are integers.(and the denominator can't be 0
).
It doesn't matter whether the sign of the fraction is at the numerator or denominator.
About 0
- If numerator is
0
, the denominator in result is :any and sign is positive. - If denominator is
0
, it will raise.
Fraction.new(0, 1)
#=> %{numerator: 0, denominator: :any, sign: :positive}
Fraction.new(1, 2)
#=> %{numerator: 1, denominator: 2, sign: :positive}
Fraction.new(-1, -2)
#=> %{numerator: 1, denominator: 2, sign: :positive}
Fraction.new(-1, 2)
#=> %{numerator: 1, denominator: 2, sign: :negative}
Fraction.new(1, -2)
#=> %{numerator: 1, denominator: 2, sign: :negative}
Specs
Give the opposite fraction
If the given numerator is 0
, returns fraction itself.
Fraction.new(1, 2)
|> Fraction.opposite()
#=> %{numerator: 1, denominator: 2, sign: :negative}
Fraction.new(0, 2)
|> Fraction.opposite()
#=> %{numerator: 0, denominator: :any, sign: :positive}
Specs
Same to inverse/1
Specs
Reduce the fraction to the simplest.
Fraction.new(1280, 2560)
|> Fraction.reduce()
#=> %{numerator: 1, denominator: 2, sign: :positive}
Specs
to_float(t(), opts :: [{:precision, non_neg_integer()}]) :: float()
Round a fraction to an arbitrary number of fractional digits.
Options
:precision
- between0
and15
. It usesFloat.round/2
to round.