<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">
#
# "Tax the rat farms." - Lord Vetinari
#

# The following hash values are used:
#   sign : +,-,NaN,+inf,-inf
#   _d   : denominator
#   _n   : numerator (value = _n/_d)
#   _a   : accuracy
#   _p   : precision
# You should not look at the innards of a BigRat - use the methods for this.

package Math::BigRat;

# anything older is untested, and unlikely to work
use 5.006;
use strict;
use Carp ();

use Math::BigFloat;
use vars qw($VERSION @ISA $upgrade $downgrade
            $accuracy $precision $round_mode $div_scale $_trap_nan $_trap_inf);

@ISA = qw(Math::BigFloat);

$VERSION = '0.2603';
$VERSION = eval $VERSION;

# inherit overload from Math::BigFloat, but disable the bitwise ops that don't
# make much sense for rationals unless they're truncated or something first

use overload
    map {
	my $op = $_;
	($op =&gt; sub {
	    Carp::croak("bitwise operation $op not supported in Math::BigRat");
	});
    } qw(&amp; | ^ ~ &lt;&lt; &gt;&gt; &amp;= |= ^= &lt;&lt;= &gt;&gt;=);

BEGIN
  {
  *objectify = \&amp;Math::BigInt::objectify; 	# inherit this from BigInt
  *AUTOLOAD = \&amp;Math::BigFloat::AUTOLOAD;	# can't inherit AUTOLOAD
  # we inherit these from BigFloat because currently it is not possible
  # that MBF has a different $MBI variable than we, because MBF also uses
  # Math::BigInt::config-&gt;('lib'); (there is always only one library loaded)
  *_e_add = \&amp;Math::BigFloat::_e_add;
  *_e_sub = \&amp;Math::BigFloat::_e_sub;
  *as_int = \&amp;as_number;
  *is_pos = \&amp;is_positive;
  *is_neg = \&amp;is_negative;
  }

##############################################################################
# Global constants and flags. Access these only via the accessor methods!

$accuracy = $precision = undef;
$round_mode = 'even';
$div_scale = 40;
$upgrade = undef;
$downgrade = undef;

# These are internally, and not to be used from the outside at all!

$_trap_nan = 0;                         # are NaNs ok? set w/ config()
$_trap_inf = 0;                         # are infs ok? set w/ config()

# the package we are using for our private parts, defaults to:
# Math::BigInt-&gt;config()-&gt;{lib}
my $MBI = 'Math::BigInt::Calc';

my $nan = 'NaN';
my $class = 'Math::BigRat';

sub isa
  {
  return 0 if $_[1] =~ /^Math::Big(Int|Float)/;		# we aren't
  UNIVERSAL::isa(@_);
  }

##############################################################################

sub _new_from_float
  {
  # turn a single float input into a rational number (like '0.1')
  my ($self,$f) = @_;

  return $self-&gt;bnan() if $f-&gt;is_nan();
  return $self-&gt;binf($f-&gt;{sign}) if $f-&gt;{sign} =~ /^[+-]inf$/;

  $self-&gt;{_n} = $MBI-&gt;_copy( $f-&gt;{_m} );	# mantissa
  $self-&gt;{_d} = $MBI-&gt;_one();
  $self-&gt;{sign} = $f-&gt;{sign} || '+';
  if ($f-&gt;{_es} eq '-')
    {
    # something like Math::BigRat-&gt;new('0.1');
    # 1 / 1 =&gt; 1/10
    $MBI-&gt;_lsft ( $self-&gt;{_d}, $f-&gt;{_e} ,10);
    }
  else
    {
    # something like Math::BigRat-&gt;new('10');
    # 1 / 1 =&gt; 10/1
    $MBI-&gt;_lsft ( $self-&gt;{_n}, $f-&gt;{_e} ,10) unless
      $MBI-&gt;_is_zero($f-&gt;{_e});
    }
  $self;
  }

