Math-Rational-Approx

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN

NAME
    Math::Rational::Approx - approximate a number with a rational number

SYNOPSIS
      use Math::Rational::Approx ':all';

      #
      # find rational approximation with maximum denominator
      ($n, $d, $bounds ) = maxD( $x, 10 );

      # refine; note reuse of $bounds from previous call
      ( $n, $d ) = maxD( $x, 20, $bounds );


      #
      # find rational approximation to arbitrary precision using
      # continued fractions

      # one shot, 10 iterations
      ( $numerator, $denominator ) =
                       contfrac_nd( contfrac( 1.234871035, 10 ) );

      # multiple calls on same number; useful for convergence tests
      # keep array containing terms; get fraction and perhaps test it
      ( $terms, $residual ) = contfrac( 1.234871035, 3 );
      ( $n, $d ) = contfrac_nd( $terms  );

      # continue for an additional number of steps; note reuse of $terms;
      # new terms are appended to it
      ( $terms, $residual ) = contfrac( $residual, 3, $terms );

      # new results
      ( $n, $d ) = contfrac_nd( $terms );

DESCRIPTION
    This module and its object oriented companion modules provide various
    means for finding rational number approximations to real numbers. The
    object oriented versions are suitable when repeated refinements are
    required. See Math::Rational::Approx::MaxD and
    Math::Rational::Approx::ContFrac.

INTERFACE
  Maximum denominator
    maxD finds the best rational approximation (n/d) to a fraction with a
    denominator less than a given value. It uses Farey's sequence and is
    based upon the algorithm given at
    <http://www.johndcook.com/blog/2010/10/20/best-rational-approximation/>.

    This is an iterative procedure, searching a given range for the best
    approximation. To enable further refinement, the limiting denominator
    may by adjusted; the approximation will be continued from the last
    calculation.

    maxD
          ( $n, $d, $bounds ) = maxD( $x, $maxD, );
          ( $n, $d, $bounds ) = maxD( $x, $maxD, $bounds );

        Calculate the rational number approximation to $x with denominator
        no greater than $maxD.

        The optional argument, $bounds, is a reference to a four element
        array containing the initial bounds on the region to search. It
        takes the form

          bounds => [ a, b, c, d ]

        where the elements are all non-negative integers and the bounds are
        given by

          a/b < x < c/d
          b < maxD && d < maxD

        The behavior is undefined if the latter condition is not yet, unless
        the bounds are the result of a previous run of maxD.

        By default it searches from float(x) to "float(x)+1".

        maxD returns the determined numerator and denominator as well as an
        arrayref containing the bounds. If the $bounds argument was



( run in 1.297 second using v1.01-cache-2.11-cpan-71847e10f99 )