View Source Toothmath.Fraction (Toothmath v0.1.0)

This is a hand spun math module to handle Fractions

Summary

Functions

Add 2 fractions together in simplified form

Approximates the given x float value into a fraction

Create a new fraction with numerator and denominator such as 1 / 2

Simply a fraction using the greatest common divisor using the Euclidean algorithm

Get the rational number

Functions

%Toothmath.Fraction{}

(struct)

A fraction

  • :a: the top (numerator) value
  • :b the bottom (denominator) value

add(fraction1, fraction2)

Add 2 fractions together in simplified form

Examples

iex> alias Toothmath.Fraction
iex> Fraction.add(%Fraction{a: 1, b: 2}, %Fraction{a: 1, b: 4})
%Fraction{a: 3, b: 4}

from_value(x, max_denominator \\ 100)

Approximates the given x float value into a fraction

Examples

iex> alias Toothmath.Fraction iex> Fraction.from_value(0.6875) %Fraction{a: 11, b: 16}

new(a, b)

Create a new fraction with numerator and denominator such as 1 / 2

  • a - the numerator
  • b - the denominator

simplify(fraction)

Simply a fraction using the greatest common divisor using the Euclidean algorithm

iex> alias Toothmath.Fraction
iex> Fraction.simplify(%Fraction{a: 2, b: 4})
%Fraction{a: 1, b: 2}

value(fraction)

Get the rational number

a fractional number is always a rational number, but a rational number may or may not be a fractional number

Examples

  iex> alias Toothmath.Fraction
  iex> Fraction.value(%Fraction{a: 1, b: 2})
  0.50