sub new
  {
  # create a Math::BigRat
  my $class = shift;

  my ($n,$d) = @_;

  my $self = { }; bless $self,$class;

  # input like (BigInt) or (BigFloat):
  if ((!defined $d) &amp;&amp; (ref $n) &amp;&amp; (!$n-&gt;isa('Math::BigRat')))
    {
    if ($n-&gt;isa('Math::BigFloat'))
      {
      $self-&gt;_new_from_float($n);
      }
    if ($n-&gt;isa('Math::BigInt'))
      {
      # TODO: trap NaN, inf
      $self-&gt;{_n} = $MBI-&gt;_copy($n-&gt;{value});		# "mantissa" = N
      $self-&gt;{_d} = $MBI-&gt;_one();			# d =&gt; 1
      $self-&gt;{sign} = $n-&gt;{sign};
      }
    if ($n-&gt;isa('Math::BigInt::Lite'))
      {
      # TODO: trap NaN, inf
      $self-&gt;{sign} = '+'; $self-&gt;{sign} = '-' if $$n &lt; 0;
      $self-&gt;{_n} = $MBI-&gt;_new(abs($$n));		# "mantissa" = N
      $self-&gt;{_d} = $MBI-&gt;_one();			# d =&gt; 1
      }
    return $self-&gt;bnorm();				# normalize (120/1 =&gt; 12/10)
    }

  # input like (BigInt,BigInt) or (BigLite,BigLite):
  if (ref($d) &amp;&amp; ref($n))
    {
    # do N first (for $self-&gt;{sign}):
    if ($n-&gt;isa('Math::BigInt'))
      {
      # TODO: trap NaN, inf
      $self-&gt;{_n} = $MBI-&gt;_copy($n-&gt;{value});		# "mantissa" = N
      $self-&gt;{sign} = $n-&gt;{sign};
      }
    elsif ($n-&gt;isa('Math::BigInt::Lite'))
      {
      # TODO: trap NaN, inf
      $self-&gt;{sign} = '+'; $self-&gt;{sign} = '-' if $$n &lt; 0;
      $self-&gt;{_n} = $MBI-&gt;_new(abs($$n));		# "mantissa" = $n
      }
    else
      {
      require Carp;
      Carp::croak(ref($n) . " is not a recognized object format for Math::BigRat-&gt;new");
      }
    # now D:
    if ($d-&gt;isa('Math::BigInt'))
      {
      # TODO: trap NaN, inf
      $self-&gt;{_d} = $MBI-&gt;_copy($d-&gt;{value});		# "mantissa" = D
      # +/+ or -/- =&gt; +, +/- or -/+ =&gt; -
      $self-&gt;{sign} = $d-&gt;{sign} ne $self-&gt;{sign} ? '-' : '+';
      }
    elsif ($d-&gt;isa('Math::BigInt::Lite'))
      {
      # TODO: trap NaN, inf
      $self-&gt;{_d} = $MBI-&gt;_new(abs($$d));		# "mantissa" = D
      my $ds = '+'; $ds = '-' if $$d &lt; 0;
      # +/+ or -/- =&gt; +, +/- or -/+ =&gt; -
      $self-&gt;{sign} = $ds ne $self-&gt;{sign} ? '-' : '+';
      }
    else
      {
      require Carp;
      Carp::croak(ref($d) . " is not a recognized object format for Math::BigRat-&gt;new");
      }
    return $self-&gt;bnorm();				# normalize (120/1 =&gt; 12/10)
    }
  return $n-&gt;copy() if ref $n;				# already a BigRat

  if (!defined $n)
    {
    $self-&gt;{_n} = $MBI-&gt;_zero();			# undef =&gt; 0
    $self-&gt;{_d} = $MBI-&gt;_one();
    $self-&gt;{sign} = '+';
    return $self;
    }

  # string input with / delimiter
  if ($n =~ /\s*\/\s*/)
    {
    return $class-&gt;bnan() if $n =~ /\/.*\//;	# 1/2/3 isn't valid
    return $class-&gt;bnan() if $n =~ /\/\s*$/;	# 1/ isn't valid
    ($n,$d) = split (/\//,$n);
    # try as BigFloats first
    if (($n =~ /[\.eE]/) || ($d =~ /[\.eE]/))
      {
      local $Math::BigFloat::accuracy = undef;
      local $Math::BigFloat::precision = undef;

      # one of them looks like a float
      my $nf = Math::BigFloat-&gt;new($n,undef,undef);
      $self-&gt;{sign} = '+';
      return $self-&gt;bnan() if $nf-&gt;is_nan();

      $self-&gt;{_n} = $MBI-&gt;_copy( $nf-&gt;{_m} );	# get mantissa

      # now correct $self-&gt;{_n} due to $n
      my $f = Math::BigFloat-&gt;new($d,undef,undef);
      return $self-&gt;bnan() if $f-&gt;is_nan();
      $self-&gt;{_d} = $MBI-&gt;_copy( $f-&gt;{_m} );

      # calculate the difference between nE and dE
      my $diff_e = $nf-&gt;exponent()-&gt;bsub( $f-&gt;exponent);
      if ($diff_e-&gt;is_negative())
	{
        # &lt; 0: mul d with it
        $MBI-&gt;_lsft( $self-&gt;{_d}, $MBI-&gt;_new( $diff_e-&gt;babs()), 10);
	}
      elsif (!$diff_e-&gt;is_zero())
        {
        # &gt; 0: mul n with it
        $MBI-&gt;_lsft( $self-&gt;{_n}, $MBI-&gt;_new( $diff_e), 10);
        }
      }
    else
      {
      # both d and n look like (big)ints

      $self-&gt;{sign} = '+';					# no sign =&gt; '+'
      $self-&gt;{_n} = undef;
      $self-&gt;{_d} = undef;
      if ($n =~ /^([+-]?)0*([0-9]+)\z/)				# first part ok?
	{
	$self-&gt;{sign} = $1 || '+';				# no sign =&gt; '+'
	$self-&gt;{_n} = $MBI-&gt;_new($2 || 0);
        }

      if ($d =~ /^([+-]?)0*([0-9]+)\z/)				# second part ok?
	{
	$self-&gt;{sign} =~ tr/+-/-+/ if ($1 || '') eq '-';	# negate if second part neg.
	$self-&gt;{_d} = $MBI-&gt;_new($2 || 0);
        }

      if (!defined $self-&gt;{_n} || !defined $self-&gt;{_d})
	{
        $d = Math::BigInt-&gt;new($d,undef,undef) unless ref $d;
        $n = Math::BigInt-&gt;new($n,undef,undef) unless ref $n;

        if ($n-&gt;{sign} =~ /^[+-]$/ &amp;&amp; $d-&gt;{sign} =~ /^[+-]$/)
	  {
	  # both parts are ok as integers (wierd things like ' 1e0'
          $self-&gt;{_n} = $MBI-&gt;_copy($n-&gt;{value});
          $self-&gt;{_d} = $MBI-&gt;_copy($d-&gt;{value});
          $self-&gt;{sign} = $n-&gt;{sign};
          $self-&gt;{sign} =~ tr/+-/-+/ if $d-&gt;{sign} eq '-';	# -1/-2 =&gt; 1/2
          return $self-&gt;bnorm();
	  }

        $self-&gt;{sign} = '+';					# a default sign
        return $self-&gt;bnan() if $n-&gt;is_nan() || $d-&gt;is_nan();

	# handle inf cases:
        if ($n-&gt;is_inf() || $d-&gt;is_inf())
	  {
	  if ($n-&gt;is_inf())
	    {
	    return $self-&gt;bnan() if $d-&gt;is_inf();		# both are inf =&gt; NaN
	    my $s = '+'; 		# '+inf/+123' or '-inf/-123'
	    $s = '-' if substr($n-&gt;{sign},0,1) ne $d-&gt;{sign};
	    # +-inf/123 =&gt; +-inf
	    return $self-&gt;binf($s);
	    }
          # 123/inf =&gt; 0
          return $self-&gt;bzero();
	  }
	}
      }

    return $self-&gt;bnorm();
    }

  # simple string input
  if (($n =~ /[\.eE]/) &amp;&amp; $n !~ /^0x/)
    {
    # looks like a float, quacks like a float, so probably is a float
    $self-&gt;{sign} = 'NaN';
    local $Math::BigFloat::accuracy = undef;
    local $Math::BigFloat::precision = undef;
    $self-&gt;_new_from_float(Math::BigFloat-&gt;new($n,undef,undef));
    }
  else
    {
    # for simple forms, use $MBI directly
    if ($n =~ /^([+-]?)0*([0-9]+)\z/)
      {
      $self-&gt;{sign} = $1 || '+';
      $self-&gt;{_n} = $MBI-&gt;_new($2 || 0);
      $self-&gt;{_d} = $MBI-&gt;_one();
      }
    else
      {
      my $n = Math::BigInt-&gt;new($n,undef,undef);
      $self-&gt;{_n} = $MBI-&gt;_copy($n-&gt;{value});
      $self-&gt;{_d} = $MBI-&gt;_one();
      $self-&gt;{sign} = $n-&gt;{sign};
      return $self-&gt;bnan() if $self-&gt;{sign} eq 'NaN';
      return $self-&gt;binf($self-&gt;{sign}) if $self-&gt;{sign} =~ /^[+-]inf$/;
      }
    }
  $self-&gt;bnorm();
  }

sub copy
  {
  # if two arguments, the first one is the class to "swallow" subclasses
  my ($c,$x) = @_;

  if (scalar @_ == 1)
    {
    $x = $_[0];
    $c = ref($x);
    }
  return unless ref($x); # only for objects

  my $self = bless {}, $c;

  $self-&gt;{sign} = $x-&gt;{sign};
  $self-&gt;{_d} = $MBI-&gt;_copy($x-&gt;{_d});
  $self-&gt;{_n} = $MBI-&gt;_copy($x-&gt;{_n});
  $self-&gt;{_a} = $x-&gt;{_a} if defined $x-&gt;{_a};
  $self-&gt;{_p} = $x-&gt;{_p} if defined $x-&gt;{_p};
  $self;
  }

##############################################################################

sub config
  {
  # return (later set?) configuration data as hash ref
  my $class = shift || 'Math::BigRat';

  if (@_ == 1 &amp;&amp; ref($_[0]) ne 'HASH')
    {
    my $cfg = $class-&gt;SUPER::config();
    return $cfg-&gt;{$_[0]};
    }

  my $cfg = $class-&gt;SUPER::config(@_);

  # now we need only to override the ones that are different from our parent
  $cfg-&gt;{class} = $class;
  $cfg-&gt;{with} = $MBI;
  $cfg;
  }

##############################################################################

sub bstr
  {
  my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_);

  if ($x-&gt;{sign} !~ /^[+-]$/)		# inf, NaN etc
    {
    my $s = $x-&gt;{sign}; $s =~ s/^\+//; 	# +inf =&gt; inf
    return $s;
    }

  my $s = ''; $s = $x-&gt;{sign} if $x-&gt;{sign} ne '+';	# '+3/2' =&gt; '3/2'

  return $s . $MBI-&gt;_str($x-&gt;{_n}) if $MBI-&gt;_is_one($x-&gt;{_d});
  $s . $MBI-&gt;_str($x-&gt;{_n}) . '/' . $MBI-&gt;_str($x-&gt;{_d});
  }

sub bsstr
  {
  my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_);

  if ($x-&gt;{sign} !~ /^[+-]$/)		# inf, NaN etc
    {
    my $s = $x-&gt;{sign}; $s =~ s/^\+//; 	# +inf =&gt; inf
    return $s;
    }

  my $s = ''; $s = $x-&gt;{sign} if $x-&gt;{sign} ne '+';	# +3 vs 3
  $s . $MBI-&gt;_str($x-&gt;{_n}) . '/' . $MBI-&gt;_str($x-&gt;{_d});
  }

sub bnorm
  {
  # reduce the number to the shortest form
  my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_);

  # Both parts must be objects of whatever we are using today.
  if ( my $c = $MBI-&gt;_check($x-&gt;{_n}) )
    {
    require Carp; Carp::croak ("n did not pass the self-check ($c) in bnorm()");
    }
  if ( my $c = $MBI-&gt;_check($x-&gt;{_d}) )
    {
    require Carp; Carp::croak ("d did not pass the self-check ($c) in bnorm()");
    }

  # no normalize for NaN, inf etc.
  return $x if $x-&gt;{sign} !~ /^[+-]$/;

  # normalize zeros to 0/1
  if ($MBI-&gt;_is_zero($x-&gt;{_n}))
    {
    $x-&gt;{sign} = '+';					# never leave a -0
    $x-&gt;{_d} = $MBI-&gt;_one() unless $MBI-&gt;_is_one($x-&gt;{_d});
    return $x;
    }

  return $x if $MBI-&gt;_is_one($x-&gt;{_d});			# no need to reduce

  # reduce other numbers
  my $gcd = $MBI-&gt;_copy($x-&gt;{_n});
  $gcd = $MBI-&gt;_gcd($gcd,$x-&gt;{_d});

  if (!$MBI-&gt;_is_one($gcd))
    {
    $x-&gt;{_n} = $MBI-&gt;_div($x-&gt;{_n},$gcd);
    $x-&gt;{_d} = $MBI-&gt;_div($x-&gt;{_d},$gcd);
    }
  $x;
  }

##############################################################################
# sign manipulation

sub bneg
  {
  # (BRAT or num_str) return BRAT
  # negate number or make a negated number from string
  my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_);

  return $x if $x-&gt;modify('bneg');

  # for +0 dont negate (to have always normalized +0). Does nothing for 'NaN'
  $x-&gt;{sign} =~ tr/+-/-+/ unless ($x-&gt;{sign} eq '+' &amp;&amp; $MBI-&gt;_is_zero($x-&gt;{_n}));
  $x;
  }

##############################################################################
# special values

sub _bnan
  {
  # used by parent class bnan() to initialize number to NaN
  my $self = shift;

  if ($_trap_nan)
    {
    require Carp;
    my $class = ref($self);
    # "$self" below will stringify the object, this blows up if $self is a
    # partial object (happens under trap_nan), so fix it beforehand
    $self-&gt;{_d} = $MBI-&gt;_zero() unless defined $self-&gt;{_d};
    $self-&gt;{_n} = $MBI-&gt;_zero() unless defined $self-&gt;{_n};
    Carp::croak ("Tried to set $self to NaN in $class\::_bnan()");
    }
  $self-&gt;{_n} = $MBI-&gt;_zero();
  $self-&gt;{_d} = $MBI-&gt;_zero();
  }

