View Source Rationalize.Search (Rationalize v0.1.1)

Implementation of the search strategy for neaby rational numbers that bracket a given number.

The search for the bracketing rationals involves a traversal of the Stern-Brocot tree. Since the Stern-Brocot tree is essentially a binary tree, the search amounts to a binary search.

Every node in the Stern-Brocot tree is a rational number with relatively prime numerator and denominator (no common factors). It follows that each rational that brackets num has relatively prime components and cannot be further reduced.

Link to this section Summary

Functions

Return the bracket of rational numbers {r1, r2} that most closely bounds the number num, such that both rationals have denominators no larger than integer den_max.

Return the rational fraction closest to the number num with denominator no larger than integer den_max.

Recursively narrow the rational bracket around number num until one of the termination conditions is satisfied.

Link to this section Types

Link to this section Functions

Link to this function

closest_bracket(num, den_max)

View Source
@spec closest_bracket(number(), pos_integer()) :: bracket()

Return the bracket of rational numbers {r1, r2} that most closely bounds the number num, such that both rationals have denominators no larger than integer den_max.

examples

Examples

iex> Rationalize.Search.closest_bracket(0.27, 10)
{%Rationalize.Rational{n: 1, d: 4}, %Rationalize.Rational{n: 2, d: 7}}
Link to this function

closest_rational(num, den_max)

View Source
@spec closest_rational(number(), pos_integer()) :: Rationalize.Rational.t()

Return the rational fraction closest to the number num with denominator no larger than integer den_max.

examples

Examples

iex> Rationalize.closest_rational(0.27, 10)
%Rationalize.Rational{n: 2, d: 7}
Link to this function

search(bracket, num, den_max)

View Source

Recursively narrow the rational bracket around number num until one of the termination conditions is satisfied.

The search terminates when:

  1. either end of the input bracket numerically equals num.
  2. the denominator of the mediant of the input bracket exceeds den_max.
  3. the mediant of the input bracket equals num.