xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/lib/Math/BigRat.pm (revision 0:68f95e015346)
1*0Sstevel@tonic-gate
2*0Sstevel@tonic-gate#
3*0Sstevel@tonic-gate# "Tax the rat farms." - Lord Vetinari
4*0Sstevel@tonic-gate#
5*0Sstevel@tonic-gate
6*0Sstevel@tonic-gate# The following hash values are used:
7*0Sstevel@tonic-gate#   sign : +,-,NaN,+inf,-inf
8*0Sstevel@tonic-gate#   _d   : denominator
9*0Sstevel@tonic-gate#   _n   : numeraotr (value = _n/_d)
10*0Sstevel@tonic-gate#   _a   : accuracy
11*0Sstevel@tonic-gate#   _p   : precision
12*0Sstevel@tonic-gate#   _f   : flags, used by MBR to flag parts of a rational as untouchable
13*0Sstevel@tonic-gate# You should not look at the innards of a BigRat - use the methods for this.
14*0Sstevel@tonic-gate
15*0Sstevel@tonic-gatepackage Math::BigRat;
16*0Sstevel@tonic-gate
17*0Sstevel@tonic-gaterequire 5.005_03;
18*0Sstevel@tonic-gateuse strict;
19*0Sstevel@tonic-gate
20*0Sstevel@tonic-gaterequire Exporter;
21*0Sstevel@tonic-gateuse Math::BigFloat;
22*0Sstevel@tonic-gateuse vars qw($VERSION @ISA $PACKAGE $upgrade $downgrade
23*0Sstevel@tonic-gate            $accuracy $precision $round_mode $div_scale $_trap_nan $_trap_inf);
24*0Sstevel@tonic-gate
25*0Sstevel@tonic-gate@ISA = qw(Exporter Math::BigFloat);
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate$VERSION = '0.12';
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gateuse overload;			# inherit from Math::BigFloat
30*0Sstevel@tonic-gate
31*0Sstevel@tonic-gateBEGIN { *objectify = \&Math::BigInt::objectify; }
32*0Sstevel@tonic-gate
33*0Sstevel@tonic-gate##############################################################################
34*0Sstevel@tonic-gate# global constants, flags and accessory
35*0Sstevel@tonic-gate
36*0Sstevel@tonic-gate$accuracy = $precision = undef;
37*0Sstevel@tonic-gate$round_mode = 'even';
38*0Sstevel@tonic-gate$div_scale = 40;
39*0Sstevel@tonic-gate$upgrade = undef;
40*0Sstevel@tonic-gate$downgrade = undef;
41*0Sstevel@tonic-gate
42*0Sstevel@tonic-gate# these are internally, and not to be used from the outside
43*0Sstevel@tonic-gate
44*0Sstevel@tonic-gateuse constant MB_NEVER_ROUND => 0x0001;
45*0Sstevel@tonic-gate
46*0Sstevel@tonic-gate$_trap_nan = 0;                         # are NaNs ok? set w/ config()
47*0Sstevel@tonic-gate$_trap_inf = 0;                         # are infs ok? set w/ config()
48*0Sstevel@tonic-gate
49*0Sstevel@tonic-gatemy $nan = 'NaN';
50*0Sstevel@tonic-gatemy $MBI = 'Math::BigInt';
51*0Sstevel@tonic-gatemy $CALC = 'Math::BigInt::Calc';
52*0Sstevel@tonic-gatemy $class = 'Math::BigRat';
53*0Sstevel@tonic-gatemy $IMPORT = 0;
54*0Sstevel@tonic-gate
55*0Sstevel@tonic-gatesub isa
56*0Sstevel@tonic-gate  {
57*0Sstevel@tonic-gate  return 0 if $_[1] =~ /^Math::Big(Int|Float)/;		# we aren't
58*0Sstevel@tonic-gate  UNIVERSAL::isa(@_);
59*0Sstevel@tonic-gate  }
60*0Sstevel@tonic-gate
61*0Sstevel@tonic-gatesub BEGIN
62*0Sstevel@tonic-gate  {
63*0Sstevel@tonic-gate  *AUTOLOAD = \&Math::BigFloat::AUTOLOAD;
64*0Sstevel@tonic-gate  }
65*0Sstevel@tonic-gate
66*0Sstevel@tonic-gatesub _new_from_float
67*0Sstevel@tonic-gate  {
68*0Sstevel@tonic-gate  # turn a single float input into a rational number (like '0.1')
69*0Sstevel@tonic-gate  my ($self,$f) = @_;
70*0Sstevel@tonic-gate
71*0Sstevel@tonic-gate  return $self->bnan() if $f->is_nan();
72*0Sstevel@tonic-gate  return $self->binf($f->{sign}) if $f->{sign} =~ /^[+-]inf$/;
73*0Sstevel@tonic-gate
74*0Sstevel@tonic-gate  local $Math::BigInt::accuracy = undef;
75*0Sstevel@tonic-gate  local $Math::BigInt::precision = undef;
76*0Sstevel@tonic-gate  $self->{_n} = $MBI->new($CALC->_str ( $f->{_m} ),undef,undef);# mantissa
77*0Sstevel@tonic-gate  $self->{_d} = $MBI->bone();
78*0Sstevel@tonic-gate  $self->{sign} = $f->{sign} || '+';
79*0Sstevel@tonic-gate  if ($f->{_es} eq '-')
80*0Sstevel@tonic-gate    {
81*0Sstevel@tonic-gate    # something like Math::BigRat->new('0.1');
82*0Sstevel@tonic-gate    # 1 / 1 => 1/10
83*0Sstevel@tonic-gate    $self->{_d}->blsft( $MBI->new($CALC->_str ( $f->{_e} )),10);
84*0Sstevel@tonic-gate    }
85*0Sstevel@tonic-gate  else
86*0Sstevel@tonic-gate    {
87*0Sstevel@tonic-gate    # something like Math::BigRat->new('10');
88*0Sstevel@tonic-gate    # 1 / 1 => 10/1
89*0Sstevel@tonic-gate    $self->{_n}->blsft( $MBI->new($CALC->_str($f->{_e})),10) unless
90*0Sstevel@tonic-gate      $CALC->_is_zero($f->{_e});
91*0Sstevel@tonic-gate    }
92*0Sstevel@tonic-gate  $self;
93*0Sstevel@tonic-gate  }
94*0Sstevel@tonic-gate
95*0Sstevel@tonic-gatesub new
96*0Sstevel@tonic-gate  {
97*0Sstevel@tonic-gate  # create a Math::BigRat
98*0Sstevel@tonic-gate  my $class = shift;
99*0Sstevel@tonic-gate
100*0Sstevel@tonic-gate  my ($n,$d) = shift;
101*0Sstevel@tonic-gate
102*0Sstevel@tonic-gate  my $self = { }; bless $self,$class;
103*0Sstevel@tonic-gate
104*0Sstevel@tonic-gate  # input like (BigInt,BigInt) or (BigFloat,BigFloat) not handled yet
105*0Sstevel@tonic-gate
106*0Sstevel@tonic-gate  if ((!defined $d) && (ref $n) && (!$n->isa('Math::BigRat')))
107*0Sstevel@tonic-gate    {
108*0Sstevel@tonic-gate    if ($n->isa('Math::BigFloat'))
109*0Sstevel@tonic-gate      {
110*0Sstevel@tonic-gate      $self->_new_from_float($n);
111*0Sstevel@tonic-gate      }
112*0Sstevel@tonic-gate    if ($n->isa('Math::BigInt'))
113*0Sstevel@tonic-gate      {
114*0Sstevel@tonic-gate      # TODO: trap NaN, inf
115*0Sstevel@tonic-gate      $self->{_n} = $n->copy();				# "mantissa" = $n
116*0Sstevel@tonic-gate      $self->{_d} = $MBI->bone();
117*0Sstevel@tonic-gate      $self->{sign} = $self->{_n}->{sign}; $self->{_n}->{sign} = '+';
118*0Sstevel@tonic-gate      }
119*0Sstevel@tonic-gate    if ($n->isa('Math::BigInt::Lite'))
120*0Sstevel@tonic-gate      {
121*0Sstevel@tonic-gate      # TODO: trap NaN, inf
122*0Sstevel@tonic-gate      $self->{sign} = '+'; $self->{sign} = '-' if $$n < 0;
123*0Sstevel@tonic-gate      $self->{_n} = $MBI->new(abs($$n),undef,undef);	# "mantissa" = $n
124*0Sstevel@tonic-gate      $self->{_d} = $MBI->bone();
125*0Sstevel@tonic-gate      }
126*0Sstevel@tonic-gate    return $self->bnorm();
127*0Sstevel@tonic-gate    }
128*0Sstevel@tonic-gate  return $n->copy() if ref $n;
129*0Sstevel@tonic-gate
130*0Sstevel@tonic-gate  if (!defined $n)
131*0Sstevel@tonic-gate    {
132*0Sstevel@tonic-gate    $self->{_n} = $MBI->bzero();			# undef => 0
133*0Sstevel@tonic-gate    $self->{_d} = $MBI->bone();
134*0Sstevel@tonic-gate    $self->{sign} = '+';
135*0Sstevel@tonic-gate    return $self->bnorm();
136*0Sstevel@tonic-gate    }
137*0Sstevel@tonic-gate  # string input with / delimiter
138*0Sstevel@tonic-gate  if ($n =~ /\s*\/\s*/)
139*0Sstevel@tonic-gate    {
140*0Sstevel@tonic-gate    return $class->bnan() if $n =~ /\/.*\//;	# 1/2/3 isn't valid
141*0Sstevel@tonic-gate    return $class->bnan() if $n =~ /\/\s*$/;	# 1/ isn't valid
142*0Sstevel@tonic-gate    ($n,$d) = split (/\//,$n);
143*0Sstevel@tonic-gate    # try as BigFloats first
144*0Sstevel@tonic-gate    if (($n =~ /[\.eE]/) || ($d =~ /[\.eE]/))
145*0Sstevel@tonic-gate      {
146*0Sstevel@tonic-gate      # one of them looks like a float
147*0Sstevel@tonic-gate      # Math::BigFloat($n,undef,undef) does not what it is supposed to do, so:
148*0Sstevel@tonic-gate      local $Math::BigFloat::accuracy = undef;
149*0Sstevel@tonic-gate      local $Math::BigFloat::precision = undef;
150*0Sstevel@tonic-gate      local $Math::BigInt::accuracy = undef;
151*0Sstevel@tonic-gate      local $Math::BigInt::precision = undef;
152*0Sstevel@tonic-gate
153*0Sstevel@tonic-gate      my $nf = Math::BigFloat->new($n,undef,undef);
154*0Sstevel@tonic-gate      $self->{sign} = '+';
155*0Sstevel@tonic-gate      return $self->bnan() if $nf->is_nan();
156*0Sstevel@tonic-gate      $self->{_n} = $MBI->new( $CALC->_str( $nf->{_m} ) );
157*0Sstevel@tonic-gate
158*0Sstevel@tonic-gate      # now correct $self->{_n} due to $n
159*0Sstevel@tonic-gate      my $f = Math::BigFloat->new($d,undef,undef);
160*0Sstevel@tonic-gate      return $self->bnan() if $f->is_nan();
161*0Sstevel@tonic-gate      $self->{_d} = $MBI->new( $CALC->_str( $f->{_m} ) );
162*0Sstevel@tonic-gate
163*0Sstevel@tonic-gate      # calculate the difference between nE and dE
164*0Sstevel@tonic-gate      my $diff_e = $MBI->new ($nf->exponent())->bsub ( $f->exponent);
165*0Sstevel@tonic-gate      if ($diff_e->is_negative())
166*0Sstevel@tonic-gate	{
167*0Sstevel@tonic-gate        # < 0: mul d with it
168*0Sstevel@tonic-gate        $self->{_d}->blsft($diff_e->babs(),10);
169*0Sstevel@tonic-gate	}
170*0Sstevel@tonic-gate      elsif (!$diff_e->is_zero())
171*0Sstevel@tonic-gate        {
172*0Sstevel@tonic-gate        # > 0: mul n with it
173*0Sstevel@tonic-gate        $self->{_n}->blsft($diff_e,10);
174*0Sstevel@tonic-gate        }
175*0Sstevel@tonic-gate      }
176*0Sstevel@tonic-gate    else
177*0Sstevel@tonic-gate      {
178*0Sstevel@tonic-gate      # both d and n are (big)ints
179*0Sstevel@tonic-gate      $self->{_n} = $MBI->new($n,undef,undef);
180*0Sstevel@tonic-gate      $self->{_d} = $MBI->new($d,undef,undef);
181*0Sstevel@tonic-gate      $self->{sign} = '+';
182*0Sstevel@tonic-gate      return $self->bnan() if $self->{_n}->{sign} eq $nan ||
183*0Sstevel@tonic-gate                              $self->{_d}->{sign} eq $nan;
184*0Sstevel@tonic-gate      # handle inf and NAN cases:
185*0Sstevel@tonic-gate      if ($self->{_n}->is_inf() || $self->{_d}->is_inf())
186*0Sstevel@tonic-gate        {
187*0Sstevel@tonic-gate        # inf/inf => NaN
188*0Sstevel@tonic-gate        return $self->bnan() if
189*0Sstevel@tonic-gate	  ($self->{_n}->is_inf() && $self->{_d}->is_inf());
190*0Sstevel@tonic-gate        if ($self->{_n}->is_inf())
191*0Sstevel@tonic-gate	  {
192*0Sstevel@tonic-gate	  my $s = '+'; 		# '+inf/+123' or '-inf/-123'
193*0Sstevel@tonic-gate	  $s = '-' if substr($self->{_n}->{sign},0,1) ne $self->{_d}->{sign};
194*0Sstevel@tonic-gate	  # +-inf/123 => +-inf
195*0Sstevel@tonic-gate          return $self->binf($s);
196*0Sstevel@tonic-gate	  }
197*0Sstevel@tonic-gate        # 123/inf => 0
198*0Sstevel@tonic-gate        return $self->bzero();
199*0Sstevel@tonic-gate        }
200*0Sstevel@tonic-gate
201*0Sstevel@tonic-gate      $self->{sign} = $self->{_n}->{sign}; $self->{_n}->babs();
202*0Sstevel@tonic-gate      # if $d is negative, flip sign
203*0Sstevel@tonic-gate      $self->{sign} =~ tr/+-/-+/ if $self->{_d}->{sign} eq '-';
204*0Sstevel@tonic-gate      $self->{_d}->babs();				# normalize
205*0Sstevel@tonic-gate      }
206*0Sstevel@tonic-gate
207*0Sstevel@tonic-gate    return $self->bnorm();
208*0Sstevel@tonic-gate    }
209*0Sstevel@tonic-gate
210*0Sstevel@tonic-gate  # simple string input
211*0Sstevel@tonic-gate  if (($n =~ /[\.eE]/))
212*0Sstevel@tonic-gate    {
213*0Sstevel@tonic-gate    # looks like a float, quacks like a float, so probably is a float
214*0Sstevel@tonic-gate    # Math::BigFloat($n,undef,undef) does not what it is supposed to do, so:
215*0Sstevel@tonic-gate    local $Math::BigFloat::accuracy = undef;
216*0Sstevel@tonic-gate    local $Math::BigFloat::precision = undef;
217*0Sstevel@tonic-gate    local $Math::BigInt::accuracy = undef;
218*0Sstevel@tonic-gate    local $Math::BigInt::precision = undef;
219*0Sstevel@tonic-gate    $self->{sign} = 'NaN';
220*0Sstevel@tonic-gate    $self->_new_from_float(Math::BigFloat->new($n,undef,undef));
221*0Sstevel@tonic-gate    }
222*0Sstevel@tonic-gate  else
223*0Sstevel@tonic-gate    {
224*0Sstevel@tonic-gate    $self->{_n} = $MBI->new($n,undef,undef);
225*0Sstevel@tonic-gate    $self->{_d} = $MBI->bone();
226*0Sstevel@tonic-gate    $self->{sign} = $self->{_n}->{sign}; $self->{_n}->babs();
227*0Sstevel@tonic-gate    return $self->bnan() if $self->{sign} eq 'NaN';
228*0Sstevel@tonic-gate    return $self->binf($self->{sign}) if $self->{sign} =~ /^[+-]inf$/;
229*0Sstevel@tonic-gate    }
230*0Sstevel@tonic-gate  $self->bnorm();
231*0Sstevel@tonic-gate  }
232*0Sstevel@tonic-gate
233*0Sstevel@tonic-gatesub copy
234*0Sstevel@tonic-gate  {
235*0Sstevel@tonic-gate  my ($c,$x);
236*0Sstevel@tonic-gate  if (@_ > 1)
237*0Sstevel@tonic-gate    {
238*0Sstevel@tonic-gate    # if two arguments, the first one is the class to "swallow" subclasses
239*0Sstevel@tonic-gate    ($c,$x) = @_;
240*0Sstevel@tonic-gate    }
241*0Sstevel@tonic-gate  else
242*0Sstevel@tonic-gate    {
243*0Sstevel@tonic-gate    $x = shift;
244*0Sstevel@tonic-gate    $c = ref($x);
245*0Sstevel@tonic-gate    }
246*0Sstevel@tonic-gate  return unless ref($x); # only for objects
247*0Sstevel@tonic-gate
248*0Sstevel@tonic-gate  my $self = {}; bless $self,$c;
249*0Sstevel@tonic-gate
250*0Sstevel@tonic-gate  $self->{sign} = $x->{sign};
251*0Sstevel@tonic-gate  $self->{_d} = $x->{_d}->copy();
252*0Sstevel@tonic-gate  $self->{_n} = $x->{_n}->copy();
253*0Sstevel@tonic-gate  $self->{_a} = $x->{_a} if defined $x->{_a};
254*0Sstevel@tonic-gate  $self->{_p} = $x->{_p} if defined $x->{_p};
255*0Sstevel@tonic-gate  $self;
256*0Sstevel@tonic-gate  }
257*0Sstevel@tonic-gate
258*0Sstevel@tonic-gate##############################################################################
259*0Sstevel@tonic-gate
260*0Sstevel@tonic-gatesub config
261*0Sstevel@tonic-gate  {
262*0Sstevel@tonic-gate  # return (later set?) configuration data as hash ref
263*0Sstevel@tonic-gate  my $class = shift || 'Math::BigFloat';
264*0Sstevel@tonic-gate
265*0Sstevel@tonic-gate  my $cfg = $class->SUPER::config(@_);
266*0Sstevel@tonic-gate
267*0Sstevel@tonic-gate  # now we need only to override the ones that are different from our parent
268*0Sstevel@tonic-gate  $cfg->{class} = $class;
269*0Sstevel@tonic-gate  $cfg->{with} = $MBI;
270*0Sstevel@tonic-gate  $cfg;
271*0Sstevel@tonic-gate  }
272*0Sstevel@tonic-gate
273*0Sstevel@tonic-gate##############################################################################
274*0Sstevel@tonic-gate
275*0Sstevel@tonic-gatesub bstr
276*0Sstevel@tonic-gate  {
277*0Sstevel@tonic-gate  my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_);
278*0Sstevel@tonic-gate
279*0Sstevel@tonic-gate  if ($x->{sign} !~ /^[+-]$/)		# inf, NaN etc
280*0Sstevel@tonic-gate    {
281*0Sstevel@tonic-gate    my $s = $x->{sign}; $s =~ s/^\+//; 	# +inf => inf
282*0Sstevel@tonic-gate    return $s;
283*0Sstevel@tonic-gate    }
284*0Sstevel@tonic-gate
285*0Sstevel@tonic-gate  my $s = ''; $s = $x->{sign} if $x->{sign} ne '+';	# '+3/2' => '3/2'
286*0Sstevel@tonic-gate
287*0Sstevel@tonic-gate  return $s . $x->{_n}->bstr() if $x->{_d}->is_one();
288*0Sstevel@tonic-gate  $s . $x->{_n}->bstr() . '/' . $x->{_d}->bstr();
289*0Sstevel@tonic-gate  }
290*0Sstevel@tonic-gate
291*0Sstevel@tonic-gatesub bsstr
292*0Sstevel@tonic-gate  {
293*0Sstevel@tonic-gate  my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_);
294*0Sstevel@tonic-gate
295*0Sstevel@tonic-gate  if ($x->{sign} !~ /^[+-]$/)		# inf, NaN etc
296*0Sstevel@tonic-gate    {
297*0Sstevel@tonic-gate    my $s = $x->{sign}; $s =~ s/^\+//; 	# +inf => inf
298*0Sstevel@tonic-gate    return $s;
299*0Sstevel@tonic-gate    }
300*0Sstevel@tonic-gate
301*0Sstevel@tonic-gate  my $s = ''; $s = $x->{sign} if $x->{sign} ne '+';	# +3 vs 3
302*0Sstevel@tonic-gate  $s . $x->{_n}->bstr() . '/' . $x->{_d}->bstr();
303*0Sstevel@tonic-gate  }
304*0Sstevel@tonic-gate
305*0Sstevel@tonic-gatesub bnorm
306*0Sstevel@tonic-gate  {
307*0Sstevel@tonic-gate  # reduce the number to the shortest form and remember this (so that we
308*0Sstevel@tonic-gate  # don't reduce again)
309*0Sstevel@tonic-gate  my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_);
310*0Sstevel@tonic-gate
311*0Sstevel@tonic-gate  # both parts must be BigInt's (or whatever we are using today)
312*0Sstevel@tonic-gate  if (ref($x->{_n}) ne $MBI)
313*0Sstevel@tonic-gate    {
314*0Sstevel@tonic-gate    require Carp; Carp::croak ("n is not $MBI but (".ref($x->{_n}).')');
315*0Sstevel@tonic-gate    }
316*0Sstevel@tonic-gate  if (ref($x->{_d}) ne $MBI)
317*0Sstevel@tonic-gate    {
318*0Sstevel@tonic-gate    require Carp; Carp::croak ("d is not $MBI but (".ref($x->{_d}).')');
319*0Sstevel@tonic-gate    }
320*0Sstevel@tonic-gate
321*0Sstevel@tonic-gate  # this is to prevent automatically rounding when MBI's globals are set
322*0Sstevel@tonic-gate  $x->{_d}->{_f} = MB_NEVER_ROUND;
323*0Sstevel@tonic-gate  $x->{_n}->{_f} = MB_NEVER_ROUND;
324*0Sstevel@tonic-gate  # 'forget' that parts were rounded via MBI::bround() in MBF's bfround()
325*0Sstevel@tonic-gate  delete $x->{_d}->{_a}; delete $x->{_n}->{_a};
326*0Sstevel@tonic-gate  delete $x->{_d}->{_p}; delete $x->{_n}->{_p};
327*0Sstevel@tonic-gate
328*0Sstevel@tonic-gate  # no normalize for NaN, inf etc.
329*0Sstevel@tonic-gate  return $x if $x->{sign} !~ /^[+-]$/;
330*0Sstevel@tonic-gate
331*0Sstevel@tonic-gate  # normalize zeros to 0/1
332*0Sstevel@tonic-gate  if (($x->{sign} =~ /^[+-]$/) &&
333*0Sstevel@tonic-gate      ($x->{_n}->is_zero()))
334*0Sstevel@tonic-gate    {
335*0Sstevel@tonic-gate    $x->{sign} = '+';					# never -0
336*0Sstevel@tonic-gate    $x->{_d} = $MBI->bone() unless $x->{_d}->is_one();
337*0Sstevel@tonic-gate    return $x;
338*0Sstevel@tonic-gate    }
339*0Sstevel@tonic-gate
340*0Sstevel@tonic-gate  return $x if $x->{_d}->is_one();			# no need to reduce
341*0Sstevel@tonic-gate
342*0Sstevel@tonic-gate  # reduce other numbers
343*0Sstevel@tonic-gate  # disable upgrade in BigInt, otherwise deep recursion
344*0Sstevel@tonic-gate  local $Math::BigInt::upgrade = undef;
345*0Sstevel@tonic-gate  local $Math::BigInt::accuracy = undef;
346*0Sstevel@tonic-gate  local $Math::BigInt::precision = undef;
347*0Sstevel@tonic-gate  my $gcd = $x->{_n}->bgcd($x->{_d});
348*0Sstevel@tonic-gate
349*0Sstevel@tonic-gate  if (!$gcd->is_one())
350*0Sstevel@tonic-gate    {
351*0Sstevel@tonic-gate    $x->{_n}->bdiv($gcd);
352*0Sstevel@tonic-gate    $x->{_d}->bdiv($gcd);
353*0Sstevel@tonic-gate    }
354*0Sstevel@tonic-gate  $x;
355*0Sstevel@tonic-gate  }
356*0Sstevel@tonic-gate
357*0Sstevel@tonic-gate##############################################################################
358*0Sstevel@tonic-gate# special values
359*0Sstevel@tonic-gate
360*0Sstevel@tonic-gatesub _bnan
361*0Sstevel@tonic-gate  {
362*0Sstevel@tonic-gate  # used by parent class bnan() to initialize number to NaN
363*0Sstevel@tonic-gate  my $self = shift;
364*0Sstevel@tonic-gate
365*0Sstevel@tonic-gate  if ($_trap_nan)
366*0Sstevel@tonic-gate    {
367*0Sstevel@tonic-gate    require Carp;
368*0Sstevel@tonic-gate    my $class = ref($self);
369*0Sstevel@tonic-gate    Carp::croak ("Tried to set $self to NaN in $class\::_bnan()");
370*0Sstevel@tonic-gate    }
371*0Sstevel@tonic-gate  $self->{_n} = $MBI->bzero();
372*0Sstevel@tonic-gate  $self->{_d} = $MBI->bzero();
373*0Sstevel@tonic-gate  }
374*0Sstevel@tonic-gate
375*0Sstevel@tonic-gatesub _binf
376*0Sstevel@tonic-gate  {
377*0Sstevel@tonic-gate  # used by parent class bone() to initialize number to +inf/-inf
378*0Sstevel@tonic-gate  my $self = shift;
379*0Sstevel@tonic-gate
380*0Sstevel@tonic-gate  if ($_trap_inf)
381*0Sstevel@tonic-gate    {
382*0Sstevel@tonic-gate    require Carp;
383*0Sstevel@tonic-gate    my $class = ref($self);
384*0Sstevel@tonic-gate    Carp::croak ("Tried to set $self to inf in $class\::_binf()");
385*0Sstevel@tonic-gate    }
386*0Sstevel@tonic-gate  $self->{_n} = $MBI->bzero();
387*0Sstevel@tonic-gate  $self->{_d} = $MBI->bzero();
388*0Sstevel@tonic-gate  }
389*0Sstevel@tonic-gate
390*0Sstevel@tonic-gatesub _bone
391*0Sstevel@tonic-gate  {
392*0Sstevel@tonic-gate  # used by parent class bone() to initialize number to +1/-1
393*0Sstevel@tonic-gate  my $self = shift;
394*0Sstevel@tonic-gate  $self->{_n} = $MBI->bone();
395*0Sstevel@tonic-gate  $self->{_d} = $MBI->bone();
396*0Sstevel@tonic-gate  }
397*0Sstevel@tonic-gate
398*0Sstevel@tonic-gatesub _bzero
399*0Sstevel@tonic-gate  {
400*0Sstevel@tonic-gate  # used by parent class bzero() to initialize number to 0
401*0Sstevel@tonic-gate  my $self = shift;
402*0Sstevel@tonic-gate  $self->{_n} = $MBI->bzero();
403*0Sstevel@tonic-gate  $self->{_d} = $MBI->bone();
404*0Sstevel@tonic-gate  }
405*0Sstevel@tonic-gate
406*0Sstevel@tonic-gate##############################################################################
407*0Sstevel@tonic-gate# mul/add/div etc
408*0Sstevel@tonic-gate
409*0Sstevel@tonic-gatesub badd
410*0Sstevel@tonic-gate  {
411*0Sstevel@tonic-gate  # add two rational numbers
412*0Sstevel@tonic-gate
413*0Sstevel@tonic-gate  # set up parameters
414*0Sstevel@tonic-gate  my ($self,$x,$y,@r) = (ref($_[0]),@_);
415*0Sstevel@tonic-gate  # objectify is costly, so avoid it
416*0Sstevel@tonic-gate  if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1])))
417*0Sstevel@tonic-gate    {
418*0Sstevel@tonic-gate    ($self,$x,$y,@r) = objectify(2,@_);
419*0Sstevel@tonic-gate    }
420*0Sstevel@tonic-gate
421*0Sstevel@tonic-gate  $x = $self->new($x) unless $x->isa($self);
422*0Sstevel@tonic-gate  $y = $self->new($y) unless $y->isa($self);
423*0Sstevel@tonic-gate
424*0Sstevel@tonic-gate  return $x->bnan() if ($x->{sign} eq 'NaN' || $y->{sign} eq 'NaN');
425*0Sstevel@tonic-gate  # TODO: inf handling
426*0Sstevel@tonic-gate
427*0Sstevel@tonic-gate  #  1   1    gcd(3,4) = 1    1*3 + 1*4    7
428*0Sstevel@tonic-gate  #  - + -                  = --------- = --
429*0Sstevel@tonic-gate  #  4   3                      4*3       12
430*0Sstevel@tonic-gate
431*0Sstevel@tonic-gate  # we do not compute the gcd() here, but simple do:
432*0Sstevel@tonic-gate  #  5   7    5*3 + 7*4   41
433*0Sstevel@tonic-gate  #  - + -  = --------- = --
434*0Sstevel@tonic-gate  #  4   3       4*3      12
435*0Sstevel@tonic-gate
436*0Sstevel@tonic-gate  # the gcd() calculation and reducing is then done in bnorm()
437*0Sstevel@tonic-gate
438*0Sstevel@tonic-gate  local $Math::BigInt::accuracy = undef;
439*0Sstevel@tonic-gate  local $Math::BigInt::precision = undef;
440*0Sstevel@tonic-gate
441*0Sstevel@tonic-gate  $x->{_n}->bmul($y->{_d}); $x->{_n}->{sign} = $x->{sign};
442*0Sstevel@tonic-gate  my $m = $y->{_n}->copy()->bmul($x->{_d});
443*0Sstevel@tonic-gate  $m->{sign} = $y->{sign};			# 2/1 - 2/1
444*0Sstevel@tonic-gate  $x->{_n}->badd($m);
445*0Sstevel@tonic-gate
446*0Sstevel@tonic-gate  $x->{_d}->bmul($y->{_d});
447*0Sstevel@tonic-gate
448*0Sstevel@tonic-gate  # calculate sign of result and norm our _n part
449*0Sstevel@tonic-gate  $x->{sign} = $x->{_n}->{sign}; $x->{_n}->{sign} = '+';
450*0Sstevel@tonic-gate
451*0Sstevel@tonic-gate  $x->bnorm()->round(@r);
452*0Sstevel@tonic-gate  }
453*0Sstevel@tonic-gate
454*0Sstevel@tonic-gatesub bsub
455*0Sstevel@tonic-gate  {
456*0Sstevel@tonic-gate  # subtract two rational numbers
457*0Sstevel@tonic-gate
458*0Sstevel@tonic-gate  # set up parameters
459*0Sstevel@tonic-gate  my ($self,$x,$y,@r) = (ref($_[0]),@_);
460*0Sstevel@tonic-gate  # objectify is costly, so avoid it
461*0Sstevel@tonic-gate  if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1])))
462*0Sstevel@tonic-gate    {
463*0Sstevel@tonic-gate    ($self,$x,$y,@r) = objectify(2,@_);
464*0Sstevel@tonic-gate    }
465*0Sstevel@tonic-gate
466*0Sstevel@tonic-gate  # flip sign of $x, call badd(), then flip sign of result
467*0Sstevel@tonic-gate  $x->{sign} =~ tr/+-/-+/
468*0Sstevel@tonic-gate    unless $x->{sign} eq '+' && $x->{_n}->is_zero();	# not -0
469*0Sstevel@tonic-gate  $x->badd($y,@r);			# does norm and round
470*0Sstevel@tonic-gate  $x->{sign} =~ tr/+-/-+/
471*0Sstevel@tonic-gate    unless $x->{sign} eq '+' && $x->{_n}->is_zero();	# not -0
472*0Sstevel@tonic-gate  $x;
473*0Sstevel@tonic-gate  }
474*0Sstevel@tonic-gate
475*0Sstevel@tonic-gatesub bmul
476*0Sstevel@tonic-gate  {
477*0Sstevel@tonic-gate  # multiply two rational numbers
478*0Sstevel@tonic-gate
479*0Sstevel@tonic-gate  # set up parameters
480*0Sstevel@tonic-gate  my ($self,$x,$y,@r) = (ref($_[0]),@_);
481*0Sstevel@tonic-gate  # objectify is costly, so avoid it
482*0Sstevel@tonic-gate  if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1])))
483*0Sstevel@tonic-gate    {
484*0Sstevel@tonic-gate    ($self,$x,$y,@r) = objectify(2,@_);
485*0Sstevel@tonic-gate    }
486*0Sstevel@tonic-gate
487*0Sstevel@tonic-gate  $x = $self->new($x) unless $x->isa($self);
488*0Sstevel@tonic-gate  $y = $self->new($y) unless $y->isa($self);
489*0Sstevel@tonic-gate
490*0Sstevel@tonic-gate  return $x->bnan() if ($x->{sign} eq 'NaN' || $y->{sign} eq 'NaN');
491*0Sstevel@tonic-gate
492*0Sstevel@tonic-gate  # inf handling
493*0Sstevel@tonic-gate  if (($x->{sign} =~ /^[+-]inf$/) || ($y->{sign} =~ /^[+-]inf$/))
494*0Sstevel@tonic-gate    {
495*0Sstevel@tonic-gate    return $x->bnan() if $x->is_zero() || $y->is_zero();
496*0Sstevel@tonic-gate    # result will always be +-inf:
497*0Sstevel@tonic-gate    # +inf * +/+inf => +inf, -inf * -/-inf => +inf
498*0Sstevel@tonic-gate    # +inf * -/-inf => -inf, -inf * +/+inf => -inf
499*0Sstevel@tonic-gate    return $x->binf() if ($x->{sign} =~ /^\+/ && $y->{sign} =~ /^\+/);
500*0Sstevel@tonic-gate    return $x->binf() if ($x->{sign} =~ /^-/ && $y->{sign} =~ /^-/);
501*0Sstevel@tonic-gate    return $x->binf('-');
502*0Sstevel@tonic-gate    }
503*0Sstevel@tonic-gate
504*0Sstevel@tonic-gate  # x== 0 # also: or y == 1 or y == -1
505*0Sstevel@tonic-gate  return wantarray ? ($x,$self->bzero()) : $x if $x->is_zero();
506*0Sstevel@tonic-gate
507*0Sstevel@tonic-gate  # According to Knuth, this can be optimized by doingtwice gcd (for d and n)
508*0Sstevel@tonic-gate  # and reducing in one step)
509*0Sstevel@tonic-gate
510*0Sstevel@tonic-gate  #  1   1    2    1
511*0Sstevel@tonic-gate  #  - * - =  -  = -
512*0Sstevel@tonic-gate  #  4   3    12   6
513*0Sstevel@tonic-gate
514*0Sstevel@tonic-gate  local $Math::BigInt::accuracy = undef;
515*0Sstevel@tonic-gate  local $Math::BigInt::precision = undef;
516*0Sstevel@tonic-gate  $x->{_n}->bmul($y->{_n});
517*0Sstevel@tonic-gate  $x->{_d}->bmul($y->{_d});
518*0Sstevel@tonic-gate
519*0Sstevel@tonic-gate  # compute new sign
520*0Sstevel@tonic-gate  $x->{sign} = $x->{sign} eq $y->{sign} ? '+' : '-';
521*0Sstevel@tonic-gate
522*0Sstevel@tonic-gate  $x->bnorm()->round(@r);
523*0Sstevel@tonic-gate  }
524*0Sstevel@tonic-gate
525*0Sstevel@tonic-gatesub bdiv
526*0Sstevel@tonic-gate  {
527*0Sstevel@tonic-gate  # (dividend: BRAT or num_str, divisor: BRAT or num_str) return
528*0Sstevel@tonic-gate  # (BRAT,BRAT) (quo,rem) or BRAT (only rem)
529*0Sstevel@tonic-gate
530*0Sstevel@tonic-gate  # set up parameters
531*0Sstevel@tonic-gate  my ($self,$x,$y,@r) = (ref($_[0]),@_);
532*0Sstevel@tonic-gate  # objectify is costly, so avoid it
533*0Sstevel@tonic-gate  if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1])))
534*0Sstevel@tonic-gate    {
535*0Sstevel@tonic-gate    ($self,$x,$y,@r) = objectify(2,@_);
536*0Sstevel@tonic-gate    }
537*0Sstevel@tonic-gate
538*0Sstevel@tonic-gate  $x = $self->new($x) unless $x->isa($self);
539*0Sstevel@tonic-gate  $y = $self->new($y) unless $y->isa($self);
540*0Sstevel@tonic-gate
541*0Sstevel@tonic-gate  return $self->_div_inf($x,$y)
542*0Sstevel@tonic-gate   if (($x->{sign} !~ /^[+-]$/) || ($y->{sign} !~ /^[+-]$/) || $y->is_zero());
543*0Sstevel@tonic-gate
544*0Sstevel@tonic-gate  # x== 0 # also: or y == 1 or y == -1
545*0Sstevel@tonic-gate  return wantarray ? ($x,$self->bzero()) : $x if $x->is_zero();
546*0Sstevel@tonic-gate
547*0Sstevel@tonic-gate  # TODO: list context, upgrade
548*0Sstevel@tonic-gate
549*0Sstevel@tonic-gate  # 1     1    1   3
550*0Sstevel@tonic-gate  # -  /  - == - * -
551*0Sstevel@tonic-gate  # 4     3    4   1
552*0Sstevel@tonic-gate
553*0Sstevel@tonic-gate  local $Math::BigInt::accuracy = undef;
554*0Sstevel@tonic-gate  local $Math::BigInt::precision = undef;
555*0Sstevel@tonic-gate  $x->{_n}->bmul($y->{_d});
556*0Sstevel@tonic-gate  $x->{_d}->bmul($y->{_n});
557*0Sstevel@tonic-gate
558*0Sstevel@tonic-gate  # compute new sign
559*0Sstevel@tonic-gate  $x->{sign} = $x->{sign} eq $y->{sign} ? '+' : '-';
560*0Sstevel@tonic-gate
561*0Sstevel@tonic-gate  $x->bnorm()->round(@r);
562*0Sstevel@tonic-gate  $x;
563*0Sstevel@tonic-gate  }
564*0Sstevel@tonic-gate
565*0Sstevel@tonic-gatesub bmod
566*0Sstevel@tonic-gate  {
567*0Sstevel@tonic-gate  # compute "remainder" (in Perl way) of $x / $y
568*0Sstevel@tonic-gate
569*0Sstevel@tonic-gate  # set up parameters
570*0Sstevel@tonic-gate  my ($self,$x,$y,@r) = (ref($_[0]),@_);
571*0Sstevel@tonic-gate  # objectify is costly, so avoid it
572*0Sstevel@tonic-gate  if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1])))
573*0Sstevel@tonic-gate    {
574*0Sstevel@tonic-gate    ($self,$x,$y,@r) = objectify(2,@_);
575*0Sstevel@tonic-gate    }
576*0Sstevel@tonic-gate
577*0Sstevel@tonic-gate  $x = $self->new($x) unless $x->isa($self);
578*0Sstevel@tonic-gate  $y = $self->new($y) unless $y->isa($self);
579*0Sstevel@tonic-gate
580*0Sstevel@tonic-gate  return $self->_div_inf($x,$y)
581*0Sstevel@tonic-gate   if (($x->{sign} !~ /^[+-]$/) || ($y->{sign} !~ /^[+-]$/) || $y->is_zero());
582*0Sstevel@tonic-gate
583*0Sstevel@tonic-gate  return $self->_div_inf($x,$y)
584*0Sstevel@tonic-gate   if (($x->{sign} !~ /^[+-]$/) || ($y->{sign} !~ /^[+-]$/) || $y->is_zero());
585*0Sstevel@tonic-gate
586*0Sstevel@tonic-gate  return $x if $x->is_zero();           # 0 / 7 = 0, mod 0
587*0Sstevel@tonic-gate
588*0Sstevel@tonic-gate  # compute $x - $y * floor($x/$y), keeping the sign of $x
589*0Sstevel@tonic-gate
590*0Sstevel@tonic-gate  # locally disable these, since they would interfere
591*0Sstevel@tonic-gate  local $Math::BigInt::upgrade = undef;
592*0Sstevel@tonic-gate  local $Math::BigInt::accuracy = undef;
593*0Sstevel@tonic-gate  local $Math::BigInt::precision = undef;
594*0Sstevel@tonic-gate
595*0Sstevel@tonic-gate  my $u = $x->copy()->babs();
596*0Sstevel@tonic-gate  # first, do a "normal" division ($x/$y)
597*0Sstevel@tonic-gate  $u->{_d}->bmul($y->{_n});
598*0Sstevel@tonic-gate  $u->{_n}->bmul($y->{_d});
599*0Sstevel@tonic-gate
600*0Sstevel@tonic-gate  # compute floor
601*0Sstevel@tonic-gate  if (!$u->{_d}->is_one())
602*0Sstevel@tonic-gate    {
603*0Sstevel@tonic-gate    $u->{_n}->bdiv($u->{_d});			# 22/7 => 3/1 w/ truncate
604*0Sstevel@tonic-gate    # no need to set $u->{_d} to 1, since later we set it to $y->{_d}
605*0Sstevel@tonic-gate    #$x->{_n}->binc() if $x->{sign} eq '-';	# -22/7 => -4/1
606*0Sstevel@tonic-gate    }
607*0Sstevel@tonic-gate
608*0Sstevel@tonic-gate  # compute $y * $u
609*0Sstevel@tonic-gate  $u->{_d} = $y->{_d};			# 1 * $y->{_d}, see floor above
610*0Sstevel@tonic-gate  $u->{_n}->bmul($y->{_n});
611*0Sstevel@tonic-gate
612*0Sstevel@tonic-gate  my $xsign = $x->{sign}; $x->{sign} = '+';	# remember sign and make abs
613*0Sstevel@tonic-gate  # compute $x - $u
614*0Sstevel@tonic-gate  $x->bsub($u);
615*0Sstevel@tonic-gate  $x->{sign} = $xsign;				# put sign back
616*0Sstevel@tonic-gate
617*0Sstevel@tonic-gate  $x->bnorm()->round(@r);
618*0Sstevel@tonic-gate  }
619*0Sstevel@tonic-gate
620*0Sstevel@tonic-gate##############################################################################
621*0Sstevel@tonic-gate# bdec/binc
622*0Sstevel@tonic-gate
623*0Sstevel@tonic-gatesub bdec
624*0Sstevel@tonic-gate  {
625*0Sstevel@tonic-gate  # decrement value (subtract 1)
626*0Sstevel@tonic-gate  my ($self,$x,@r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_);
627*0Sstevel@tonic-gate
628*0Sstevel@tonic-gate  return $x if $x->{sign} !~ /^[+-]$/;	# NaN, inf, -inf
629*0Sstevel@tonic-gate
630*0Sstevel@tonic-gate  local $Math::BigInt::accuracy = undef;
631*0Sstevel@tonic-gate  local $Math::BigInt::precision = undef;
632*0Sstevel@tonic-gate  if ($x->{sign} eq '-')
633*0Sstevel@tonic-gate    {
634*0Sstevel@tonic-gate    $x->{_n}->badd($x->{_d});	# -5/2 => -7/2
635*0Sstevel@tonic-gate    }
636*0Sstevel@tonic-gate  else
637*0Sstevel@tonic-gate    {
638*0Sstevel@tonic-gate    if ($x->{_n}->bacmp($x->{_d}) < 0)
639*0Sstevel@tonic-gate      {
640*0Sstevel@tonic-gate      # 1/3 -- => -2/3
641*0Sstevel@tonic-gate      $x->{_n} = $x->{_d} - $x->{_n};
642*0Sstevel@tonic-gate      $x->{sign} = '-';
643*0Sstevel@tonic-gate      }
644*0Sstevel@tonic-gate    else
645*0Sstevel@tonic-gate      {
646*0Sstevel@tonic-gate      $x->{_n}->bsub($x->{_d});		# 5/2 => 3/2
647*0Sstevel@tonic-gate      }
648*0Sstevel@tonic-gate    }
649*0Sstevel@tonic-gate  $x->bnorm()->round(@r);
650*0Sstevel@tonic-gate  }
651*0Sstevel@tonic-gate
652*0Sstevel@tonic-gatesub binc
653*0Sstevel@tonic-gate  {
654*0Sstevel@tonic-gate  # increment value (add 1)
655*0Sstevel@tonic-gate  my ($self,$x,@r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_);
656*0Sstevel@tonic-gate
657*0Sstevel@tonic-gate  return $x if $x->{sign} !~ /^[+-]$/;	# NaN, inf, -inf
658*0Sstevel@tonic-gate
659*0Sstevel@tonic-gate  local $Math::BigInt::accuracy = undef;
660*0Sstevel@tonic-gate  local $Math::BigInt::precision = undef;
661*0Sstevel@tonic-gate  if ($x->{sign} eq '-')
662*0Sstevel@tonic-gate    {
663*0Sstevel@tonic-gate    if ($x->{_n}->bacmp($x->{_d}) < 0)
664*0Sstevel@tonic-gate      {
665*0Sstevel@tonic-gate      # -1/3 ++ => 2/3 (overflow at 0)
666*0Sstevel@tonic-gate      $x->{_n} = $x->{_d} - $x->{_n};
667*0Sstevel@tonic-gate      $x->{sign} = '+';
668*0Sstevel@tonic-gate      }
669*0Sstevel@tonic-gate    else
670*0Sstevel@tonic-gate      {
671*0Sstevel@tonic-gate      $x->{_n}->bsub($x->{_d});		# -5/2 => -3/2
672*0Sstevel@tonic-gate      }
673*0Sstevel@tonic-gate    }
674*0Sstevel@tonic-gate  else
675*0Sstevel@tonic-gate    {
676*0Sstevel@tonic-gate    $x->{_n}->badd($x->{_d});	# 5/2 => 7/2
677*0Sstevel@tonic-gate    }
678*0Sstevel@tonic-gate  $x->bnorm()->round(@r);
679*0Sstevel@tonic-gate  }
680*0Sstevel@tonic-gate
681*0Sstevel@tonic-gate##############################################################################
682*0Sstevel@tonic-gate# is_foo methods (the rest is inherited)
683*0Sstevel@tonic-gate
684*0Sstevel@tonic-gatesub is_int
685*0Sstevel@tonic-gate  {
686*0Sstevel@tonic-gate  # return true if arg (BRAT or num_str) is an integer
687*0Sstevel@tonic-gate  my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_);
688*0Sstevel@tonic-gate
689*0Sstevel@tonic-gate  return 1 if ($x->{sign} =~ /^[+-]$/) &&	# NaN and +-inf aren't
690*0Sstevel@tonic-gate    $x->{_d}->is_one();				# x/y && y != 1 => no integer
691*0Sstevel@tonic-gate  0;
692*0Sstevel@tonic-gate  }
693*0Sstevel@tonic-gate
694*0Sstevel@tonic-gatesub is_zero
695*0Sstevel@tonic-gate  {
696*0Sstevel@tonic-gate  # return true if arg (BRAT or num_str) is zero
697*0Sstevel@tonic-gate  my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_);
698*0Sstevel@tonic-gate
699*0Sstevel@tonic-gate  return 1 if $x->{sign} eq '+' && $x->{_n}->is_zero();
700*0Sstevel@tonic-gate  0;
701*0Sstevel@tonic-gate  }
702*0Sstevel@tonic-gate
703*0Sstevel@tonic-gatesub is_one
704*0Sstevel@tonic-gate  {
705*0Sstevel@tonic-gate  # return true if arg (BRAT or num_str) is +1 or -1 if signis given
706*0Sstevel@tonic-gate  my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_);
707*0Sstevel@tonic-gate
708*0Sstevel@tonic-gate  my $sign = $_[2] || ''; $sign = '+' if $sign ne '-';
709*0Sstevel@tonic-gate  return 1
710*0Sstevel@tonic-gate   if ($x->{sign} eq $sign && $x->{_n}->is_one() && $x->{_d}->is_one());
711*0Sstevel@tonic-gate  0;
712*0Sstevel@tonic-gate  }
713*0Sstevel@tonic-gate
714*0Sstevel@tonic-gatesub is_odd
715*0Sstevel@tonic-gate  {
716*0Sstevel@tonic-gate  # return true if arg (BFLOAT or num_str) is odd or false if even
717*0Sstevel@tonic-gate  my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_);
718*0Sstevel@tonic-gate
719*0Sstevel@tonic-gate  return 1 if ($x->{sign} =~ /^[+-]$/) &&		# NaN & +-inf aren't
720*0Sstevel@tonic-gate    ($x->{_d}->is_one() && $x->{_n}->is_odd());		# x/2 is not, but 3/1
721*0Sstevel@tonic-gate  0;
722*0Sstevel@tonic-gate  }
723*0Sstevel@tonic-gate
724*0Sstevel@tonic-gatesub is_even
725*0Sstevel@tonic-gate  {
726*0Sstevel@tonic-gate  # return true if arg (BINT or num_str) is even or false if odd
727*0Sstevel@tonic-gate  my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_);
728*0Sstevel@tonic-gate
729*0Sstevel@tonic-gate  return 0 if $x->{sign} !~ /^[+-]$/;			# NaN & +-inf aren't
730*0Sstevel@tonic-gate  return 1 if ($x->{_d}->is_one()			# x/3 is never
731*0Sstevel@tonic-gate     && $x->{_n}->is_even());				# but 4/1 is
732*0Sstevel@tonic-gate  0;
733*0Sstevel@tonic-gate  }
734*0Sstevel@tonic-gate
735*0Sstevel@tonic-gate##############################################################################
736*0Sstevel@tonic-gate# parts() and friends
737*0Sstevel@tonic-gate
738*0Sstevel@tonic-gatesub numerator
739*0Sstevel@tonic-gate  {
740*0Sstevel@tonic-gate  my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_);
741*0Sstevel@tonic-gate
742*0Sstevel@tonic-gate  return $MBI->new($x->{sign}) if ($x->{sign} !~ /^[+-]$/);
743*0Sstevel@tonic-gate
744*0Sstevel@tonic-gate  my $n = $x->{_n}->copy(); $n->{sign} = $x->{sign};
745*0Sstevel@tonic-gate  $n;
746*0Sstevel@tonic-gate  }
747*0Sstevel@tonic-gate
748*0Sstevel@tonic-gatesub denominator
749*0Sstevel@tonic-gate  {
750*0Sstevel@tonic-gate  my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_);
751*0Sstevel@tonic-gate
752*0Sstevel@tonic-gate  return $MBI->new($x->{sign}) if ($x->{sign} !~ /^[+-]$/);
753*0Sstevel@tonic-gate  $x->{_d}->copy();
754*0Sstevel@tonic-gate  }
755*0Sstevel@tonic-gate
756*0Sstevel@tonic-gatesub parts
757*0Sstevel@tonic-gate  {
758*0Sstevel@tonic-gate  my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_);
759*0Sstevel@tonic-gate
760*0Sstevel@tonic-gate  return ($self->bnan(),$self->bnan()) if $x->{sign} eq 'NaN';
761*0Sstevel@tonic-gate  return ($self->binf(),$self->binf()) if $x->{sign} eq '+inf';
762*0Sstevel@tonic-gate  return ($self->binf('-'),$self->binf()) if $x->{sign} eq '-inf';
763*0Sstevel@tonic-gate
764*0Sstevel@tonic-gate  my $n = $x->{_n}->copy();
765*0Sstevel@tonic-gate  $n->{sign} = $x->{sign};
766*0Sstevel@tonic-gate  return ($n,$x->{_d}->copy());
767*0Sstevel@tonic-gate  }
768*0Sstevel@tonic-gate
769*0Sstevel@tonic-gatesub length
770*0Sstevel@tonic-gate  {
771*0Sstevel@tonic-gate  my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_);
772*0Sstevel@tonic-gate
773*0Sstevel@tonic-gate  return $nan unless $x->is_int();
774*0Sstevel@tonic-gate  $x->{_n}->length();			# length(-123/1) => length(123)
775*0Sstevel@tonic-gate  }
776*0Sstevel@tonic-gate
777*0Sstevel@tonic-gatesub digit
778*0Sstevel@tonic-gate  {
779*0Sstevel@tonic-gate  my ($self,$x,$n) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_);
780*0Sstevel@tonic-gate
781*0Sstevel@tonic-gate  return $nan unless $x->is_int();
782*0Sstevel@tonic-gate  $x->{_n}->digit($n);			# digit(-123/1,2) => digit(123,2)
783*0Sstevel@tonic-gate  }
784*0Sstevel@tonic-gate
785*0Sstevel@tonic-gate##############################################################################
786*0Sstevel@tonic-gate# special calc routines
787*0Sstevel@tonic-gate
788*0Sstevel@tonic-gatesub bceil
789*0Sstevel@tonic-gate  {
790*0Sstevel@tonic-gate  my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_);
791*0Sstevel@tonic-gate
792*0Sstevel@tonic-gate  return $x unless $x->{sign} =~ /^[+-]$/;
793*0Sstevel@tonic-gate  return $x if $x->{_d}->is_one();		# 22/1 => 22, 0/1 => 0
794*0Sstevel@tonic-gate
795*0Sstevel@tonic-gate  local $Math::BigInt::upgrade = undef;
796*0Sstevel@tonic-gate  local $Math::BigInt::accuracy = undef;
797*0Sstevel@tonic-gate  local $Math::BigInt::precision = undef;
798*0Sstevel@tonic-gate  $x->{_n}->bdiv($x->{_d});			# 22/7 => 3/1 w/ truncate
799*0Sstevel@tonic-gate  $x->{_d}->bone();
800*0Sstevel@tonic-gate  $x->{_n}->binc() if $x->{sign} eq '+';	# +22/7 => 4/1
801*0Sstevel@tonic-gate  $x->{sign} = '+' if $x->{_n}->is_zero();	# -0 => 0
802*0Sstevel@tonic-gate  $x;
803*0Sstevel@tonic-gate  }
804*0Sstevel@tonic-gate
805*0Sstevel@tonic-gatesub bfloor
806*0Sstevel@tonic-gate  {
807*0Sstevel@tonic-gate  my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_);
808*0Sstevel@tonic-gate
809*0Sstevel@tonic-gate  return $x unless $x->{sign} =~ /^[+-]$/;
810*0Sstevel@tonic-gate  return $x if $x->{_d}->is_one();		# 22/1 => 22, 0/1 => 0
811*0Sstevel@tonic-gate
812*0Sstevel@tonic-gate  local $Math::BigInt::upgrade = undef;
813*0Sstevel@tonic-gate  local $Math::BigInt::accuracy = undef;
814*0Sstevel@tonic-gate  local $Math::BigInt::precision = undef;
815*0Sstevel@tonic-gate  $x->{_n}->bdiv($x->{_d});			# 22/7 => 3/1 w/ truncate
816*0Sstevel@tonic-gate  $x->{_d}->bone();
817*0Sstevel@tonic-gate  $x->{_n}->binc() if $x->{sign} eq '-';	# -22/7 => -4/1
818*0Sstevel@tonic-gate  $x;
819*0Sstevel@tonic-gate  }
820*0Sstevel@tonic-gate
821*0Sstevel@tonic-gatesub bfac
822*0Sstevel@tonic-gate  {
823*0Sstevel@tonic-gate  my ($self,$x,@r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_);
824*0Sstevel@tonic-gate
825*0Sstevel@tonic-gate  # if $x is an integer
826*0Sstevel@tonic-gate  if (($x->{sign} eq '+') && ($x->{_d}->is_one()))
827*0Sstevel@tonic-gate    {
828*0Sstevel@tonic-gate    $x->{_n}->bfac();
829*0Sstevel@tonic-gate    return $x->round(@r);
830*0Sstevel@tonic-gate    }
831*0Sstevel@tonic-gate  $x->bnan();
832*0Sstevel@tonic-gate  }
833*0Sstevel@tonic-gate
834*0Sstevel@tonic-gatesub bpow
835*0Sstevel@tonic-gate  {
836*0Sstevel@tonic-gate  # power ($x ** $y)
837*0Sstevel@tonic-gate
838*0Sstevel@tonic-gate  # set up parameters
839*0Sstevel@tonic-gate  my ($self,$x,$y,@r) = (ref($_[0]),@_);
840*0Sstevel@tonic-gate  # objectify is costly, so avoid it
841*0Sstevel@tonic-gate  if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1])))
842*0Sstevel@tonic-gate    {
843*0Sstevel@tonic-gate    ($self,$x,$y,@r) = objectify(2,@_);
844*0Sstevel@tonic-gate    }
845*0Sstevel@tonic-gate
846*0Sstevel@tonic-gate  return $x if $x->{sign} =~ /^[+-]inf$/;       # -inf/+inf ** x
847*0Sstevel@tonic-gate  return $x->bnan() if $x->{sign} eq $nan || $y->{sign} eq $nan;
848*0Sstevel@tonic-gate  return $x->bone(@r) if $y->is_zero();
849*0Sstevel@tonic-gate  return $x->round(@r) if $x->is_one() || $y->is_one();
850*0Sstevel@tonic-gate  if ($x->{sign} eq '-' && $x->{_n}->is_one() && $x->{_d}->is_one())
851*0Sstevel@tonic-gate    {
852*0Sstevel@tonic-gate    # if $x == -1 and odd/even y => +1/-1
853*0Sstevel@tonic-gate    return $y->is_odd() ? $x->round(@r) : $x->babs()->round(@r);
854*0Sstevel@tonic-gate    # my Casio FX-5500L has a bug here: -1 ** 2 is -1, but -1 * -1 is 1;
855*0Sstevel@tonic-gate    }
856*0Sstevel@tonic-gate  # 1 ** -y => 1 / (1 ** |y|)
857*0Sstevel@tonic-gate  # so do test for negative $y after above's clause
858*0Sstevel@tonic-gate #  return $x->bnan() if $y->{sign} eq '-';
859*0Sstevel@tonic-gate  return $x->round(@r) if $x->is_zero();  # 0**y => 0 (if not y <= 0)
860*0Sstevel@tonic-gate
861*0Sstevel@tonic-gate  # shortcut y/1 (and/or x/1)
862*0Sstevel@tonic-gate  if ($y->{_d}->is_one())
863*0Sstevel@tonic-gate    {
864*0Sstevel@tonic-gate    # shortcut for x/1 and y/1
865*0Sstevel@tonic-gate    if ($x->{_d}->is_one())
866*0Sstevel@tonic-gate      {
867*0Sstevel@tonic-gate      $x->{_n}->bpow($y->{_n});		# x/1 ** y/1 => (x ** y)/1
868*0Sstevel@tonic-gate      if ($y->{sign} eq '-')
869*0Sstevel@tonic-gate        {
870*0Sstevel@tonic-gate        # 0.2 ** -3 => 1/(0.2 ** 3)
871*0Sstevel@tonic-gate        ($x->{_n},$x->{_d}) = ($x->{_d},$x->{_n});	# swap
872*0Sstevel@tonic-gate        }
873*0Sstevel@tonic-gate      # correct sign; + ** + => +
874*0Sstevel@tonic-gate      if ($x->{sign} eq '-')
875*0Sstevel@tonic-gate        {
876*0Sstevel@tonic-gate        # - * - => +, - * - * - => -
877*0Sstevel@tonic-gate        $x->{sign} = '+' if $y->{_n}->is_even();
878*0Sstevel@tonic-gate        }
879*0Sstevel@tonic-gate      return $x->round(@r);
880*0Sstevel@tonic-gate      }
881*0Sstevel@tonic-gate    # x/z ** y/1
882*0Sstevel@tonic-gate    $x->{_n}->bpow($y->{_n});		# 5/2 ** y/1 => 5 ** y / 2 ** y
883*0Sstevel@tonic-gate    $x->{_d}->bpow($y->{_n});
884*0Sstevel@tonic-gate    if ($y->{sign} eq '-')
885*0Sstevel@tonic-gate      {
886*0Sstevel@tonic-gate      # 0.2 ** -3 => 1/(0.2 ** 3)
887*0Sstevel@tonic-gate      ($x->{_n},$x->{_d}) = ($x->{_d},$x->{_n});	# swap
888*0Sstevel@tonic-gate      }
889*0Sstevel@tonic-gate    # correct sign; + ** + => +
890*0Sstevel@tonic-gate    if ($x->{sign} eq '-')
891*0Sstevel@tonic-gate      {
892*0Sstevel@tonic-gate      # - * - => +, - * - * - => -
893*0Sstevel@tonic-gate      $x->{sign} = '+' if $y->{_n}->is_even();
894*0Sstevel@tonic-gate      }
895*0Sstevel@tonic-gate    return $x->round(@r);
896*0Sstevel@tonic-gate    }
897*0Sstevel@tonic-gate
898*0Sstevel@tonic-gate  # regular calculation (this is wrong for d/e ** f/g)
899*0Sstevel@tonic-gate  my $pow2 = $self->__one();
900*0Sstevel@tonic-gate  my $y1 = $MBI->new($y->{_n}/$y->{_d})->babs();
901*0Sstevel@tonic-gate  my $two = $MBI->new(2);
902*0Sstevel@tonic-gate  while (!$y1->is_one())
903*0Sstevel@tonic-gate    {
904*0Sstevel@tonic-gate    $pow2->bmul($x) if $y1->is_odd();
905*0Sstevel@tonic-gate    $y1->bdiv($two);
906*0Sstevel@tonic-gate    $x->bmul($x);
907*0Sstevel@tonic-gate    }
908*0Sstevel@tonic-gate  $x->bmul($pow2) unless $pow2->is_one();
909*0Sstevel@tonic-gate  # n ** -x => 1/n ** x
910*0Sstevel@tonic-gate  ($x->{_d},$x->{_n}) = ($x->{_n},$x->{_d}) if $y->{sign} eq '-';
911*0Sstevel@tonic-gate  $x->bnorm()->round(@r);
912*0Sstevel@tonic-gate  }
913*0Sstevel@tonic-gate
914*0Sstevel@tonic-gatesub blog
915*0Sstevel@tonic-gate  {
916*0Sstevel@tonic-gate  # set up parameters
917*0Sstevel@tonic-gate  my ($self,$x,$y,@r) = (ref($_[0]),@_);
918*0Sstevel@tonic-gate
919*0Sstevel@tonic-gate  # objectify is costly, so avoid it
920*0Sstevel@tonic-gate  if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1])))
921*0Sstevel@tonic-gate    {
922*0Sstevel@tonic-gate    ($self,$x,$y,@r) = objectify(2,$class,@_);
923*0Sstevel@tonic-gate    }
924*0Sstevel@tonic-gate
925*0Sstevel@tonic-gate  # blog(1,Y) => 0
926*0Sstevel@tonic-gate  return $x->bzero() if $x->is_one() && $y->{sign} eq '+';
927*0Sstevel@tonic-gate
928*0Sstevel@tonic-gate  # $x <= 0 => NaN
929*0Sstevel@tonic-gate  return $x->bnan() if $x->is_zero() || $x->{sign} ne '+' || $y->{sign} ne '+';
930*0Sstevel@tonic-gate
931*0Sstevel@tonic-gate  if ($x->is_int() && $y->is_int())
932*0Sstevel@tonic-gate    {
933*0Sstevel@tonic-gate    return $self->new($x->as_number()->blog($y->as_number(),@r));
934*0Sstevel@tonic-gate    }
935*0Sstevel@tonic-gate
936*0Sstevel@tonic-gate  # do it with floats
937*0Sstevel@tonic-gate  $x->_new_from_float( $x->_as_float()->blog(Math::BigFloat->new("$y"),@r) );
938*0Sstevel@tonic-gate  }
939*0Sstevel@tonic-gate
940*0Sstevel@tonic-gatesub _as_float
941*0Sstevel@tonic-gate  {
942*0Sstevel@tonic-gate  my $x = shift;
943*0Sstevel@tonic-gate
944*0Sstevel@tonic-gate  local $Math::BigFloat::upgrade = undef;
945*0Sstevel@tonic-gate  local $Math::BigFloat::accuracy = undef;
946*0Sstevel@tonic-gate  local $Math::BigFloat::precision = undef;
947*0Sstevel@tonic-gate  # 22/7 => 3.142857143..
948*0Sstevel@tonic-gate  Math::BigFloat->new($x->{_n})->bdiv($x->{_d}, $x->accuracy());
949*0Sstevel@tonic-gate  }
950*0Sstevel@tonic-gate
951*0Sstevel@tonic-gatesub broot
952*0Sstevel@tonic-gate  {
953*0Sstevel@tonic-gate  # set up parameters
954*0Sstevel@tonic-gate  my ($self,$x,$y,@r) = (ref($_[0]),@_);
955*0Sstevel@tonic-gate  # objectify is costly, so avoid it
956*0Sstevel@tonic-gate  if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1])))
957*0Sstevel@tonic-gate    {
958*0Sstevel@tonic-gate    ($self,$x,$y,@r) = objectify(2,@_);
959*0Sstevel@tonic-gate    }
960*0Sstevel@tonic-gate
961*0Sstevel@tonic-gate  if ($x->is_int() && $y->is_int())
962*0Sstevel@tonic-gate    {
963*0Sstevel@tonic-gate    return $self->new($x->as_number()->broot($y->as_number(),@r));
964*0Sstevel@tonic-gate    }
965*0Sstevel@tonic-gate
966*0Sstevel@tonic-gate  # do it with floats
967*0Sstevel@tonic-gate  $x->_new_from_float( $x->_as_float()->broot($y,@r) );
968*0Sstevel@tonic-gate  }
969*0Sstevel@tonic-gate
970*0Sstevel@tonic-gatesub bmodpow
971*0Sstevel@tonic-gate  {
972*0Sstevel@tonic-gate  # set up parameters
973*0Sstevel@tonic-gate  my ($self,$x,$y,$m,@r) = (ref($_[0]),@_);
974*0Sstevel@tonic-gate  # objectify is costly, so avoid it
975*0Sstevel@tonic-gate  if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1])))
976*0Sstevel@tonic-gate    {
977*0Sstevel@tonic-gate    ($self,$x,$y,$m,@r) = objectify(3,@_);
978*0Sstevel@tonic-gate    }
979*0Sstevel@tonic-gate
980*0Sstevel@tonic-gate  # $x or $y or $m are NaN or +-inf => NaN
981*0Sstevel@tonic-gate  return $x->bnan()
982*0Sstevel@tonic-gate   if $x->{sign} !~ /^[+-]$/ || $y->{sign} !~ /^[+-]$/ ||
983*0Sstevel@tonic-gate   $m->{sign} !~ /^[+-]$/;
984*0Sstevel@tonic-gate
985*0Sstevel@tonic-gate  if ($x->is_int() && $y->is_int() && $m->is_int())
986*0Sstevel@tonic-gate    {
987*0Sstevel@tonic-gate    return $self->new($x->as_number()->bmodpow($y->as_number(),$m,@r));
988*0Sstevel@tonic-gate    }
989*0Sstevel@tonic-gate
990*0Sstevel@tonic-gate  warn ("bmodpow() not fully implemented");
991*0Sstevel@tonic-gate  $x->bnan();
992*0Sstevel@tonic-gate  }
993*0Sstevel@tonic-gate
994*0Sstevel@tonic-gatesub bmodinv
995*0Sstevel@tonic-gate  {
996*0Sstevel@tonic-gate  # set up parameters
997*0Sstevel@tonic-gate  my ($self,$x,$y,@r) = (ref($_[0]),@_);
998*0Sstevel@tonic-gate  # objectify is costly, so avoid it
999*0Sstevel@tonic-gate  if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1])))
1000*0Sstevel@tonic-gate    {
1001*0Sstevel@tonic-gate    ($self,$x,$y,@r) = objectify(2,@_);
1002*0Sstevel@tonic-gate    }
1003*0Sstevel@tonic-gate
1004*0Sstevel@tonic-gate  # $x or $y are NaN or +-inf => NaN
1005*0Sstevel@tonic-gate  return $x->bnan()
1006*0Sstevel@tonic-gate   if $x->{sign} !~ /^[+-]$/ || $y->{sign} !~ /^[+-]$/;
1007*0Sstevel@tonic-gate
1008*0Sstevel@tonic-gate  if ($x->is_int() && $y->is_int())
1009*0Sstevel@tonic-gate    {
1010*0Sstevel@tonic-gate    return $self->new($x->as_number()->bmodinv($y->as_number(),@r));
1011*0Sstevel@tonic-gate    }
1012*0Sstevel@tonic-gate
1013*0Sstevel@tonic-gate  warn ("bmodinv() not fully implemented");
1014*0Sstevel@tonic-gate  $x->bnan();
1015*0Sstevel@tonic-gate  }
1016*0Sstevel@tonic-gate
1017*0Sstevel@tonic-gatesub bsqrt
1018*0Sstevel@tonic-gate  {
1019*0Sstevel@tonic-gate  my ($self,$x,@r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_);
1020*0Sstevel@tonic-gate
1021*0Sstevel@tonic-gate  return $x->bnan() if $x->{sign} !~ /^[+]/;    # NaN, -inf or < 0
1022*0Sstevel@tonic-gate  return $x if $x->{sign} eq '+inf';            # sqrt(inf) == inf
1023*0Sstevel@tonic-gate  return $x->round(@r) if $x->is_zero() || $x->is_one();
1024*0Sstevel@tonic-gate
1025*0Sstevel@tonic-gate  local $Math::BigFloat::upgrade = undef;
1026*0Sstevel@tonic-gate  local $Math::BigFloat::downgrade = undef;
1027*0Sstevel@tonic-gate  local $Math::BigFloat::precision = undef;
1028*0Sstevel@tonic-gate  local $Math::BigFloat::accuracy = undef;
1029*0Sstevel@tonic-gate  local $Math::BigInt::upgrade = undef;
1030*0Sstevel@tonic-gate  local $Math::BigInt::precision = undef;
1031*0Sstevel@tonic-gate  local $Math::BigInt::accuracy = undef;
1032*0Sstevel@tonic-gate
1033*0Sstevel@tonic-gate  $x->{_d} = Math::BigFloat->new($x->{_d})->bsqrt();
1034*0Sstevel@tonic-gate  $x->{_n} = Math::BigFloat->new($x->{_n})->bsqrt();
1035*0Sstevel@tonic-gate
1036*0Sstevel@tonic-gate  # if sqrt(D) was not integer
1037*0Sstevel@tonic-gate  if ($x->{_d}->{_es} ne '+')
1038*0Sstevel@tonic-gate    {
1039*0Sstevel@tonic-gate    $x->{_n}->blsft($x->{_d}->exponent()->babs(),10);	# 7.1/4.51 => 7.1/45.1
1040*0Sstevel@tonic-gate    $x->{_d} = $MBI->new($CALC->_str($x->{_d}->{_m}));	# 7.1/45.1 => 71/45.1
1041*0Sstevel@tonic-gate    }
1042*0Sstevel@tonic-gate  # if sqrt(N) was not integer
1043*0Sstevel@tonic-gate  if ($x->{_n}->{_es} ne '+')
1044*0Sstevel@tonic-gate    {
1045*0Sstevel@tonic-gate    $x->{_d}->blsft($x->{_n}->exponent()->babs(),10);	# 71/45.1 => 710/45.1
1046*0Sstevel@tonic-gate    $x->{_n} = $MBI->new($CALC->_str($x->{_n}->{_m}));	# 710/45.1 => 710/451
1047*0Sstevel@tonic-gate    }
1048*0Sstevel@tonic-gate
1049*0Sstevel@tonic-gate  # convert parts to $MBI again
1050*0Sstevel@tonic-gate  $x->{_n} = $x->{_n}->as_number() unless $x->{_n}->isa($MBI);
1051*0Sstevel@tonic-gate  $x->{_d} = $x->{_d}->as_number() unless $x->{_d}->isa($MBI);
1052*0Sstevel@tonic-gate  $x->bnorm()->round(@r);
1053*0Sstevel@tonic-gate  }
1054*0Sstevel@tonic-gate
1055*0Sstevel@tonic-gatesub blsft
1056*0Sstevel@tonic-gate  {
1057*0Sstevel@tonic-gate  my ($self,$x,$y,$b,@r) = objectify(3,@_);
1058*0Sstevel@tonic-gate
1059*0Sstevel@tonic-gate  $b = 2 unless defined $b;
1060*0Sstevel@tonic-gate  $b = $self->new($b) unless ref ($b);
1061*0Sstevel@tonic-gate  $x->bmul( $b->copy()->bpow($y), @r);
1062*0Sstevel@tonic-gate  $x;
1063*0Sstevel@tonic-gate  }
1064*0Sstevel@tonic-gate
1065*0Sstevel@tonic-gatesub brsft
1066*0Sstevel@tonic-gate  {
1067*0Sstevel@tonic-gate  my ($self,$x,$y,$b,@r) = objectify(2,@_);
1068*0Sstevel@tonic-gate
1069*0Sstevel@tonic-gate  $b = 2 unless defined $b;
1070*0Sstevel@tonic-gate  $b = $self->new($b) unless ref ($b);
1071*0Sstevel@tonic-gate  $x->bdiv( $b->copy()->bpow($y), @r);
1072*0Sstevel@tonic-gate  $x;
1073*0Sstevel@tonic-gate  }
1074*0Sstevel@tonic-gate
1075*0Sstevel@tonic-gate##############################################################################
1076*0Sstevel@tonic-gate# round
1077*0Sstevel@tonic-gate
1078*0Sstevel@tonic-gatesub round
1079*0Sstevel@tonic-gate  {
1080*0Sstevel@tonic-gate  $_[0];
1081*0Sstevel@tonic-gate  }
1082*0Sstevel@tonic-gate
1083*0Sstevel@tonic-gatesub bround
1084*0Sstevel@tonic-gate  {
1085*0Sstevel@tonic-gate  $_[0];
1086*0Sstevel@tonic-gate  }
1087*0Sstevel@tonic-gate
1088*0Sstevel@tonic-gatesub bfround
1089*0Sstevel@tonic-gate  {
1090*0Sstevel@tonic-gate  $_[0];
1091*0Sstevel@tonic-gate  }
1092*0Sstevel@tonic-gate
1093*0Sstevel@tonic-gate##############################################################################
1094*0Sstevel@tonic-gate# comparing
1095*0Sstevel@tonic-gate
1096*0Sstevel@tonic-gatesub bcmp
1097*0Sstevel@tonic-gate  {
1098*0Sstevel@tonic-gate  # compare two signed numbers
1099*0Sstevel@tonic-gate
1100*0Sstevel@tonic-gate  # set up parameters
1101*0Sstevel@tonic-gate  my ($self,$x,$y) = (ref($_[0]),@_);
1102*0Sstevel@tonic-gate  # objectify is costly, so avoid it
1103*0Sstevel@tonic-gate  if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1])))
1104*0Sstevel@tonic-gate    {
1105*0Sstevel@tonic-gate    ($self,$x,$y) = objectify(2,@_);
1106*0Sstevel@tonic-gate    }
1107*0Sstevel@tonic-gate
1108*0Sstevel@tonic-gate  if (($x->{sign} !~ /^[+-]$/) || ($y->{sign} !~ /^[+-]$/))
1109*0Sstevel@tonic-gate    {
1110*0Sstevel@tonic-gate    # handle +-inf and NaN
1111*0Sstevel@tonic-gate    return undef if (($x->{sign} eq $nan) || ($y->{sign} eq $nan));
1112*0Sstevel@tonic-gate    return 0 if $x->{sign} eq $y->{sign} && $x->{sign} =~ /^[+-]inf$/;
1113*0Sstevel@tonic-gate    return +1 if $x->{sign} eq '+inf';
1114*0Sstevel@tonic-gate    return -1 if $x->{sign} eq '-inf';
1115*0Sstevel@tonic-gate    return -1 if $y->{sign} eq '+inf';
1116*0Sstevel@tonic-gate    return +1;
1117*0Sstevel@tonic-gate    }
1118*0Sstevel@tonic-gate  # check sign for speed first
1119*0Sstevel@tonic-gate  return 1 if $x->{sign} eq '+' && $y->{sign} eq '-';   # does also 0 <=> -y
1120*0Sstevel@tonic-gate  return -1 if $x->{sign} eq '-' && $y->{sign} eq '+';  # does also -x <=> 0
1121*0Sstevel@tonic-gate
1122*0Sstevel@tonic-gate  # shortcut
1123*0Sstevel@tonic-gate  my $xz = $x->{_n}->is_zero();
1124*0Sstevel@tonic-gate  my $yz = $y->{_n}->is_zero();
1125*0Sstevel@tonic-gate  return 0 if $xz && $yz;                               # 0 <=> 0
1126*0Sstevel@tonic-gate  return -1 if $xz && $y->{sign} eq '+';                # 0 <=> +y
1127*0Sstevel@tonic-gate  return 1 if $yz && $x->{sign} eq '+';                 # +x <=> 0
1128*0Sstevel@tonic-gate
1129*0Sstevel@tonic-gate  my $t = $x->{_n} * $y->{_d}; $t->{sign} = $x->{sign};
1130*0Sstevel@tonic-gate  my $u = $y->{_n} * $x->{_d}; $u->{sign} = $y->{sign};
1131*0Sstevel@tonic-gate  $t->bcmp($u);
1132*0Sstevel@tonic-gate  }
1133*0Sstevel@tonic-gate
1134*0Sstevel@tonic-gatesub bacmp
1135*0Sstevel@tonic-gate  {
1136*0Sstevel@tonic-gate  # compare two numbers (as unsigned)
1137*0Sstevel@tonic-gate
1138*0Sstevel@tonic-gate  # set up parameters
1139*0Sstevel@tonic-gate  my ($self,$x,$y) = (ref($_[0]),@_);
1140*0Sstevel@tonic-gate  # objectify is costly, so avoid it
1141*0Sstevel@tonic-gate  if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1])))
1142*0Sstevel@tonic-gate    {
1143*0Sstevel@tonic-gate    ($self,$x,$y) = objectify(2,$class,@_);
1144*0Sstevel@tonic-gate    }
1145*0Sstevel@tonic-gate
1146*0Sstevel@tonic-gate  if (($x->{sign} !~ /^[+-]$/) || ($y->{sign} !~ /^[+-]$/))
1147*0Sstevel@tonic-gate    {
1148*0Sstevel@tonic-gate    # handle +-inf and NaN
1149*0Sstevel@tonic-gate    return undef if (($x->{sign} eq $nan) || ($y->{sign} eq $nan));
1150*0Sstevel@tonic-gate    return 0 if $x->{sign} =~ /^[+-]inf$/ && $y->{sign} =~ /^[+-]inf$/;
1151*0Sstevel@tonic-gate    return 1 if $x->{sign} =~ /^[+-]inf$/ && $y->{sign} !~ /^[+-]inf$/;
1152*0Sstevel@tonic-gate    return -1;
1153*0Sstevel@tonic-gate    }
1154*0Sstevel@tonic-gate
1155*0Sstevel@tonic-gate  my $t = $x->{_n} * $y->{_d};
1156*0Sstevel@tonic-gate  my $u = $y->{_n} * $x->{_d};
1157*0Sstevel@tonic-gate  $t->bacmp($u);
1158*0Sstevel@tonic-gate  }
1159*0Sstevel@tonic-gate
1160*0Sstevel@tonic-gate##############################################################################
1161*0Sstevel@tonic-gate# output conversation
1162*0Sstevel@tonic-gate
1163*0Sstevel@tonic-gatesub numify
1164*0Sstevel@tonic-gate  {
1165*0Sstevel@tonic-gate  # convert 17/8 => float (aka 2.125)
1166*0Sstevel@tonic-gate  my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_);
1167*0Sstevel@tonic-gate
1168*0Sstevel@tonic-gate  return $x->bstr() if $x->{sign} !~ /^[+-]$/;	# inf, NaN, etc
1169*0Sstevel@tonic-gate
1170*0Sstevel@tonic-gate  # N/1 => N
1171*0Sstevel@tonic-gate  return $x->{_n}->numify() if $x->{_d}->is_one();
1172*0Sstevel@tonic-gate
1173*0Sstevel@tonic-gate  # N/D
1174*0Sstevel@tonic-gate  my $neg = 1; $neg = -1 if $x->{sign} ne '+';
1175*0Sstevel@tonic-gate  $neg * $x->{_n}->numify() / $x->{_d}->numify();	# return sign * N/D
1176*0Sstevel@tonic-gate  }
1177*0Sstevel@tonic-gate
1178*0Sstevel@tonic-gatesub as_number
1179*0Sstevel@tonic-gate  {
1180*0Sstevel@tonic-gate  my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_);
1181*0Sstevel@tonic-gate
1182*0Sstevel@tonic-gate  return $x if $x->{sign} !~ /^[+-]$/;			# NaN, inf etc
1183*0Sstevel@tonic-gate
1184*0Sstevel@tonic-gate  # need to disable these, otherwise bdiv() gives BigRat again
1185*0Sstevel@tonic-gate  local $Math::BigInt::upgrade = undef;
1186*0Sstevel@tonic-gate  local $Math::BigInt::accuracy = undef;
1187*0Sstevel@tonic-gate  local $Math::BigInt::precision = undef;
1188*0Sstevel@tonic-gate  my $t = $x->{_n}->copy()->bdiv($x->{_d});		# 22/7 => 3
1189*0Sstevel@tonic-gate  $t->{sign} = $x->{sign};
1190*0Sstevel@tonic-gate  $t;
1191*0Sstevel@tonic-gate  }
1192*0Sstevel@tonic-gate
1193*0Sstevel@tonic-gatesub as_bin
1194*0Sstevel@tonic-gate  {
1195*0Sstevel@tonic-gate  my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_);
1196*0Sstevel@tonic-gate
1197*0Sstevel@tonic-gate  return $x unless $x->is_int();
1198*0Sstevel@tonic-gate
1199*0Sstevel@tonic-gate  my $s = $x->{sign}; $s = '' if $s eq '+';
1200*0Sstevel@tonic-gate  $s . $x->{_n}->as_bin();
1201*0Sstevel@tonic-gate  }
1202*0Sstevel@tonic-gate
1203*0Sstevel@tonic-gatesub as_hex
1204*0Sstevel@tonic-gate  {
1205*0Sstevel@tonic-gate  my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_);
1206*0Sstevel@tonic-gate
1207*0Sstevel@tonic-gate  return $x unless $x->is_int();
1208*0Sstevel@tonic-gate
1209*0Sstevel@tonic-gate  my $s = $x->{sign}; $s = '' if $s eq '+';
1210*0Sstevel@tonic-gate  $s . $x->{_n}->as_hex();
1211*0Sstevel@tonic-gate  }
1212*0Sstevel@tonic-gate
1213*0Sstevel@tonic-gatesub import
1214*0Sstevel@tonic-gate  {
1215*0Sstevel@tonic-gate  my $self = shift;
1216*0Sstevel@tonic-gate  my $l = scalar @_;
1217*0Sstevel@tonic-gate  my $lib = ''; my @a;
1218*0Sstevel@tonic-gate  $IMPORT++;
1219*0Sstevel@tonic-gate
1220*0Sstevel@tonic-gate  for ( my $i = 0; $i < $l ; $i++)
1221*0Sstevel@tonic-gate    {
1222*0Sstevel@tonic-gate#    print "at $_[$i] (",$_[$i+1]||'undef',")\n";
1223*0Sstevel@tonic-gate    if ( $_[$i] eq ':constant' )
1224*0Sstevel@tonic-gate      {
1225*0Sstevel@tonic-gate      # this rest causes overlord er load to step in
1226*0Sstevel@tonic-gate      # print "overload @_\n";
1227*0Sstevel@tonic-gate      overload::constant float => sub { $self->new(shift); };
1228*0Sstevel@tonic-gate      }
1229*0Sstevel@tonic-gate#    elsif ($_[$i] eq 'upgrade')
1230*0Sstevel@tonic-gate#      {
1231*0Sstevel@tonic-gate#     # this causes upgrading
1232*0Sstevel@tonic-gate#      $upgrade = $_[$i+1];              # or undef to disable
1233*0Sstevel@tonic-gate#      $i++;
1234*0Sstevel@tonic-gate#      }
1235*0Sstevel@tonic-gate    elsif ($_[$i] eq 'downgrade')
1236*0Sstevel@tonic-gate      {
1237*0Sstevel@tonic-gate      # this causes downgrading
1238*0Sstevel@tonic-gate      $downgrade = $_[$i+1];            # or undef to disable
1239*0Sstevel@tonic-gate      $i++;
1240*0Sstevel@tonic-gate      }
1241*0Sstevel@tonic-gate    elsif ($_[$i] eq 'lib')
1242*0Sstevel@tonic-gate      {
1243*0Sstevel@tonic-gate      $lib = $_[$i+1] || '';            # default Calc
1244*0Sstevel@tonic-gate      $i++;
1245*0Sstevel@tonic-gate      }
1246*0Sstevel@tonic-gate    elsif ($_[$i] eq 'with')
1247*0Sstevel@tonic-gate      {
1248*0Sstevel@tonic-gate      $MBI = $_[$i+1] || 'Math::BigInt';        # default Math::BigInt
1249*0Sstevel@tonic-gate      $i++;
1250*0Sstevel@tonic-gate      }
1251*0Sstevel@tonic-gate    else
1252*0Sstevel@tonic-gate      {
1253*0Sstevel@tonic-gate      push @a, $_[$i];
1254*0Sstevel@tonic-gate      }
1255*0Sstevel@tonic-gate    }
1256*0Sstevel@tonic-gate  # let use Math::BigInt lib => 'GMP'; use Math::BigRat; still work
1257*0Sstevel@tonic-gate  my $mbilib = eval { Math::BigInt->config()->{lib} };
1258*0Sstevel@tonic-gate  if ((defined $mbilib) && ($MBI eq 'Math::BigInt'))
1259*0Sstevel@tonic-gate    {
1260*0Sstevel@tonic-gate    # MBI already loaded
1261*0Sstevel@tonic-gate    $MBI->import('lib',"$lib,$mbilib", 'objectify');
1262*0Sstevel@tonic-gate    }
1263*0Sstevel@tonic-gate  else
1264*0Sstevel@tonic-gate    {
1265*0Sstevel@tonic-gate    # MBI not loaded, or not with "Math::BigInt"
1266*0Sstevel@tonic-gate    $lib .= ",$mbilib" if defined $mbilib;
1267*0Sstevel@tonic-gate
1268*0Sstevel@tonic-gate    if ($] < 5.006)
1269*0Sstevel@tonic-gate      {
1270*0Sstevel@tonic-gate      # Perl < 5.6.0 dies with "out of memory!" when eval() and ':constant' is
1271*0Sstevel@tonic-gate      # used in the same script, or eval inside import().
1272*0Sstevel@tonic-gate      my @parts = split /::/, $MBI;             # Math::BigInt => Math BigInt
1273*0Sstevel@tonic-gate      my $file = pop @parts; $file .= '.pm';    # BigInt => BigInt.pm
1274*0Sstevel@tonic-gate      $file = File::Spec->catfile (@parts, $file);
1275*0Sstevel@tonic-gate      eval { require $file; $MBI->import( lib => '$lib', 'objectify' ); }
1276*0Sstevel@tonic-gate      }
1277*0Sstevel@tonic-gate    else
1278*0Sstevel@tonic-gate      {
1279*0Sstevel@tonic-gate      my $rc = "use $MBI lib => '$lib', 'objectify';";
1280*0Sstevel@tonic-gate      eval $rc;
1281*0Sstevel@tonic-gate      }
1282*0Sstevel@tonic-gate    }
1283*0Sstevel@tonic-gate  if ($@)
1284*0Sstevel@tonic-gate    {
1285*0Sstevel@tonic-gate    require Carp; Carp::croak ("Couldn't load $MBI: $! $@");
1286*0Sstevel@tonic-gate    }
1287*0Sstevel@tonic-gate
1288*0Sstevel@tonic-gate  $CALC = Math::BigFloat->config()->{lib};
1289*0Sstevel@tonic-gate
1290*0Sstevel@tonic-gate  # any non :constant stuff is handled by our parent, Exporter
1291*0Sstevel@tonic-gate  # even if @_ is empty, to give it a chance
1292*0Sstevel@tonic-gate  $self->SUPER::import(@a);             # for subclasses
1293*0Sstevel@tonic-gate  $self->export_to_level(1,$self,@a);   # need this, too
1294*0Sstevel@tonic-gate  }
1295*0Sstevel@tonic-gate
1296*0Sstevel@tonic-gate1;
1297*0Sstevel@tonic-gate
1298*0Sstevel@tonic-gate__END__
1299*0Sstevel@tonic-gate
1300*0Sstevel@tonic-gate=head1 NAME
1301*0Sstevel@tonic-gate
1302*0Sstevel@tonic-gateMath::BigRat - arbitrarily big rational numbers
1303*0Sstevel@tonic-gate
1304*0Sstevel@tonic-gate=head1 SYNOPSIS
1305*0Sstevel@tonic-gate
1306*0Sstevel@tonic-gate	use Math::BigRat;
1307*0Sstevel@tonic-gate
1308*0Sstevel@tonic-gate	my $x = Math::BigRat->new('3/7'); $x += '5/9';
1309*0Sstevel@tonic-gate
1310*0Sstevel@tonic-gate	print $x->bstr(),"\n";
1311*0Sstevel@tonic-gate  	print $x ** 2,"\n";
1312*0Sstevel@tonic-gate
1313*0Sstevel@tonic-gate	my $y = Math::BigRat->new('inf');
1314*0Sstevel@tonic-gate	print "$y ", ($y->is_inf ? 'is' : 'is not') , " infinity\n";
1315*0Sstevel@tonic-gate
1316*0Sstevel@tonic-gate	my $z = Math::BigRat->new(144); $z->bsqrt();
1317*0Sstevel@tonic-gate
1318*0Sstevel@tonic-gate=head1 DESCRIPTION
1319*0Sstevel@tonic-gate
1320*0Sstevel@tonic-gateMath::BigRat complements Math::BigInt and Math::BigFloat by providing support
1321*0Sstevel@tonic-gatefor arbitrarily big rational numbers.
1322*0Sstevel@tonic-gate
1323*0Sstevel@tonic-gate=head2 MATH LIBRARY
1324*0Sstevel@tonic-gate
1325*0Sstevel@tonic-gateMath with the numbers is done (by default) by a module called
1326*0Sstevel@tonic-gateMath::BigInt::Calc. This is equivalent to saying:
1327*0Sstevel@tonic-gate
1328*0Sstevel@tonic-gate	use Math::BigRat lib => 'Calc';
1329*0Sstevel@tonic-gate
1330*0Sstevel@tonic-gateYou can change this by using:
1331*0Sstevel@tonic-gate
1332*0Sstevel@tonic-gate	use Math::BigRat lib => 'BitVect';
1333*0Sstevel@tonic-gate
1334*0Sstevel@tonic-gateThe following would first try to find Math::BigInt::Foo, then
1335*0Sstevel@tonic-gateMath::BigInt::Bar, and when this also fails, revert to Math::BigInt::Calc:
1336*0Sstevel@tonic-gate
1337*0Sstevel@tonic-gate	use Math::BigRat lib => 'Foo,Math::BigInt::Bar';
1338*0Sstevel@tonic-gate
1339*0Sstevel@tonic-gateCalc.pm uses as internal format an array of elements of some decimal base
1340*0Sstevel@tonic-gate(usually 1e7, but this might be different for some systems) with the least
1341*0Sstevel@tonic-gatesignificant digit first, while BitVect.pm uses a bit vector of base 2, most
1342*0Sstevel@tonic-gatesignificant bit first. Other modules might use even different means of
1343*0Sstevel@tonic-gaterepresenting the numbers. See the respective module documentation for further
1344*0Sstevel@tonic-gatedetails.
1345*0Sstevel@tonic-gate
1346*0Sstevel@tonic-gateCurrently the following replacement libraries exist, search for them at CPAN:
1347*0Sstevel@tonic-gate
1348*0Sstevel@tonic-gate	Math::BigInt::BitVect
1349*0Sstevel@tonic-gate	Math::BigInt::GMP
1350*0Sstevel@tonic-gate	Math::BigInt::Pari
1351*0Sstevel@tonic-gate	Math::BigInt::FastCalc
1352*0Sstevel@tonic-gate
1353*0Sstevel@tonic-gate=head1 METHODS
1354*0Sstevel@tonic-gate
1355*0Sstevel@tonic-gateAny methods not listed here are dervied from Math::BigFloat (or
1356*0Sstevel@tonic-gateMath::BigInt), so make sure you check these two modules for further
1357*0Sstevel@tonic-gateinformation.
1358*0Sstevel@tonic-gate
1359*0Sstevel@tonic-gate=head2 new()
1360*0Sstevel@tonic-gate
1361*0Sstevel@tonic-gate	$x = Math::BigRat->new('1/3');
1362*0Sstevel@tonic-gate
1363*0Sstevel@tonic-gateCreate a new Math::BigRat object. Input can come in various forms:
1364*0Sstevel@tonic-gate
1365*0Sstevel@tonic-gate	$x = Math::BigRat->new(123);				# scalars
1366*0Sstevel@tonic-gate	$x = Math::BigRat->new('inf');				# infinity
1367*0Sstevel@tonic-gate	$x = Math::BigRat->new('123.3');			# float
1368*0Sstevel@tonic-gate	$x = Math::BigRat->new('1/3');				# simple string
1369*0Sstevel@tonic-gate	$x = Math::BigRat->new('1 / 3');			# spaced
1370*0Sstevel@tonic-gate	$x = Math::BigRat->new('1 / 0.1');			# w/ floats
1371*0Sstevel@tonic-gate	$x = Math::BigRat->new(Math::BigInt->new(3));		# BigInt
1372*0Sstevel@tonic-gate	$x = Math::BigRat->new(Math::BigFloat->new('3.1'));	# BigFloat
1373*0Sstevel@tonic-gate	$x = Math::BigRat->new(Math::BigInt::Lite->new('2'));	# BigLite
1374*0Sstevel@tonic-gate
1375*0Sstevel@tonic-gate=head2 numerator()
1376*0Sstevel@tonic-gate
1377*0Sstevel@tonic-gate	$n = $x->numerator();
1378*0Sstevel@tonic-gate
1379*0Sstevel@tonic-gateReturns a copy of the numerator (the part above the line) as signed BigInt.
1380*0Sstevel@tonic-gate
1381*0Sstevel@tonic-gate=head2 denominator()
1382*0Sstevel@tonic-gate
1383*0Sstevel@tonic-gate	$d = $x->denominator();
1384*0Sstevel@tonic-gate
1385*0Sstevel@tonic-gateReturns a copy of the denominator (the part under the line) as positive BigInt.
1386*0Sstevel@tonic-gate
1387*0Sstevel@tonic-gate=head2 parts()
1388*0Sstevel@tonic-gate
1389*0Sstevel@tonic-gate	($n,$d) = $x->parts();
1390*0Sstevel@tonic-gate
1391*0Sstevel@tonic-gateReturn a list consisting of (signed) numerator and (unsigned) denominator as
1392*0Sstevel@tonic-gateBigInts.
1393*0Sstevel@tonic-gate
1394*0Sstevel@tonic-gate=head2 as_number()
1395*0Sstevel@tonic-gate
1396*0Sstevel@tonic-gate	$x = Math::BigRat->new('13/7');
1397*0Sstevel@tonic-gate	print $x->as_number(),"\n";		# '1'
1398*0Sstevel@tonic-gate
1399*0Sstevel@tonic-gateReturns a copy of the object as BigInt trunced it to integer.
1400*0Sstevel@tonic-gate
1401*0Sstevel@tonic-gate=head2 bfac()
1402*0Sstevel@tonic-gate
1403*0Sstevel@tonic-gate	$x->bfac();
1404*0Sstevel@tonic-gate
1405*0Sstevel@tonic-gateCalculates the factorial of $x. For instance:
1406*0Sstevel@tonic-gate
1407*0Sstevel@tonic-gate	print Math::BigRat->new('3/1')->bfac(),"\n";	# 1*2*3
1408*0Sstevel@tonic-gate	print Math::BigRat->new('5/1')->bfac(),"\n";	# 1*2*3*4*5
1409*0Sstevel@tonic-gate
1410*0Sstevel@tonic-gateWorks currently only for integers.
1411*0Sstevel@tonic-gate
1412*0Sstevel@tonic-gate=head2 blog()
1413*0Sstevel@tonic-gate
1414*0Sstevel@tonic-gateIs not yet implemented.
1415*0Sstevel@tonic-gate
1416*0Sstevel@tonic-gate=head2 bround()/round()/bfround()
1417*0Sstevel@tonic-gate
1418*0Sstevel@tonic-gateAre not yet implemented.
1419*0Sstevel@tonic-gate
1420*0Sstevel@tonic-gate=head2 bmod()
1421*0Sstevel@tonic-gate
1422*0Sstevel@tonic-gate	use Math::BigRat;
1423*0Sstevel@tonic-gate	my $x = Math::BigRat->new('7/4');
1424*0Sstevel@tonic-gate	my $y = Math::BigRat->new('4/3');
1425*0Sstevel@tonic-gate	print $x->bmod($y);
1426*0Sstevel@tonic-gate
1427*0Sstevel@tonic-gateSet $x to the remainder of the division of $x by $y.
1428*0Sstevel@tonic-gate
1429*0Sstevel@tonic-gate=head2 is_one()
1430*0Sstevel@tonic-gate
1431*0Sstevel@tonic-gate	print "$x is 1\n" if $x->is_one();
1432*0Sstevel@tonic-gate
1433*0Sstevel@tonic-gateReturn true if $x is exactly one, otherwise false.
1434*0Sstevel@tonic-gate
1435*0Sstevel@tonic-gate=head2 is_zero()
1436*0Sstevel@tonic-gate
1437*0Sstevel@tonic-gate	print "$x is 0\n" if $x->is_zero();
1438*0Sstevel@tonic-gate
1439*0Sstevel@tonic-gateReturn true if $x is exactly zero, otherwise false.
1440*0Sstevel@tonic-gate
1441*0Sstevel@tonic-gate=head2 is_positive()
1442*0Sstevel@tonic-gate
1443*0Sstevel@tonic-gate	print "$x is >= 0\n" if $x->is_positive();
1444*0Sstevel@tonic-gate
1445*0Sstevel@tonic-gateReturn true if $x is positive (greater than or equal to zero), otherwise
1446*0Sstevel@tonic-gatefalse. Please note that '+inf' is also positive, while 'NaN' and '-inf' aren't.
1447*0Sstevel@tonic-gate
1448*0Sstevel@tonic-gate=head2 is_negative()
1449*0Sstevel@tonic-gate
1450*0Sstevel@tonic-gate	print "$x is < 0\n" if $x->is_negative();
1451*0Sstevel@tonic-gate
1452*0Sstevel@tonic-gateReturn true if $x is negative (smaller than zero), otherwise false. Please
1453*0Sstevel@tonic-gatenote that '-inf' is also negative, while 'NaN' and '+inf' aren't.
1454*0Sstevel@tonic-gate
1455*0Sstevel@tonic-gate=head2 is_int()
1456*0Sstevel@tonic-gate
1457*0Sstevel@tonic-gate	print "$x is an integer\n" if $x->is_int();
1458*0Sstevel@tonic-gate
1459*0Sstevel@tonic-gateReturn true if $x has a denominator of 1 (e.g. no fraction parts), otherwise
1460*0Sstevel@tonic-gatefalse. Please note that '-inf', 'inf' and 'NaN' aren't integer.
1461*0Sstevel@tonic-gate
1462*0Sstevel@tonic-gate=head2 is_odd()
1463*0Sstevel@tonic-gate
1464*0Sstevel@tonic-gate	print "$x is odd\n" if $x->is_odd();
1465*0Sstevel@tonic-gate
1466*0Sstevel@tonic-gateReturn true if $x is odd, otherwise false.
1467*0Sstevel@tonic-gate
1468*0Sstevel@tonic-gate=head2 is_even()
1469*0Sstevel@tonic-gate
1470*0Sstevel@tonic-gate	print "$x is even\n" if $x->is_even();
1471*0Sstevel@tonic-gate
1472*0Sstevel@tonic-gateReturn true if $x is even, otherwise false.
1473*0Sstevel@tonic-gate
1474*0Sstevel@tonic-gate=head2 bceil()
1475*0Sstevel@tonic-gate
1476*0Sstevel@tonic-gate	$x->bceil();
1477*0Sstevel@tonic-gate
1478*0Sstevel@tonic-gateSet $x to the next bigger integer value (e.g. truncate the number to integer
1479*0Sstevel@tonic-gateand then increment it by one).
1480*0Sstevel@tonic-gate
1481*0Sstevel@tonic-gate=head2 bfloor()
1482*0Sstevel@tonic-gate
1483*0Sstevel@tonic-gate	$x->bfloor();
1484*0Sstevel@tonic-gate
1485*0Sstevel@tonic-gateTruncate $x to an integer value.
1486*0Sstevel@tonic-gate
1487*0Sstevel@tonic-gate=head2 bsqrt()
1488*0Sstevel@tonic-gate
1489*0Sstevel@tonic-gate	$x->bsqrt();
1490*0Sstevel@tonic-gate
1491*0Sstevel@tonic-gateCalculate the square root of $x.
1492*0Sstevel@tonic-gate
1493*0Sstevel@tonic-gate=head2 config
1494*0Sstevel@tonic-gate
1495*0Sstevel@tonic-gate        use Data::Dumper;
1496*0Sstevel@tonic-gate
1497*0Sstevel@tonic-gate        print Dumper ( Math::BigRat->config() );
1498*0Sstevel@tonic-gate        print Math::BigRat->config()->{lib},"\n";
1499*0Sstevel@tonic-gate
1500*0Sstevel@tonic-gateReturns a hash containing the configuration, e.g. the version number, lib
1501*0Sstevel@tonic-gateloaded etc. The following hash keys are currently filled in with the
1502*0Sstevel@tonic-gateappropriate information.
1503*0Sstevel@tonic-gate
1504*0Sstevel@tonic-gate        key             RO/RW   Description
1505*0Sstevel@tonic-gate                                Example
1506*0Sstevel@tonic-gate        ============================================================
1507*0Sstevel@tonic-gate        lib             RO      Name of the Math library
1508*0Sstevel@tonic-gate                                Math::BigInt::Calc
1509*0Sstevel@tonic-gate        lib_version     RO      Version of 'lib'
1510*0Sstevel@tonic-gate                                0.30
1511*0Sstevel@tonic-gate        class           RO      The class of config you just called
1512*0Sstevel@tonic-gate                                Math::BigRat
1513*0Sstevel@tonic-gate        version         RO      version number of the class you used
1514*0Sstevel@tonic-gate                                0.10
1515*0Sstevel@tonic-gate        upgrade         RW      To which class numbers are upgraded
1516*0Sstevel@tonic-gate                                undef
1517*0Sstevel@tonic-gate        downgrade       RW      To which class numbers are downgraded
1518*0Sstevel@tonic-gate                                undef
1519*0Sstevel@tonic-gate        precision       RW      Global precision
1520*0Sstevel@tonic-gate                                undef
1521*0Sstevel@tonic-gate        accuracy        RW      Global accuracy
1522*0Sstevel@tonic-gate                                undef
1523*0Sstevel@tonic-gate        round_mode      RW      Global round mode
1524*0Sstevel@tonic-gate                                even
1525*0Sstevel@tonic-gate        div_scale       RW      Fallback acccuracy for div
1526*0Sstevel@tonic-gate                                40
1527*0Sstevel@tonic-gate        trap_nan        RW      Trap creation of NaN (undef = no)
1528*0Sstevel@tonic-gate                                undef
1529*0Sstevel@tonic-gate        trap_inf        RW      Trap creation of +inf/-inf (undef = no)
1530*0Sstevel@tonic-gate                                undef
1531*0Sstevel@tonic-gate
1532*0Sstevel@tonic-gateBy passing a reference to a hash you may set the configuration values. This
1533*0Sstevel@tonic-gateworks only for values that a marked with a C<RW> above, anything else is
1534*0Sstevel@tonic-gateread-only.
1535*0Sstevel@tonic-gate
1536*0Sstevel@tonic-gate=head1 BUGS
1537*0Sstevel@tonic-gate
1538*0Sstevel@tonic-gateSome things are not yet implemented, or only implemented half-way:
1539*0Sstevel@tonic-gate
1540*0Sstevel@tonic-gate=over 2
1541*0Sstevel@tonic-gate
1542*0Sstevel@tonic-gate=item inf handling (partial)
1543*0Sstevel@tonic-gate
1544*0Sstevel@tonic-gate=item NaN handling (partial)
1545*0Sstevel@tonic-gate
1546*0Sstevel@tonic-gate=item rounding (not implemented except for bceil/bfloor)
1547*0Sstevel@tonic-gate
1548*0Sstevel@tonic-gate=item $x ** $y where $y is not an integer
1549*0Sstevel@tonic-gate
1550*0Sstevel@tonic-gate=item bmod(), blog(), bmodinv() and bmodpow() (partial)
1551*0Sstevel@tonic-gate
1552*0Sstevel@tonic-gate=back
1553*0Sstevel@tonic-gate
1554*0Sstevel@tonic-gate=head1 LICENSE
1555*0Sstevel@tonic-gate
1556*0Sstevel@tonic-gateThis program is free software; you may redistribute it and/or modify it under
1557*0Sstevel@tonic-gatethe same terms as Perl itself.
1558*0Sstevel@tonic-gate
1559*0Sstevel@tonic-gate=head1 SEE ALSO
1560*0Sstevel@tonic-gate
1561*0Sstevel@tonic-gateL<Math::BigFloat> and L<Math::Big> as well as L<Math::BigInt::BitVect>,
1562*0Sstevel@tonic-gateL<Math::BigInt::Pari> and  L<Math::BigInt::GMP>.
1563*0Sstevel@tonic-gate
1564*0Sstevel@tonic-gateSee L<http://search.cpan.org/search?dist=bignum> for a way to use
1565*0Sstevel@tonic-gateMath::BigRat.
1566*0Sstevel@tonic-gate
1567*0Sstevel@tonic-gateThe package at L<http://search.cpan.org/search?dist=Math%3A%3ABigRat>
1568*0Sstevel@tonic-gatemay contain more documentation and examples as well as testcases.
1569*0Sstevel@tonic-gate
1570*0Sstevel@tonic-gate=head1 AUTHORS
1571*0Sstevel@tonic-gate
1572*0Sstevel@tonic-gate(C) by Tels L<http://bloodgate.com/> 2001, 2002, 2003, 2004.
1573*0Sstevel@tonic-gate
1574*0Sstevel@tonic-gate=cut
1575