sub _binf
  {
  # used by parent class bone() to initialize number to +inf/-inf
  my $self = shift;

  if ($_trap_inf)
    {
    require Carp;
    my $class = ref($self);
    # "$self" below will stringify the object, this blows up if $self is a
    # partial object (happens under trap_nan), so fix it beforehand
    $self-&gt;{_d} = $MBI-&gt;_zero() unless defined $self-&gt;{_d};
    $self-&gt;{_n} = $MBI-&gt;_zero() unless defined $self-&gt;{_n};
    Carp::croak ("Tried to set $self to inf in $class\::_binf()");
    }
  $self-&gt;{_n} = $MBI-&gt;_zero();
  $self-&gt;{_d} = $MBI-&gt;_zero();
  }

sub _bone
  {
  # used by parent class bone() to initialize number to +1/-1
  my $self = shift;
  $self-&gt;{_n} = $MBI-&gt;_one();
  $self-&gt;{_d} = $MBI-&gt;_one();
  }

sub _bzero
  {
  # used by parent class bzero() to initialize number to 0
  my $self = shift;
  $self-&gt;{_n} = $MBI-&gt;_zero();
  $self-&gt;{_d} = $MBI-&gt;_one();
  }

##############################################################################
# mul/add/div etc

sub badd
  {
  # add two rational numbers

  # set up parameters
  my ($self,$x,$y,@r) = (ref($_[0]),@_);
  # objectify is costly, so avoid it
  if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1])))
    {
    ($self,$x,$y,@r) = objectify(2,@_);
    }

  # +inf + +inf =&gt; +inf,  -inf + -inf =&gt; -inf
  return $x-&gt;binf(substr($x-&gt;{sign},0,1))
    if $x-&gt;{sign} eq $y-&gt;{sign} &amp;&amp; $x-&gt;{sign} =~ /^[+-]inf$/;

  # +inf + -inf or -inf + +inf =&gt; NaN
  return $x-&gt;bnan() if ($x-&gt;{sign} !~ /^[+-]$/ || $y-&gt;{sign} !~ /^[+-]$/);

  #  1   1    gcd(3,4) = 1    1*3 + 1*4    7
  #  - + -                  = --------- = --
  #  4   3                      4*3       12

  # we do not compute the gcd() here, but simple do:
  #  5   7    5*3 + 7*4   43
  #  - + -  = --------- = --
  #  4   3       4*3      12

  # and bnorm() will then take care of the rest

  # 5 * 3
  $x-&gt;{_n} = $MBI-&gt;_mul( $x-&gt;{_n}, $y-&gt;{_d});

  # 7 * 4
  my $m = $MBI-&gt;_mul( $MBI-&gt;_copy( $y-&gt;{_n} ), $x-&gt;{_d} );

  # 5 * 3 + 7 * 4
  ($x-&gt;{_n}, $x-&gt;{sign}) = _e_add( $x-&gt;{_n}, $m, $x-&gt;{sign}, $y-&gt;{sign});

  # 4 * 3
  $x-&gt;{_d} = $MBI-&gt;_mul( $x-&gt;{_d}, $y-&gt;{_d});

  # normalize result, and possible round
  $x-&gt;bnorm()-&gt;round(@r);
  }

sub bsub
  {
  # subtract two rational numbers

  # set up parameters
  my ($self,$x,$y,@r) = (ref($_[0]),@_);
  # objectify is costly, so avoid it
  if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1])))
    {
    ($self,$x,$y,@r) = objectify(2,@_);
    }

  # flip sign of $x, call badd(), then flip sign of result
  $x-&gt;{sign} =~ tr/+-/-+/
    unless $x-&gt;{sign} eq '+' &amp;&amp; $MBI-&gt;_is_zero($x-&gt;{_n});	# not -0
  $x-&gt;badd($y,@r);				# does norm and round
  $x-&gt;{sign} =~ tr/+-/-+/
    unless $x-&gt;{sign} eq '+' &amp;&amp; $MBI-&gt;_is_zero($x-&gt;{_n});	# not -0
  $x;
  }

sub bmul
  {
  # multiply two rational numbers

  # set up parameters
  my ($self,$x,$y,@r) = (ref($_[0]),@_);
  # objectify is costly, so avoid it
  if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1])))
    {
    ($self,$x,$y,@r) = objectify(2,@_);
    }

  return $x-&gt;bnan() if ($x-&gt;{sign} eq 'NaN' || $y-&gt;{sign} eq 'NaN');

  # inf handling
  if (($x-&gt;{sign} =~ /^[+-]inf$/) || ($y-&gt;{sign} =~ /^[+-]inf$/))
    {
    return $x-&gt;bnan() if $x-&gt;is_zero() || $y-&gt;is_zero();
    # result will always be +-inf:
    # +inf * +/+inf =&gt; +inf, -inf * -/-inf =&gt; +inf
    # +inf * -/-inf =&gt; -inf, -inf * +/+inf =&gt; -inf
    return $x-&gt;binf() if ($x-&gt;{sign} =~ /^\+/ &amp;&amp; $y-&gt;{sign} =~ /^\+/);
    return $x-&gt;binf() if ($x-&gt;{sign} =~ /^-/ &amp;&amp; $y-&gt;{sign} =~ /^-/);
    return $x-&gt;binf('-');
    }

  # x== 0 # also: or y == 1 or y == -1
  return wantarray ? ($x,$self-&gt;bzero()) : $x if $x-&gt;is_zero();

  # XXX TODO:
  # According to Knuth, this can be optimized by doing gcd twice (for d and n)
  # and reducing in one step. This would save us the bnorm() at the end.

  #  1   2    1 * 2    2    1
  #  - * - =  -----  = -  = -
  #  4   3    4 * 3    12   6

  $x-&gt;{_n} = $MBI-&gt;_mul( $x-&gt;{_n}, $y-&gt;{_n});
  $x-&gt;{_d} = $MBI-&gt;_mul( $x-&gt;{_d}, $y-&gt;{_d});

  # compute new sign
  $x-&gt;{sign} = $x-&gt;{sign} eq $y-&gt;{sign} ? '+' : '-';

  $x-&gt;bnorm()-&gt;round(@r);
  }

sub bdiv
  {
  # (dividend: BRAT or num_str, divisor: BRAT or num_str) return
  # (BRAT,BRAT) (quo,rem) or BRAT (only rem)

  # set up parameters
  my ($self,$x,$y,@r) = (ref($_[0]),@_);
  # objectify is costly, so avoid it
  if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1])))
    {
    ($self,$x,$y,@r) = objectify(2,@_);
    }

  return $self-&gt;_div_inf($x,$y)
   if (($x-&gt;{sign} !~ /^[+-]$/) || ($y-&gt;{sign} !~ /^[+-]$/) || $y-&gt;is_zero());

  # x== 0 # also: or y == 1 or y == -1
  return wantarray ? ($x,$self-&gt;bzero()) : $x if $x-&gt;is_zero();

  # XXX TODO: list context, upgrade
  # According to Knuth, this can be optimized by doing gcd twice (for d and n)
  # and reducing in one step. This would save us the bnorm() at the end.

  # 1     1    1   3
  # -  /  - == - * -
  # 4     3    4   1

  $x-&gt;{_n} = $MBI-&gt;_mul( $x-&gt;{_n}, $y-&gt;{_d});
  $x-&gt;{_d} = $MBI-&gt;_mul( $x-&gt;{_d}, $y-&gt;{_n});

  # compute new sign
  $x-&gt;{sign} = $x-&gt;{sign} eq $y-&gt;{sign} ? '+' : '-';

  $x-&gt;bnorm()-&gt;round(@r);
  $x;
  }

sub bmod
  {
  # compute "remainder" (in Perl way) of $x / $y

  # set up parameters
  my ($self,$x,$y,@r) = (ref($_[0]),@_);
  # objectify is costly, so avoid it
  if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1])))
    {
    ($self,$x,$y,@r) = objectify(2,@_);
    }

  return $self-&gt;_div_inf($x,$y)
   if (($x-&gt;{sign} !~ /^[+-]$/) || ($y-&gt;{sign} !~ /^[+-]$/) || $y-&gt;is_zero());

  return $x if $x-&gt;is_zero();           # 0 / 7 = 0, mod 0

  # compute $x - $y * floor($x/$y), keeping the sign of $x

  # copy x to u, make it positive and then do a normal division ($u/$y)
  my $u = bless { sign =&gt; '+' }, $self;
  $u-&gt;{_n} = $MBI-&gt;_mul( $MBI-&gt;_copy($x-&gt;{_n}), $y-&gt;{_d} );
  $u-&gt;{_d} = $MBI-&gt;_mul( $MBI-&gt;_copy($x-&gt;{_d}), $y-&gt;{_n} );

  # compute floor(u)
  if (! $MBI-&gt;_is_one($u-&gt;{_d}))
    {
    $u-&gt;{_n} = $MBI-&gt;_div($u-&gt;{_n},$u-&gt;{_d});	# 22/7 =&gt; 3/1 w/ truncate
    # no need to set $u-&gt;{_d} to 1, since below we set it to $y-&gt;{_d} anyway
    }

  # now compute $y * $u
  $u-&gt;{_d} = $MBI-&gt;_copy($y-&gt;{_d});		# 1 * $y-&gt;{_d}, see floor above
  $u-&gt;{_n} = $MBI-&gt;_mul($u-&gt;{_n},$y-&gt;{_n});

  my $xsign = $x-&gt;{sign}; $x-&gt;{sign} = '+';	# remember sign and make x positive
  # compute $x - $u
  $x-&gt;bsub($u);
  $x-&gt;{sign} = $xsign;				# put sign back

  $x-&gt;bnorm()-&gt;round(@r);
  }

