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.

Creates a fraction from integer value or tuple.

Creates a fraction with numerator and denominator.

Give the opposite fraction

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

t() :: %Muscat.Fraction{
  denominator: integer() | :any,
  numerator: integer(),
  sign: :positive | :negative
}

Link to this section Functions

Specs

abs(t()) :: t()

Return the absolute value of fraction.

Link to this function

add(fraction1, fraction2)

View Source

Specs

add(t(), t()) :: t()

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}
Link to this function

compare(fraction1, fraction2)

View Source

Specs

compare(t(), t()) :: :gt | :lt | :eq

Compare two fractions with returning :eq, :lt and :gt .

fraction1 = Fraction.new(1280, 2560)
fraction2 = Fraction.new(1, 2)

Fraction.equal?(fraction1, fraction2)
#=> :eq
Link to this function

divide(fraction, fraction)

View Source

Specs

divide(t(), t()) :: t()

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}
Link to this function

equal?(fraction1, fraction2)

View Source

Specs

equal?(t(), t()) :: boolean()

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

inverse(t()) :: t()

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}
Link to this macro

is_zero_fraction(fraction)

View Source (macro)
Link to this function

minus(fraction, fraction)

View Source

Specs

minus(t(), t()) :: t()

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}
Link to this function

multi(fraction, fraction)

View Source

Specs

multi(t(), t()) :: t()

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

negate(t()) :: t()

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}
Link to this function

new(numerator, denominator)

View Source

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

opposite(t()) :: t()

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

reciprocal(t()) :: t()

Same to inverse/1

Specs

reduce(t()) :: t()

Reduce the fraction to the simplest.

Fraction.new(1280, 2560)
|> Fraction.reduce()
#=> %{numerator: 1, denominator: 2, sign: :positive}
Link to this function

to_float(fraction, opts \\ [])

View Source

Specs

to_float(t(), opts :: [{:precision, non_neg_integer()}]) :: float()

Round a fraction to an arbitrary number of fractional digits.

Options