Math-BigInt

 view release on metacpan or  search on metacpan

lib/Math/BigInt/Calc.pm  view on Meta::CPAN

}

sub _nok {
    # Return binomial coefficient (n over k).
    # Given refs to arrays, return ref to array.
    # First input argument is modified.

    my ($c, $n, $k) = @_;

    # If k > n/2, or, equivalently, 2*k > n, compute nok(n, k) as
    # nok(n, n-k), to minimize the number if iterations in the loop.

    {
        my $twok = $c->_mul($c->_two(), $c->_copy($k)); # 2 * k
        if ($c->_acmp($twok, $n) > 0) {               # if 2*k > n
            $k = $c->_sub($c->_copy($n), $k);         # k = n - k
        }
    }

    # Example:
    #

lib/Math/BigInt/Lib.pm  view on Meta::CPAN


    $x = $class -> _mul($x, $pow2);
    return $x;
}

sub _nok {
    # Return binomial coefficient (n over k).
    my ($class, $n, $k) = @_;

    # If k > n/2, or, equivalently, 2*k > n, compute nok(n, k) as
    # nok(n, n-k), to minimize the number if iterations in the loop.

    {
        my $twok = $class -> _mul($class -> _two(), $class -> _copy($k));
        if ($class -> _acmp($twok, $n) > 0) {
            $k = $class -> _sub($class -> _copy($n), $k);
        }
    }

    # Example:
    #

lib/Math/BigInt/Lib.pm  view on Meta::CPAN

        print "xm     = $xm\n";
        print "xe     = $xe\n";
    }

    # If the mantissa is not an integer, round up to nearest integer, and then
    # convert the number to a string. It is important to always round up due to
    # how Newton's method behaves in this case. If the initial guess is too
    # small, the next guess will be too large, after which every succeeding
    # guess converges the correct value from above. Now, if the initial guess
    # is too small and n is large, the next guess will be much too large and
    # require a large number of iterations to get close to the solution.
    # Because of this, we are likely to find the solution faster if we make
    # sure the initial guess is not too small.

    my $xm_int = int($xm);
    my $x_str = sprintf '%.0f', $xm > $xm_int ? $xm_int + 1 : $xm_int;
    $x_str .= "0" x $xe;

    my $x = $class -> _new($x_str);

    if ($DEBUG) {

lib/Math/BigRat.pm  view on Meta::CPAN


    # The accuracy, i.e., the number of digits. Pi has one digit before the
    # dot, so a precision of 4 digits is equivalent to an accuracy of 5 digits.

    my $n = defined $r[0] ? $r[0]
          : defined $r[1] ? 1 - $r[1]
          : $self -> div_scale();

    # The algorithm below creates a fraction from a floating point number. The
    # worst case is the number (1 + sqrt(5))/2 (golden ratio), which takes
    # almost 2.4*N iterations to find a fraction that is accurate to N digits,
    # i.e., the relative error is less than 10**(-N).
    #
    # This algorithm might be useful in general, so it should probably be moved
    # out to a method of its own. XXX

    my $max_iter = $n * 2.4;

    my $x = Math::BigFloat -> bpi($n + 10);

    my $tol = $class -> new("1/10") -> bpow("$n") -> bmul($x);



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