##############################################################################
# bdec/binc

sub bdec
  {
  # decrement value (subtract 1)
  my ($self,$x,@r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_);

  return $x if $x-&gt;{sign} !~ /^[+-]$/;	# NaN, inf, -inf

  if ($x-&gt;{sign} eq '-')
    {
    $x-&gt;{_n} = $MBI-&gt;_add( $x-&gt;{_n}, $x-&gt;{_d});		# -5/2 =&gt; -7/2
    }
  else
    {
    if ($MBI-&gt;_acmp($x-&gt;{_n},$x-&gt;{_d}) &lt; 0)		# n &lt; d?
      {
      # 1/3 -- =&gt; -2/3
      $x-&gt;{_n} = $MBI-&gt;_sub( $MBI-&gt;_copy($x-&gt;{_d}), $x-&gt;{_n});
      $x-&gt;{sign} = '-';
      }
    else
      {
      $x-&gt;{_n} = $MBI-&gt;_sub($x-&gt;{_n}, $x-&gt;{_d}); 	# 5/2 =&gt; 3/2
      }
    }
  $x-&gt;bnorm()-&gt;round(@r);
  }

sub binc
  {
  # increment value (add 1)
  my ($self,$x,@r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_);

  return $x if $x-&gt;{sign} !~ /^[+-]$/;	# NaN, inf, -inf

  if ($x-&gt;{sign} eq '-')
    {
    if ($MBI-&gt;_acmp($x-&gt;{_n},$x-&gt;{_d}) &lt; 0)
      {
      # -1/3 ++ =&gt; 2/3 (overflow at 0)
      $x-&gt;{_n} = $MBI-&gt;_sub( $MBI-&gt;_copy($x-&gt;{_d}), $x-&gt;{_n});
      $x-&gt;{sign} = '+';
      }
    else
      {
      $x-&gt;{_n} = $MBI-&gt;_sub($x-&gt;{_n}, $x-&gt;{_d}); 	# -5/2 =&gt; -3/2
      }
    }
  else
    {
    $x-&gt;{_n} = $MBI-&gt;_add($x-&gt;{_n},$x-&gt;{_d});		# 5/2 =&gt; 7/2
    }
  $x-&gt;bnorm()-&gt;round(@r);
  }

##############################################################################
# is_foo methods (the rest is inherited)

sub is_int
  {
  # return true if arg (BRAT or num_str) is an integer
  my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_);

  return 1 if ($x-&gt;{sign} =~ /^[+-]$/) &amp;&amp;	# NaN and +-inf aren't
    $MBI-&gt;_is_one($x-&gt;{_d});			# x/y &amp;&amp; y != 1 =&gt; no integer
  0;
  }

sub is_zero
  {
  # return true if arg (BRAT or num_str) is zero
  my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_);

  return 1 if $x-&gt;{sign} eq '+' &amp;&amp; $MBI-&gt;_is_zero($x-&gt;{_n});
  0;
  }

sub is_one
  {
  # return true if arg (BRAT or num_str) is +1 or -1 if signis given
  my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_);

  my $sign = $_[2] || ''; $sign = '+' if $sign ne '-';
  return 1
   if ($x-&gt;{sign} eq $sign &amp;&amp; $MBI-&gt;_is_one($x-&gt;{_n}) &amp;&amp; $MBI-&gt;_is_one($x-&gt;{_d}));
  0;
  }

sub is_odd
  {
  # return true if arg (BFLOAT or num_str) is odd or false if even
  my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_);

  return 1 if ($x-&gt;{sign} =~ /^[+-]$/) &amp;&amp;		# NaN &amp; +-inf aren't
    ($MBI-&gt;_is_one($x-&gt;{_d}) &amp;&amp; $MBI-&gt;_is_odd($x-&gt;{_n})); # x/2 is not, but 3/1
  0;
  }

sub is_even
  {
  # return true if arg (BINT or num_str) is even or false if odd
  my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_);

  return 0 if $x-&gt;{sign} !~ /^[+-]$/;			# NaN &amp; +-inf aren't
  return 1 if ($MBI-&gt;_is_one($x-&gt;{_d})			# x/3 is never
     &amp;&amp; $MBI-&gt;_is_even($x-&gt;{_n}));			# but 4/1 is
  0;
  }

##############################################################################
# parts() and friends

sub numerator
  {
  my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_);

  # NaN, inf, -inf
  return Math::BigInt-&gt;new($x-&gt;{sign}) if ($x-&gt;{sign} !~ /^[+-]$/);

  my $n = Math::BigInt-&gt;new($MBI-&gt;_str($x-&gt;{_n})); $n-&gt;{sign} = $x-&gt;{sign};
  $n;
  }

sub denominator
  {
  my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_);

  # NaN
  return Math::BigInt-&gt;new($x-&gt;{sign}) if $x-&gt;{sign} eq 'NaN';
  # inf, -inf
  return Math::BigInt-&gt;bone() if $x-&gt;{sign} !~ /^[+-]$/;

  Math::BigInt-&gt;new($MBI-&gt;_str($x-&gt;{_d}));
  }

sub parts
  {
  my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_);

  my $c = 'Math::BigInt';

  return ($c-&gt;bnan(),$c-&gt;bnan()) if $x-&gt;{sign} eq 'NaN';
  return ($c-&gt;binf(),$c-&gt;binf()) if $x-&gt;{sign} eq '+inf';
  return ($c-&gt;binf('-'),$c-&gt;binf()) if $x-&gt;{sign} eq '-inf';

  my $n = $c-&gt;new( $MBI-&gt;_str($x-&gt;{_n}));
  $n-&gt;{sign} = $x-&gt;{sign};
  my $d = $c-&gt;new( $MBI-&gt;_str($x-&gt;{_d}));
  ($n,$d);
  }

sub length
  {
  my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_);

  return $nan unless $x-&gt;is_int();
  $MBI-&gt;_len($x-&gt;{_n});				# length(-123/1) =&gt; length(123)
  }

sub digit
  {
  my ($self,$x,$n) = ref($_[0]) ? (undef,$_[0],$_[1]) : objectify(1,@_);

  return $nan unless $x-&gt;is_int();
  $MBI-&gt;_digit($x-&gt;{_n},$n || 0);		# digit(-123/1,2) =&gt; digit(123,2)
  }

##############################################################################
# special calc routines

sub bceil
  {
  my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_);

  return $x if $x-&gt;{sign} !~ /^[+-]$/ ||	# not for NaN, inf
            $MBI-&gt;_is_one($x-&gt;{_d});		# 22/1 =&gt; 22, 0/1 =&gt; 0

  $x-&gt;{_n} = $MBI-&gt;_div($x-&gt;{_n},$x-&gt;{_d});	# 22/7 =&gt; 3/1 w/ truncate
  $x-&gt;{_d} = $MBI-&gt;_one();			# d =&gt; 1
  $x-&gt;{_n} = $MBI-&gt;_inc($x-&gt;{_n})
    if $x-&gt;{sign} eq '+';			# +22/7 =&gt; 4/1
  $x-&gt;{sign} = '+' if $MBI-&gt;_is_zero($x-&gt;{_n});	# -0 =&gt; 0
  $x;
  }

sub bfloor
  {
  my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_);

  return $x if $x-&gt;{sign} !~ /^[+-]$/ ||	# not for NaN, inf
            $MBI-&gt;_is_one($x-&gt;{_d});		# 22/1 =&gt; 22, 0/1 =&gt; 0

  $x-&gt;{_n} = $MBI-&gt;_div($x-&gt;{_n},$x-&gt;{_d});	# 22/7 =&gt; 3/1 w/ truncate
  $x-&gt;{_d} = $MBI-&gt;_one();			# d =&gt; 1
  $x-&gt;{_n} = $MBI-&gt;_inc($x-&gt;{_n})
    if $x-&gt;{sign} eq '-';			# -22/7 =&gt; -4/1
  $x;
  }

sub bfac
  {
  my ($self,$x,@r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_);

  # if $x is not an integer
  if (($x-&gt;{sign} ne '+') || (!$MBI-&gt;_is_one($x-&gt;{_d})))
    {
    return $x-&gt;bnan();
    }

  $x-&gt;{_n} = $MBI-&gt;_fac($x-&gt;{_n});
  # since _d is 1, we don't need to reduce/norm the result
  $x-&gt;round(@r);
  }

