chunky v0.6.0 Chunky.Fraction View Source
Functions for creating and manipulating fractions.
Link to this section Summary
Functions
Add two fractions, or a fraction and an integer, and return the (optionally simplified) result.
Extract the numerator and denominator of a fraction as a tuple of values.
Divide two fractions, or a fraction and an integer and return the (optionally simplified) result.
Compare a fraction with an integer or fraction using equals comparison.
Get the fractional part left after removing any whole components from the fraction.
Get the whole component of a fraction.
Compare a fraction with an integer or fraction using greater than comparison.
Compare a fraction with an integer or fraction using greater than or equal comparison.
Determine if a fraction is greater than 1, and has a whole number component.
Determine if the n-th root of a number is a whole integer.
Determine if a fraction is negative.
Determine if a fraction is positive.
Determine if a fraction is in simplified form.
Determine if a fraction exactly represents a whole number, with no remainder.
Does a fraction represent a zero value?
Compare a fraction with an integer or fraction using less than comparison.
Compare a fraction with an integer or fraction using less than or equal comparison.
Multiply two fractions, or a fraction and an integer and return the (optionally simplified) result.
Create a whole number fraction from an integer.
Create a new fraction from two integers.
Change a combination of fractions, or fractions and integers, into the same denominator.
Normalize (move to the same denominator) all of the fractions and integers in a list.
Generalized integer nth root, from
Use fractions in power/exponent calculations.
Create the reciprocal of a fraction, optionally simplifying the result.
Simplify a fraction.
Break a fraction into whole and fractional parts, returning both.
Subtract two fractions, or a fraction an an integer, and return the (optionally) simplified result.
Add a series of fractions and integers.
Link to this section Functions
Add two fractions, or a fraction and an integer, and return the (optionally simplified) result.
Examples
iex> Fraction.add(Fraction.new(1, 2), Fraction.new(-8, 3))
%Fraction{num: -13, den: 6}
iex> Fraction.add(Fraction.new(3, 4), Fraction.new(3, 4), simplify: true)
%Fraction{num: 3, den: 2}
iex> Fraction.add(Fraction.new(1, 3), 5)
%Fraction{num: 16, den: 3}
iex> Fraction.add(2, Fraction.new(5, 4))
%Fraction{num: 13, den: 4}
Extract the numerator and denominator of a fraction as a tuple of values.
Examples
iex> Fraction.new(1, 37) |> Fraction.components()
{1, 37}
iex> Fraction.new(0, 3) |> Fraction.components()
{0, 3}
Divide two fractions, or a fraction and an integer and return the (optionally simplified) result.
Examples
iex> Fraction.divide(Fraction.new(3, 4), Fraction.new(-7, 2))
%Fraction{num: -6, den: 28}
iex> Fraction.divide(Fraction.new(28, 4), 4, simplify: true)
%Fraction{num: 7, den: 4}
iex> Fraction.divide(60, Fraction.new(7, 12))
%Fraction{num: 720, den: 7}
Compare a fraction with an integer or fraction using equals comparison.
Examples
iex> Fraction.eq?(Fraction.new(7, 9), Fraction.new(3, 5))
false
iex> Fraction.eq?(3, Fraction.new(12, 4))
true
iex> Fraction.eq?(Fraction.new(-3, 12), -2)
false
Get the fractional part left after removing any whole components from the fraction.
Example
iex> Fraction.new(3, 45) |> Fraction.get_remainder()
%Fraction{num: 3, den: 45}
iex> Fraction.new(7, 3) |> Fraction.get_remainder()
%Fraction{num: 1, den: 3}
iex> Fraction.new(-10, 4) |> Fraction.get_remainder()
%Fraction{num: -2, den: 4}
Get the whole component of a fraction.
Examples
iex> Fraction.new(3, 7) |> Fraction.get_whole()
0
iex> Fraction.new(22, 7) |> Fraction.get_whole()
3
iex> Fraction.new(-10, 2) |> Fraction.get_whole()
-5
Compare a fraction with an integer or fraction using greater than comparison.
Examples
iex> Fraction.gt?(Fraction.new(7, 9), Fraction.new(3, 5))
true
iex> Fraction.gt?(3, Fraction.new(15, 4))
false
iex> Fraction.gt?(Fraction.new(-3, 12), -2)
true
Compare a fraction with an integer or fraction using greater than or equal comparison.
Examples
iex> Fraction.gte?(Fraction.new(3, 9), Fraction.new(1, 3))
true
iex> Fraction.gte?(3, Fraction.new(15, 4))
false
iex> Fraction.gte?(Fraction.new(-3, 12), -2)
true
Determine if a fraction is greater than 1, and has a whole number component.
Examples
iex> Fraction.new(3, 17) |> Fraction.has_whole?()
false
iex> Fraction.new(22, 7) |> Fraction.has_whole?()
true
Determine if the n-th root of a number is a whole integer.
If the result n-th root is within epsilon
of a whole
integer, we consider the result an integer n-th root.
This calcualtion runs the fast converging n-th root at a higher
epsilon than it's configured to use for comparison and testing of the
result value.
Examples
iex> Fraction.integer_nth_root?(27, 3)
{true, 3}
iex> Fraction.integer_nth_root?(1234, 6)
{false, :no_integer_nth_root, 3.2750594908836885}
Determine if a fraction is negative.
Examples
iex> Fraction.new(-3, 4) |> Fraction.is_negative?()
true
Determine if a fraction is positive.
Examples
iex> Fraction.new(-3, 4) |> Fraction.is_positive?()
false
Determine if a fraction is in simplified form.
Examples
iex> Fraction.new(2, 4) |> Fraction.is_simplified?()
false
iex> Fraction.new(-10, 4) |> Fraction.is_simplified?()
false
iex> Fraction.new(0, 7) |> Fraction.is_simplified?()
false
iex> Fraction.new(22, 7) |> Fraction.is_simplified?()
true
Determine if a fraction exactly represents a whole number, with no remainder.
Examples
iex> Fraction.new(5, 3) |> Fraction.is_whole?()
false
iex> Fraction.new(-22, 11) |> Fraction.is_whole?()
true
Does a fraction represent a zero value?
Examples
iex> Fraction.new(3, 134) |> Fraction.is_zero?()
false
iex> Fraction.new(0, 34) |> Fraction.is_zero?()
true
Compare a fraction with an integer or fraction using less than comparison.
Examples
iex> Fraction.lt?(Fraction.new(7, 9), Fraction.new(3, 5))
false
iex> Fraction.lt?(3, Fraction.new(15, 4))
true
iex> Fraction.lt?(Fraction.new(-3, 12), -2)
false
Compare a fraction with an integer or fraction using less than or equal comparison.
Examples
iex> Fraction.lte?(Fraction.new(7, 9), Fraction.new(3, 5))
false
iex> Fraction.lte?(3, Fraction.new(15, 4))
true
iex> Fraction.lte?(Fraction.new(-24, 12), -2)
true
Multiply two fractions, or a fraction and an integer and return the (optionally simplified) result.
Examples
iex> Fraction.multiply(Fraction.new(3, 7), Fraction.new(22, 7))
%Fraction{num: 66, den: 49}
iex> Fraction.multiply(Fraction.new(2, 9), 33, simplify: true)
%Fraction{num: 22, den: 3}
iex> Fraction.multiply(4, Fraction.new(12, 5))
%Fraction{num: 48, den: 5}
Create a whole number fraction from an integer.
Examples
iex> Fraction.new(4)
%Fraction{num: 4, den: 1}
iex> Fraction.new(-22)
%Fraction{num: -22, den: 1}
Create a new fraction from two integers.
Integers are stored as component numerator and denominator parts, and are only simplified when explicity told to. Fractions representing a negative number will always carry the negative sign on the numerator.
While a denominator with a zero value is undefined, a 0
numerator is acceptable,
and will be manipulated normally.
Examples
iex> Fraction.new(1, 3)
%Fraction{num: 1, den: 3}
iex> Fraction.new(22, -7)
%Fraction{num: -22, den: 7}
iex> Fraction.new(17, 0)
{:error, :invalid_denominator}
iex> Fraction.new(0, 37)
%Fraction{num: 0, den: 37}
Change a combination of fractions, or fractions and integers, into the same denominator.
Example
iex> Fraction.normalize(Fraction.new(3, 4), Fraction.new(1, 7))
{ %Fraction{num: 21, den: 28}, %Fraction{num: 4, den: 28} }
iex> Fraction.normalize(3, Fraction.new(2, 5))
{ %Fraction{num: 15, den: 5}, %Fraction{num: 2, den: 5} }
iex> Fraction.normalize(Fraction.new(6, 4), 2)
{ %Fraction{num: 6, den: 4}, %Fraction{num: 8, den: 4} }
Normalize (move to the same denominator) all of the fractions and integers in a list.
At least one member of the list must be a fraction. Integers will be automatically converted to normalized fractions.
Examples
iex> Fraction.normalize_all([Fraction.new(1, 2), Fraction.new(2, 3), Fraction.new(3, 4), 7])
[
%Fraction{den: 12, num: 6},
%Fraction{den: 12, num: 8},
%Fraction{den: 12, num: 9},
%Fraction{den: 12, num: 84}
]
Generalized integer nth root, from:
https://github.com/acmeism/RosettaCodeData/blob/master/Task/Nth-root/Elixir/nth-root.elixir
based on a fast converging Newton's Method process.
Examples
iex> nth_root(8, 3)
2.0
iex> nth_root(27, 3)
3.0
iex> nth_root(78125, 7)
5.0
Use fractions in power/exponent calculations.
The base or the power (or both) can be fractions.
Fractions taken to an integer power will behave as expected:
iex> Fraction.power(Fraction.new(3, 4), 7)
%Chunky.Fraction{den: 16384, num: 2187}
Fractions or integers taken to a fractional power will not always return expected values, as most fractional powers do not have a result that can be represented as a fraction. By default the power functions will return an error value in these cases:
iex> Fraction.power(9, Fraction.new(7, 13))
{:error, :no_fractional_power}
If you want the floating point fractional result, you can use the allow_irrational
flag:
iex> Fraction.power(9, Fraction.new(7, 13), allow_irrational: true)
3.26454673038995
When calculating the fractional power of a value, an epsilon
value is used as part
of the n-th root finding and resulting analysis The epsilon determines how close to
a whole number different components of the resulting fractions need to be, to be treated
as whole numbers.
Options
simplify
- Boolean. Optionally simplify the return fractionepsilon
- Float. Small number used to compare how close two values areallow_irrational
- Boolean. Allow a non-fractional (irrational) result to be returned
Examples
iex> Fraction.power(Fraction.new(7, 32), Fraction.new(30, 5))
%Fraction{num: 117649, den: 1073741824}
iex> Fraction.power(Fraction.new(3, 5), 3)
%Fraction{num: 27, den: 125}
iex> Fraction.power(4, Fraction.new(-4, 8))
%Fraction{num: 1, den: 2}
iex> Fraction.power(9, Fraction.new(7, 13), allow_irrational: true)
3.26454673038995
Create the reciprocal of a fraction, optionally simplifying the result.
Negative fractions will switch the sign to carry on the new numerator. Trying to take the reciprocal of a zero fraction will result in an error.
Examples
iex> Fraction.new(3, 4) |> Fraction.reciprocal()
%Fraction{num: 4, den: 3}
iex> Fraction.new(-4, 40) |> Fraction.reciprocal(simplify: true)
%Fraction{num: -10, den: 1}
iex> Fraction.new(0, 3) |> Fraction.reciprocal()
{:error, :invalid_denominator}
Simplify a fraction.
Examples
iex> Fraction.new(6, 8) |> Fraction.simplify()
%Fraction{num: 3, den: 4}
iex> Fraction.new(24, 8) |> Fraction.simplify()
%Fraction{num: 3, den: 1}
iex> Fraction.new(22, 7) |> Fraction.simplify()
%Fraction{num: 22, den: 7}
Break a fraction into whole and fractional parts, returning both.
Examples
iex> Fraction.new(6, 4) |> Fraction.split()
{1, %Fraction{num: 2, den: 4}}
iex> Fraction.new(-22, 7) |> Fraction.split()
{-3, %Fraction{num: -1, den: 7}}
iex> Fraction.new(0, 3) |> Fraction.split()
{0, %Fraction{num: 0, den: 3}}
Subtract two fractions, or a fraction an an integer, and return the (optionally) simplified result.
Examples
iex> Fraction.subtract(Fraction.new(3, 5), Fraction.new(1, 5))
%Fraction{num: 2, den: 5}
iex> Fraction.subtract(2, Fraction.new(-6, 9), simplify: true)
%Fraction{num: 8, den: 3}
iex> Fraction.subtract(Fraction.new(-2, 4), -5, simplify: true)
%Fraction{num: 9, den: 2}
Add a series of fractions and integers.
Example
iex> Fraction.sum([Fraction.new(3, 4), Fraction.new(5, 7), Fraction.new(8, 11), 1, Fraction.new(249, 308)])
%Fraction{num: 1232, den: 308}
iex> Fraction.sum([Fraction.new(3, 4), Fraction.new(5, 7), Fraction.new(8, 11), 1, Fraction.new(249, 308)], simplify: true)
%Fraction{num: 4, den: 1}