Box.Fraction (box v0.15.1)

View Source

Fraction type to work with irrational numbers. The fractions are not simplified. At the moment, it also doesn't support negative fractions.

Summary

Functions

Compares two fraction. The fractions are compared using their float conversion in order to make sure unsimplified expression gets invalid results.

Creates a fraction from primitive types, for strings, see parse/1

Creates a fraction from numerator and denominator

Parses a string to a fraction type. It supports parsing the following formats:

Converts to float

Converts a fraction to string.

Converts to float

Types

t()

@type t() :: %Box.Fraction{denominator: integer(), numerator: integer()}

Functions

compare(left, right)

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

Compares two fraction. The fractions are compared using their float conversion in order to make sure unsimplified expression gets invalid results.

new(numerator)

@spec new({integer(), integer()} | float() | integer()) :: t()

Creates a fraction from primitive types, for strings, see parse/1

new(numerator, denominator)

@spec new(integer(), integer()) :: t()

Creates a fraction from numerator and denominator

parse(string)

@spec parse(String.t()) :: t()

Parses a string to a fraction type. It supports parsing the following formats:

  • Integer ("43"): rendered as %Fraction{numerator: 43, denominator: 1}
  • Float ("12.1"): rendered as %Fraction{numerator: 121, denominator: 10}
  • Fraction ("12/14"): rendered as %Fraction{numerator: 12, denominator: 14}

to_float(fraction)

@spec to_float(t()) :: Float.t()

Converts to float

to_string(fraction)

@spec to_string(t()) :: String.t()

Converts a fraction to string.

to_tuple(fraction)

@spec to_tuple(t()) :: {integer(), integer()}

Converts to float