sub bpow
  {
  # power ($x ** $y)

  # set up parameters
  my ($self,$x,$y,@r) = (ref($_[0]),@_);
  # objectify is costly, so avoid it
  if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1])))
    {
    ($self,$x,$y,@r) = objectify(2,@_);
    }

  return $x if $x-&gt;{sign} =~ /^[+-]inf$/;       # -inf/+inf ** x
  return $x-&gt;bnan() if $x-&gt;{sign} eq $nan || $y-&gt;{sign} eq $nan;
  return $x-&gt;bone(@r) if $y-&gt;is_zero();
  return $x-&gt;round(@r) if $x-&gt;is_one() || $y-&gt;is_one();

  if ($x-&gt;{sign} eq '-' &amp;&amp; $MBI-&gt;_is_one($x-&gt;{_n}) &amp;&amp; $MBI-&gt;_is_one($x-&gt;{_d}))
    {
    # if $x == -1 and odd/even y =&gt; +1/-1
    return $y-&gt;is_odd() ? $x-&gt;round(@r) : $x-&gt;babs()-&gt;round(@r);
    # my Casio FX-5500L has a bug here: -1 ** 2 is -1, but -1 * -1 is 1;
    }
  # 1 ** -y =&gt; 1 / (1 ** |y|)
  # so do test for negative $y after above's clause

  return $x-&gt;round(@r) if $x-&gt;is_zero();  # 0**y =&gt; 0 (if not y &lt;= 0)

  # shortcut if y == 1/N (is then sqrt() respective broot())
  if ($MBI-&gt;_is_one($y-&gt;{_n}))
    {
    return $x-&gt;bsqrt(@r) if $MBI-&gt;_is_two($y-&gt;{_d});	# 1/2 =&gt; sqrt
    return $x-&gt;broot($MBI-&gt;_str($y-&gt;{_d}),@r);		# 1/N =&gt; root(N)
    }

  # shortcut y/1 (and/or x/1)
  if ($MBI-&gt;_is_one($y-&gt;{_d}))
    {
    # shortcut for x/1 and y/1
    if ($MBI-&gt;_is_one($x-&gt;{_d}))
      {
      $x-&gt;{_n} = $MBI-&gt;_pow($x-&gt;{_n},$y-&gt;{_n});		# x/1 ** y/1 =&gt; (x ** y)/1
      if ($y-&gt;{sign} eq '-')
        {
        # 0.2 ** -3 =&gt; 1/(0.2 ** 3)
        ($x-&gt;{_n},$x-&gt;{_d}) = ($x-&gt;{_d},$x-&gt;{_n});	# swap
        }
      # correct sign; + ** + =&gt; +
      if ($x-&gt;{sign} eq '-')
        {
        # - * - =&gt; +, - * - * - =&gt; -
        $x-&gt;{sign} = '+' if $MBI-&gt;_is_even($y-&gt;{_n});
        }
      return $x-&gt;round(@r);
      }
    # x/z ** y/1
    $x-&gt;{_n} = $MBI-&gt;_pow($x-&gt;{_n},$y-&gt;{_n});		# 5/2 ** y/1 =&gt; 5 ** y / 2 ** y
    $x-&gt;{_d} = $MBI-&gt;_pow($x-&gt;{_d},$y-&gt;{_n});
    if ($y-&gt;{sign} eq '-')
      {
      # 0.2 ** -3 =&gt; 1/(0.2 ** 3)
      ($x-&gt;{_n},$x-&gt;{_d}) = ($x-&gt;{_d},$x-&gt;{_n});	# swap
      }
    # correct sign; + ** + =&gt; +
    if ($x-&gt;{sign} eq '-')
      {
      # - * - =&gt; +, - * - * - =&gt; -
      $x-&gt;{sign} = '+' if $MBI-&gt;_is_even($y-&gt;{_n});
      }
    return $x-&gt;round(@r);
    }

#  print STDERR "# $x $y\n";

  # otherwise:

  #      n/d     n  ______________
  # a/b       =  -\/  (a/b) ** d

  # (a/b) ** n == (a ** n) / (b ** n)
  $MBI-&gt;_pow($x-&gt;{_n}, $y-&gt;{_n} );
  $MBI-&gt;_pow($x-&gt;{_d}, $y-&gt;{_n} );

  return $x-&gt;broot($MBI-&gt;_str($y-&gt;{_d}),@r);		# n/d =&gt; root(n)
  }

sub blog
  {
  # set up parameters
  my ($self,$x,$y,@r) = (ref($_[0]),@_);

  # objectify is costly, so avoid it
  if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1])))
    {
    ($self,$x,$y,@r) = objectify(2,$class,@_);
    }

  # blog(1,Y) =&gt; 0
  return $x-&gt;bzero() if $x-&gt;is_one() &amp;&amp; $y-&gt;{sign} eq '+';

  # $x &lt;= 0 =&gt; NaN
  return $x-&gt;bnan() if $x-&gt;is_zero() || $x-&gt;{sign} ne '+' || $y-&gt;{sign} ne '+';

  if ($x-&gt;is_int() &amp;&amp; $y-&gt;is_int())
    {
    return $self-&gt;new($x-&gt;as_number()-&gt;blog($y-&gt;as_number(),@r));
    }

  # do it with floats
  $x-&gt;_new_from_float( $x-&gt;_as_float()-&gt;blog(Math::BigFloat-&gt;new("$y"),@r) );
  }

sub bexp
  {
  # set up parameters
  my ($self,$x,$y,@r) = (ref($_[0]),@_);

  # objectify is costly, so avoid it
  if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1])))
    {
    ($self,$x,$y,@r) = objectify(2,$class,@_);
    }

  return $x-&gt;binf(@r) if $x-&gt;{sign} eq '+inf';
  return $x-&gt;bzero(@r) if $x-&gt;{sign} eq '-inf';

  # we need to limit the accuracy to protect against overflow
  my $fallback = 0;
  my ($scale,@params);
  ($x,@params) = $x-&gt;_find_round_parameters(@r);

  # also takes care of the "error in _find_round_parameters?" case
  return $x if $x-&gt;{sign} eq 'NaN';

  # no rounding at all, so must use fallback
  if (scalar @params == 0)
    {
    # simulate old behaviour
    $params[0] = $self-&gt;div_scale();	# and round to it as accuracy
    $params[1] = undef;			# P = undef
    $scale = $params[0]+4;		# at least four more for proper round
    $params[2] = $r[2];			# round mode by caller or undef
    $fallback = 1;			# to clear a/p afterwards
    }
  else
    {
    # the 4 below is empirical, and there might be cases where it's not enough...
    $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined
    }

  return $x-&gt;bone(@params) if $x-&gt;is_zero();

  # See the comments in Math::BigFloat on how this algorithm works.
  # Basically we calculate A and B (where B is faculty(N)) so that A/B = e

  my $x_org = $x-&gt;copy();
  if ($scale &lt;= 75)
    {
    # set $x directly from a cached string form
    $x-&gt;{_n} = $MBI-&gt;_new("90933395208605785401971970164779391644753259799242");
    $x-&gt;{_d} = $MBI-&gt;_new("33452526613163807108170062053440751665152000000000");
    $x-&gt;{sign} = '+';
    }
  else
    {
    # compute A and B so that e = A / B.

    # After some terms we end up with this, so we use it as a starting point:
    my $A = $MBI-&gt;_new("90933395208605785401971970164779391644753259799242");
    my $F = $MBI-&gt;_new(42); my $step = 42;

    # Compute how many steps we need to take to get $A and $B sufficiently big
    my $steps = Math::BigFloat::_len_to_steps($scale - 4);
#    print STDERR "# Doing $steps steps for ", $scale-4, " digits\n";
    while ($step++ &lt;= $steps)
      {
      # calculate $a * $f + 1
      $A = $MBI-&gt;_mul($A, $F);
      $A = $MBI-&gt;_inc($A);
      # increment f
      $F = $MBI-&gt;_inc($F);
      }
    # compute $B as factorial of $steps (this is faster than doing it manually)
    my $B = $MBI-&gt;_fac($MBI-&gt;_new($steps));

#  print "A ", $MBI-&gt;_str($A), "\nB ", $MBI-&gt;_str($B), "\n";

    $x-&gt;{_n} = $A;
    $x-&gt;{_d} = $B;
    $x-&gt;{sign} = '+';
    }

  # $x contains now an estimate of e, with some surplus digits, so we can round
  if (!$x_org-&gt;is_one())
    {
    # raise $x to the wanted power and round it in one step:
    $x-&gt;bpow($x_org, @params);
    }
  else
    {
    # else just round the already computed result
    delete $x-&gt;{_a}; delete $x-&gt;{_p};
    # shortcut to not run through _find_round_parameters again
    if (defined $params[0])
      {
      $x-&gt;bround($params[0],$params[2]);                # then round accordingly
      }
    else
      {
      $x-&gt;bfround($params[1],$params[2]);               # then round accordingly
      }
    }
  if ($fallback)
    {
    # clear a/p after round, since user did not request it
    delete $x-&gt;{_a}; delete $x-&gt;{_p};
    }

  $x;
  }

sub bnok
  {
  # set up parameters
  my ($self,$x,$y,@r) = (ref($_[0]),@_);

  # objectify is costly, so avoid it
  if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1])))
    {
    ($self,$x,$y,@r) = objectify(2,$class,@_);
    }

  # do it with floats
  $x-&gt;_new_from_float( $x-&gt;_as_float()-&gt;bnok(Math::BigFloat-&gt;new("$y"),@r) );
  }

sub _float_from_part
  {
  my $x = shift;

  my $f = Math::BigFloat-&gt;bzero();
  $f-&gt;{_m} = $MBI-&gt;_copy($x);
  $f-&gt;{_e} = $MBI-&gt;_zero();

  $f;
  }

