View Source Rationalize.Search (Rationalize v0.1.0)
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
@type bracket() :: {Rationalize.Rational.t(), Rationalize.Rational.t()}
Link to this section Functions
@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}}
@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}
Recursively narrow the rational bracket around number num
until one of the
termination conditions is satisfied.
The search terminates when:
- either end of the input bracket numerically equals
num
. - the denominator of the mediant of the input bracket exceeds
den_max
. - the mediant of the input bracket equals
num
.