sub _as_float
  {
  my $x = shift;

  local $Math::BigFloat::upgrade = undef;
  local $Math::BigFloat::accuracy = undef;
  local $Math::BigFloat::precision = undef;
  # 22/7 =&gt; 3.142857143..

  my $a = $x-&gt;accuracy() || 0;
  if ($a != 0 || !$MBI-&gt;_is_one($x-&gt;{_d}))
    {
    # n/d
    return scalar Math::BigFloat-&gt;new($x-&gt;{sign} . $MBI-&gt;_str($x-&gt;{_n}))-&gt;bdiv( $MBI-&gt;_str($x-&gt;{_d}), $x-&gt;accuracy());
    }
  # just n
  Math::BigFloat-&gt;new($x-&gt;{sign} . $MBI-&gt;_str($x-&gt;{_n}));
  }

sub broot
  {
  # set up parameters
  my ($self,$x,$y,@r) = (ref($_[0]),@_);
  # objectify is costly, so avoid it
  if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1])))
    {
    ($self,$x,$y,@r) = objectify(2,@_);
    }

  if ($x-&gt;is_int() &amp;&amp; $y-&gt;is_int())
    {
    return $self-&gt;new($x-&gt;as_number()-&gt;broot($y-&gt;as_number(),@r));
    }

  # do it with floats
  $x-&gt;_new_from_float( $x-&gt;_as_float()-&gt;broot($y-&gt;_as_float(),@r) )-&gt;bnorm()-&gt;bround(@r);
  }

sub bmodpow
  {
  # set up parameters
  my ($self,$x,$y,$m,@r) = (ref($_[0]),@_);
  # objectify is costly, so avoid it
  if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1])))
    {
    ($self,$x,$y,$m,@r) = objectify(3,@_);
    }

  # $x or $y or $m are NaN or +-inf =&gt; NaN
  return $x-&gt;bnan()
   if $x-&gt;{sign} !~ /^[+-]$/ || $y-&gt;{sign} !~ /^[+-]$/ ||
   $m-&gt;{sign} !~ /^[+-]$/;

  if ($x-&gt;is_int() &amp;&amp; $y-&gt;is_int() &amp;&amp; $m-&gt;is_int())
    {
    return $self-&gt;new($x-&gt;as_number()-&gt;bmodpow($y-&gt;as_number(),$m,@r));
    }

  warn ("bmodpow() not fully implemented");
  $x-&gt;bnan();
  }

sub bmodinv
  {
  # set up parameters
  my ($self,$x,$y,@r) = (ref($_[0]),@_);
  # objectify is costly, so avoid it
  if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1])))
    {
    ($self,$x,$y,@r) = objectify(2,@_);
    }

  # $x or $y are NaN or +-inf =&gt; NaN
  return $x-&gt;bnan()
   if $x-&gt;{sign} !~ /^[+-]$/ || $y-&gt;{sign} !~ /^[+-]$/;

  if ($x-&gt;is_int() &amp;&amp; $y-&gt;is_int())
    {
    return $self-&gt;new($x-&gt;as_number()-&gt;bmodinv($y-&gt;as_number(),@r));
    }

  warn ("bmodinv() not fully implemented");
  $x-&gt;bnan();
  }

sub bsqrt
  {
  my ($self,$x,@r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_);

  return $x-&gt;bnan() if $x-&gt;{sign} !~ /^[+]/;    # NaN, -inf or &lt; 0
  return $x if $x-&gt;{sign} eq '+inf';            # sqrt(inf) == inf
  return $x-&gt;round(@r) if $x-&gt;is_zero() || $x-&gt;is_one();

  local $Math::BigFloat::upgrade = undef;
  local $Math::BigFloat::downgrade = undef;
  local $Math::BigFloat::precision = undef;
  local $Math::BigFloat::accuracy = undef;
  local $Math::BigInt::upgrade = undef;
  local $Math::BigInt::precision = undef;
  local $Math::BigInt::accuracy = undef;

  $x-&gt;{_n} = _float_from_part( $x-&gt;{_n} )-&gt;bsqrt();
  $x-&gt;{_d} = _float_from_part( $x-&gt;{_d} )-&gt;bsqrt();

  # XXX TODO: we probably can optimze this:

  # if sqrt(D) was not integer
  if ($x-&gt;{_d}-&gt;{_es} ne '+')
    {
    $x-&gt;{_n}-&gt;blsft($x-&gt;{_d}-&gt;exponent()-&gt;babs(),10);	# 7.1/4.51 =&gt; 7.1/45.1
    $x-&gt;{_d} = $MBI-&gt;_copy( $x-&gt;{_d}-&gt;{_m} );		# 7.1/45.1 =&gt; 71/45.1
    }
  # if sqrt(N) was not integer
  if ($x-&gt;{_n}-&gt;{_es} ne '+')
    {
    $x-&gt;{_d}-&gt;blsft($x-&gt;{_n}-&gt;exponent()-&gt;babs(),10);	# 71/45.1 =&gt; 710/45.1
    $x-&gt;{_n} = $MBI-&gt;_copy( $x-&gt;{_n}-&gt;{_m} );		# 710/45.1 =&gt; 710/451
    }

  # convert parts to $MBI again
  $x-&gt;{_n} = $MBI-&gt;_lsft( $MBI-&gt;_copy( $x-&gt;{_n}-&gt;{_m} ), $x-&gt;{_n}-&gt;{_e}, 10)
    if ref($x-&gt;{_n}) ne $MBI &amp;&amp; ref($x-&gt;{_n}) ne 'ARRAY';
  $x-&gt;{_d} = $MBI-&gt;_lsft( $MBI-&gt;_copy( $x-&gt;{_d}-&gt;{_m} ), $x-&gt;{_d}-&gt;{_e}, 10)
    if ref($x-&gt;{_d}) ne $MBI &amp;&amp; ref($x-&gt;{_d}) ne 'ARRAY';

  $x-&gt;bnorm()-&gt;round(@r);
  }

sub blsft
  {
  my ($self,$x,$y,$b,@r) = objectify(3,@_);

  $b = 2 unless defined $b;
  $b = $self-&gt;new($b) unless ref ($b);
  $x-&gt;bmul( $b-&gt;copy()-&gt;bpow($y), @r);
  $x;
  }

sub brsft
  {
  my ($self,$x,$y,$b,@r) = objectify(3,@_);

  $b = 2 unless defined $b;
  $b = $self-&gt;new($b) unless ref ($b);
  $x-&gt;bdiv( $b-&gt;copy()-&gt;bpow($y), @r);
  $x;
  }

##############################################################################
# round

sub round
  {
  $_[0];
  }

sub bround
  {
  $_[0];
  }

sub bfround
  {
  $_[0];
  }

##############################################################################
# comparing

sub bcmp
  {
  # compare two signed numbers

  # set up parameters
  my ($self,$x,$y) = (ref($_[0]),@_);
  # objectify is costly, so avoid it
  if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1])))
    {
    ($self,$x,$y) = objectify(2,@_);
    }

  if (($x-&gt;{sign} !~ /^[+-]$/) || ($y-&gt;{sign} !~ /^[+-]$/))
    {
    # handle +-inf and NaN
    return undef if (($x-&gt;{sign} eq $nan) || ($y-&gt;{sign} eq $nan));
    return 0 if $x-&gt;{sign} eq $y-&gt;{sign} &amp;&amp; $x-&gt;{sign} =~ /^[+-]inf$/;
    return +1 if $x-&gt;{sign} eq '+inf';
    return -1 if $x-&gt;{sign} eq '-inf';
    return -1 if $y-&gt;{sign} eq '+inf';
    return +1;
    }
  # check sign for speed first
  return 1 if $x-&gt;{sign} eq '+' &amp;&amp; $y-&gt;{sign} eq '-';   # does also 0 &lt;=&gt; -y
  return -1 if $x-&gt;{sign} eq '-' &amp;&amp; $y-&gt;{sign} eq '+';  # does also -x &lt;=&gt; 0

  # shortcut
  my $xz = $MBI-&gt;_is_zero($x-&gt;{_n});
  my $yz = $MBI-&gt;_is_zero($y-&gt;{_n});
  return 0 if $xz &amp;&amp; $yz;                               # 0 &lt;=&gt; 0
  return -1 if $xz &amp;&amp; $y-&gt;{sign} eq '+';                # 0 &lt;=&gt; +y
  return 1 if $yz &amp;&amp; $x-&gt;{sign} eq '+';                 # +x &lt;=&gt; 0

  my $t = $MBI-&gt;_mul( $MBI-&gt;_copy($x-&gt;{_n}), $y-&gt;{_d});
  my $u = $MBI-&gt;_mul( $MBI-&gt;_copy($y-&gt;{_n}), $x-&gt;{_d});

  my $cmp = $MBI-&gt;_acmp($t,$u);				# signs are equal
  $cmp = -$cmp if $x-&gt;{sign} eq '-';			# both are '-' =&gt; reverse
  $cmp;
  }

sub bacmp
  {
  # compare two numbers (as unsigned)

  # set up parameters
  my ($self,$x,$y) = (ref($_[0]),@_);
  # objectify is costly, so avoid it
  if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1])))
    {
    ($self,$x,$y) = objectify(2,$class,@_);
    }

  if (($x-&gt;{sign} !~ /^[+-]$/) || ($y-&gt;{sign} !~ /^[+-]$/))
    {
    # handle +-inf and NaN
    return undef if (($x-&gt;{sign} eq $nan) || ($y-&gt;{sign} eq $nan));
    return 0 if $x-&gt;{sign} =~ /^[+-]inf$/ &amp;&amp; $y-&gt;{sign} =~ /^[+-]inf$/;
    return 1 if $x-&gt;{sign} =~ /^[+-]inf$/ &amp;&amp; $y-&gt;{sign} !~ /^[+-]inf$/;
    return -1;
    }

  my $t = $MBI-&gt;_mul( $MBI-&gt;_copy($x-&gt;{_n}), $y-&gt;{_d});
  my $u = $MBI-&gt;_mul( $MBI-&gt;_copy($y-&gt;{_n}), $x-&gt;{_d});
  $MBI-&gt;_acmp($t,$u);					# ignore signs
  }

##############################################################################
# output conversation

sub numify
  {
  # convert 17/8 =&gt; float (aka 2.125)
  my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_);

  return $x-&gt;bstr() if $x-&gt;{sign} !~ /^[+-]$/;	# inf, NaN, etc

  # N/1 =&gt; N
  my $neg = ''; $neg = '-' if $x-&gt;{sign} eq '-';
  return $neg . $MBI-&gt;_num($x-&gt;{_n}) if $MBI-&gt;_is_one($x-&gt;{_d});

  $x-&gt;_as_float()-&gt;numify() + 0.0;
  }

sub as_number
  {
  my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_);

  # NaN, inf etc
  return Math::BigInt-&gt;new($x-&gt;{sign}) if $x-&gt;{sign} !~ /^[+-]$/;

  my $u = Math::BigInt-&gt;bzero();
  $u-&gt;{value} = $MBI-&gt;_div( $MBI-&gt;_copy($x-&gt;{_n}), $x-&gt;{_d});	# 22/7 =&gt; 3
  $u-&gt;bneg if $x-&gt;{sign} eq '-'; # no negative zero
  $u;
  }

sub as_float
  {
  # return N/D as Math::BigFloat

  # set up parameters
  my ($self,$x,@r) = (ref($_[0]),@_);
  # objectify is costly, so avoid it
  ($self,$x,@r) = objectify(1,$class,@_) unless ref $_[0];

  # NaN, inf etc
  return Math::BigFloat-&gt;new($x-&gt;{sign}) if $x-&gt;{sign} !~ /^[+-]$/;

  my $u = Math::BigFloat-&gt;bzero();
  $u-&gt;{sign} = $x-&gt;{sign};
  # n
  $u-&gt;{_m} = $MBI-&gt;_copy($x-&gt;{_n});
  $u-&gt;{_e} = $MBI-&gt;_zero();
  $u-&gt;bdiv( $MBI-&gt;_str($x-&gt;{_d}), @r);
  # return $u
  $u;
  }

sub as_bin
  {
  my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_);

  return $x unless $x-&gt;is_int();

  my $s = $x-&gt;{sign}; $s = '' if $s eq '+';
  $s . $MBI-&gt;_as_bin($x-&gt;{_n});
  }

sub as_hex
  {
  my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_);

  return $x unless $x-&gt;is_int();

  my $s = $x-&gt;{sign}; $s = '' if $s eq '+';
  $s . $MBI-&gt;_as_hex($x-&gt;{_n});
  }

sub as_oct
  {
  my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_);

  return $x unless $x-&gt;is_int();

  my $s = $x-&gt;{sign}; $s = '' if $s eq '+';
  $s . $MBI-&gt;_as_oct($x-&gt;{_n});
  }

##############################################################################

sub from_hex
  {
  my $class = shift;

  $class-&gt;new(@_);
  }

sub from_bin
  {
  my $class = shift;

  $class-&gt;new(@_);
  }

sub from_oct
  {
  my $class = shift;

  my @parts;
  for my $c (@_)
    {
    push @parts, Math::BigInt-&gt;from_oct($c);
    }
  $class-&gt;new ( @parts );
  }

##############################################################################
# import

sub import
  {
  my $self = shift;
  my $l = scalar @_;
  my $lib = ''; my @a;
  my $try = 'try';

  for ( my $i = 0; $i &lt; $l ; $i++)
    {
    if ( $_[$i] eq ':constant' )
      {
      # this rest causes overlord er load to step in
      overload::constant float =&gt; sub { $self-&gt;new(shift); };
      }
#    elsif ($_[$i] eq 'upgrade')
#      {
#     # this causes upgrading
#      $upgrade = $_[$i+1];		# or undef to disable
#      $i++;
#      }
    elsif ($_[$i] eq 'downgrade')
      {
      # this causes downgrading
      $downgrade = $_[$i+1];		# or undef to disable
      $i++;
      }
    elsif ($_[$i] =~ /^(lib|try|only)\z/)
      {
      $lib = $_[$i+1] || '';		# default Calc
      $try = $1;			# lib, try or only
      $i++;
      }
    elsif ($_[$i] eq 'with')
      {
      # this argument is no longer used
      #$MBI = $_[$i+1] || 'Math::BigInt::Calc';	# default Math::BigInt::Calc
      $i++;
      }
    else
      {
      push @a, $_[$i];
      }
    }
  require Math::BigInt;

  # let use Math::BigInt lib =&gt; 'GMP'; use Math::BigRat; still have GMP
  if ($lib ne '')
    {
    my @c = split /\s*,\s*/, $lib;
    foreach (@c)
      {
      $_ =~ tr/a-zA-Z0-9://cd;                    # limit to sane characters
      }
    $lib = join(",", @c);
    }
  my @import = ('objectify');
  push @import, $try =&gt; $lib if $lib ne '';

  # MBI already loaded, so feed it our lib arguments
  Math::BigInt-&gt;import( @import );

  $MBI = Math::BigFloat-&gt;config()-&gt;{lib};

  # register us with MBI to get notified of future lib changes
  Math::BigInt::_register_callback( $self, sub { $MBI = $_[0]; } );

  # any non :constant stuff is handled by our parent, Exporter (loaded
  # by Math::BigFloat, even if @_ is empty, to give it a chance
  $self-&gt;SUPER::import(@a);             # for subclasses
  $self-&gt;export_to_level(1,$self,@a);   # need this, too
  }

1;

__END__

=head1 NAME

Math::BigRat - Arbitrary big rational numbers

=head1 SYNOPSIS

	use Math::BigRat;

	my $x = Math::BigRat-&gt;new('3/7'); $x += '5/9';

	print $x-&gt;bstr(),"\n";
	print $x ** 2,"\n";

	my $y = Math::BigRat-&gt;new('inf');
	print "$y ", ($y-&gt;is_inf ? 'is' : 'is not') , " infinity\n";

	my $z = Math::BigRat-&gt;new(144); $z-&gt;bsqrt();

=head1 DESCRIPTION

Math::BigRat complements Math::BigInt and Math::BigFloat by providing support
for arbitrary big rational numbers.

=head2 MATH LIBRARY

You can change the underlying module that does the low-level
math operations by using:

	use Math::BigRat try =&gt; 'GMP';

Note: This needs Math::BigInt::GMP installed.

The following would first try to find Math::BigInt::Foo, then
Math::BigInt::Bar, and when this also fails, revert to Math::BigInt::Calc:

	use Math::BigRat try =&gt; 'Foo,Math::BigInt::Bar';

If you want to get warned when the fallback occurs, replace "try" with
"lib":

	use Math::BigRat lib =&gt; 'Foo,Math::BigInt::Bar';

If you want the code to die instead, replace "try" with
"only":

	use Math::BigRat only =&gt; 'Foo,Math::BigInt::Bar';

=head1 METHODS

Any methods not listed here are derived from Math::BigFloat (or
Math::BigInt), so make sure you check these two modules for further
information.

=head2 new()

	$x = Math::BigRat-&gt;new('1/3');

Create a new Math::BigRat object. Input can come in various forms:

	$x = Math::BigRat-&gt;new(123);				# scalars
	$x = Math::BigRat-&gt;new('inf');				# infinity
	$x = Math::BigRat-&gt;new('123.3');			# float
	$x = Math::BigRat-&gt;new('1/3');				# simple string
	$x = Math::BigRat-&gt;new('1 / 3');			# spaced
	$x = Math::BigRat-&gt;new('1 / 0.1');			# w/ floats
	$x = Math::BigRat-&gt;new(Math::BigInt-&gt;new(3));		# BigInt
	$x = Math::BigRat-&gt;new(Math::BigFloat-&gt;new('3.1'));	# BigFloat
	$x = Math::BigRat-&gt;new(Math::BigInt::Lite-&gt;new('2'));	# BigLite

	# You can also give D and N as different objects:
	$x = Math::BigRat-&gt;new(
		Math::BigInt-&gt;new(-123),
		Math::BigInt-&gt;new(7),
		);			# =&gt; -123/7

=head2 numerator()

	$n = $x-&gt;numerator();

Returns a copy of the numerator (the part above the line) as signed BigInt.

=head2 denominator()

	$d = $x-&gt;denominator();

Returns a copy of the denominator (the part under the line) as positive BigInt.

=head2 parts()

	($n,$d) = $x-&gt;parts();

Return a list consisting of (signed) numerator and (unsigned) denominator as
BigInts.

=head2 numify()

	my $y = $x-&gt;numify();

Returns the object as a scalar. This will lose some data if the object
cannot be represented by a normal Perl scalar (integer or float), so
use L&lt;as_int()&gt; or L&lt;as_float()&gt; instead.

This routine is automatically used whenever a scalar is required:

	my $x = Math::BigRat-&gt;new('3/1');
	@array = (0,1,2,3);
	$y = $array[$x];		# set $y to 3

=head2 as_int()/as_number()

	$x = Math::BigRat-&gt;new('13/7');
	print $x-&gt;as_int(),"\n";		# '1'

Returns a copy of the object as BigInt, truncated to an integer.

C&lt;as_number()&gt; is an alias for C&lt;as_int()&gt;.

=head2 as_float()

	$x = Math::BigRat-&gt;new('13/7');
	print $x-&gt;as_float(),"\n";		# '1'

	$x = Math::BigRat-&gt;new('2/3');
	print $x-&gt;as_float(5),"\n";		# '0.66667'

Returns a copy of the object as BigFloat, preserving the
accuracy as wanted, or the default of 40 digits.

This method was added in v0.22 of Math::BigRat (April 2008).

=head2 as_hex()

	$x = Math::BigRat-&gt;new('13');
	print $x-&gt;as_hex(),"\n";		# '0xd'

Returns the BigRat as hexadecimal string. Works only for integers.

=head2 as_bin()

	$x = Math::BigRat-&gt;new('13');
	print $x-&gt;as_bin(),"\n";		# '0x1101'

Returns the BigRat as binary string. Works only for integers.

=head2 as_oct()

	$x = Math::BigRat-&gt;new('13');
	print $x-&gt;as_oct(),"\n";		# '015'

Returns the BigRat as octal string. Works only for integers.

=head2 from_hex()/from_bin()/from_oct()

	my $h = Math::BigRat-&gt;from_hex('0x10');
	my $b = Math::BigRat-&gt;from_bin('0b10000000');
	my $o = Math::BigRat-&gt;from_oct('020');

Create a BigRat from an hexadecimal, binary or octal number
in string form.

=head2 length()

	$len = $x-&gt;length();

Return the length of $x in digits for integer values.

=head2 digit()

	print Math::BigRat-&gt;new('123/1')-&gt;digit(1);	# 1
	print Math::BigRat-&gt;new('123/1')-&gt;digit(-1);	# 3

Return the N'ths digit from X when X is an integer value.

=head2 bnorm()

	$x-&gt;bnorm();

Reduce the number to the shortest form. This routine is called
automatically whenever it is needed.

=head2 bfac()

	$x-&gt;bfac();

Calculates the factorial of $x. For instance:

	print Math::BigRat-&gt;new('3/1')-&gt;bfac(),"\n";	# 1*2*3
	print Math::BigRat-&gt;new('5/1')-&gt;bfac(),"\n";	# 1*2*3*4*5

Works currently only for integers.

=head2 bround()/round()/bfround()

Are not yet implemented.

=head2 bmod()

	use Math::BigRat;
	my $x = Math::BigRat-&gt;new('7/4');
	my $y = Math::BigRat-&gt;new('4/3');
	print $x-&gt;bmod($y);

Set $x to the remainder of the division of $x by $y.

=head2 bneg()

	$x-&gt;bneg();

Used to negate the object in-place.

=head2 is_one()

	print "$x is 1\n" if $x-&gt;is_one();

Return true if $x is exactly one, otherwise false.

=head2 is_zero()

	print "$x is 0\n" if $x-&gt;is_zero();

Return true if $x is exactly zero, otherwise false.

=head2 is_pos()/is_positive()

	print "$x is &gt;= 0\n" if $x-&gt;is_positive();

Return true if $x is positive (greater than or equal to zero), otherwise
false. Please note that '+inf' is also positive, while 'NaN' and '-inf' aren't.

C&lt;is_positive()&gt; is an alias for C&lt;is_pos()&gt;.

=head2 is_neg()/is_negative()

	print "$x is &lt; 0\n" if $x-&gt;is_negative();

Return true if $x is negative (smaller than zero), otherwise false. Please
note that '-inf' is also negative, while 'NaN' and '+inf' aren't.

C&lt;is_negative()&gt; is an alias for C&lt;is_neg()&gt;.

=head2 is_int()

	print "$x is an integer\n" if $x-&gt;is_int();

Return true if $x has a denominator of 1 (e.g. no fraction parts), otherwise
false. Please note that '-inf', 'inf' and 'NaN' aren't integer.

=head2 is_odd()

	print "$x is odd\n" if $x-&gt;is_odd();

Return true if $x is odd, otherwise false.

=head2 is_even()

	print "$x is even\n" if $x-&gt;is_even();

Return true if $x is even, otherwise false.

=head2 bceil()

	$x-&gt;bceil();

Set $x to the next bigger integer value (e.g. truncate the number to integer
and then increment it by one).

=head2 bfloor()

	$x-&gt;bfloor();

Truncate $x to an integer value.

=head2 bsqrt()

	$x-&gt;bsqrt();

Calculate the square root of $x.

=head2 broot()

	$x-&gt;broot($n);

Calculate the N'th root of $x.

=head2 badd()/bmul()/bsub()/bdiv()/bdec()/binc()

Please see the documentation in L&lt;Math::BigInt&gt;.

=head2 copy()

	my $z = $x-&gt;copy();

Makes a deep copy of the object.

Please see the documentation in L&lt;Math::BigInt&gt; for further details.

=head2 bstr()/bsstr()

	my $x = Math::BigInt-&gt;new('8/4');
	print $x-&gt;bstr(),"\n";			# prints 1/2
	print $x-&gt;bsstr(),"\n";			# prints 1/2

Return a string representing this object.

=head2 bacmp()/bcmp()

Used to compare numbers.

Please see the documentation in L&lt;Math::BigInt&gt; for further details.

=head2 blsft()/brsft()

Used to shift numbers left/right.

Please see the documentation in L&lt;Math::BigInt&gt; for further details.

=head2 bpow()

	$x-&gt;bpow($y);

Compute $x ** $y.

Please see the documentation in L&lt;Math::BigInt&gt; for further details.

=head2 bexp()

	$x-&gt;bexp($accuracy);		# calculate e ** X

Calculates two integers A and B so that A/B is equal to C&lt;e ** $x&gt;, where C&lt;e&gt; is
Euler's number.

This method was added in v0.20 of Math::BigRat (May 2007).

See also L&lt;blog()&gt;.

=head2 bnok()

	$x-&gt;bnok($y);		   # x over y (binomial coefficient n over k)

Calculates the binomial coefficient n over k, also called the "choose"
function. The result is equivalent to:

	( n )      n!
	| - |  = -------
	( k )    k!(n-k)!

This method was added in v0.20 of Math::BigRat (May 2007).

=head2 config()

        use Data::Dumper;

        print Dumper ( Math::BigRat-&gt;config() );
        print Math::BigRat-&gt;config()-&gt;{lib},"\n";

Returns a hash containing the configuration, e.g. the version number, lib
loaded etc. The following hash keys are currently filled in with the
appropriate information.

        key             RO/RW   Description
                                Example
        ============================================================
        lib             RO      Name of the Math library
                                Math::BigInt::Calc
        lib_version     RO      Version of 'lib'
                                0.30
        class           RO      The class of config you just called
                                Math::BigRat
        version         RO      version number of the class you used
                                0.10
        upgrade         RW      To which class numbers are upgraded
                                undef
        downgrade       RW      To which class numbers are downgraded
                                undef
        precision       RW      Global precision
                                undef
        accuracy        RW      Global accuracy
                                undef
        round_mode      RW      Global round mode
                                even
        div_scale       RW      Fallback accuracy for div
                                40
        trap_nan        RW      Trap creation of NaN (undef = no)
                                undef
        trap_inf        RW      Trap creation of +inf/-inf (undef = no)
                                undef

By passing a reference to a hash you may set the configuration values. This
works only for values that a marked with a C&lt;RW&gt; above, anything else is
read-only.

=head2 objectify()

This is an internal routine that turns scalars into objects.

=head1 BUGS

Some things are not yet implemented, or only implemented half-way:

=over 2

=item inf handling (partial)

=item NaN handling (partial)

=item rounding (not implemented except for bceil/bfloor)

=item $x ** $y where $y is not an integer

=item bmod(), blog(), bmodinv() and bmodpow() (partial)

=back

=head1 LICENSE

This program is free software; you may redistribute it and/or modify it under
the same terms as Perl itself.

=head1 SEE ALSO

L&lt;Math::BigFloat&gt; and L&lt;Math::Big&gt; as well as L&lt;Math::BigInt::BitVect&gt;,
L&lt;Math::BigInt::Pari&gt; and  L&lt;Math::BigInt::GMP&gt;.

See L&lt;http://search.cpan.org/search?dist=bignum&gt; for a way to use
Math::BigRat.

The package at L&lt;http://search.cpan.org/search?dist=Math%3A%3ABigRat&gt;
may contain more documentation and examples as well as testcases.

=head1 AUTHORS

(C) by Tels L&lt;http://bloodgate.com/&gt; 2001 - 2009.

Currently maintained by Jonathan "Duke" Leto &lt;jonathan@leto.net&gt; L&lt;http://leto.net&gt;

=cut
</pre></body></html>