package Math::BigRat;
-require 5.005_03;
+# anythig older is untested, and unlikely to work
+use 5.006002;
use strict;
-require Exporter;
use Math::BigFloat;
use vars qw($VERSION @ISA $upgrade $downgrade
$accuracy $precision $round_mode $div_scale $_trap_nan $_trap_inf);
-@ISA = qw(Exporter Math::BigFloat);
+@ISA = qw(Math::BigFloat);
-$VERSION = '0.14';
+$VERSION = '0.19';
use overload; # inherit overload from Math::BigFloat
my $nan = 'NaN';
my $class = 'Math::BigRat';
-my $IMPORT = 0;
sub isa
{
my $nf = Math::BigFloat->new($n,undef,undef);
$self->{sign} = '+';
return $self->bnan() if $nf->is_nan();
+
$self->{_n} = $MBI->_copy( $nf->{_m} ); # get mantissa
# now correct $self->{_n} due to $n
$self->{_d} = $MBI->_copy( $f->{_m} );
# calculate the difference between nE and dE
- # XXX TODO: check that exponent() makes a copy to avoid copy()
- my $diff_e = $nf->exponent()->copy()->bsub( $f->exponent);
+ my $diff_e = $nf->exponent()->bsub( $f->exponent);
if ($diff_e->is_negative())
{
# < 0: mul d with it
$self->{sign} = '+'; # no sign => '+'
$self->{_n} = undef;
$self->{_d} = undef;
- if ($n =~ /^([+-]?)0*(\d+)\z/) # first part ok?
+ if ($n =~ /^([+-]?)0*([0-9]+)\z/) # first part ok?
{
$self->{sign} = $1 || '+'; # no sign => '+'
$self->{_n} = $MBI->_new($2 || 0);
}
- if ($d =~ /^([+-]?)0*(\d+)\z/) # second part ok?
+ if ($d =~ /^([+-]?)0*([0-9]+)\z/) # second part ok?
{
$self->{sign} =~ tr/+-/-+/ if ($1 || '') eq '-'; # negate if second part neg.
$self->{_d} = $MBI->_new($2 || 0);
{
$d = Math::BigInt->new($d,undef,undef) unless ref $d;
$n = Math::BigInt->new($n,undef,undef) unless ref $n;
-
+
if ($n->{sign} =~ /^[+-]$/ && $d->{sign} =~ /^[+-]$/)
{
# both parts are ok as integers (wierd things like ' 1e0'
else
{
# for simple forms, use $MBI directly
- if ($n =~ /^([+-]?)0*(\d+)\z/)
+ if ($n =~ /^([+-]?)0*([0-9]+)\z/)
{
$self->{sign} = $1 || '+';
$self->{_n} = $MBI->_new($2 || 0);
my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_);
# Both parts must be objects of whatever we are using today.
- # Second check because Calc.pm has ARRAY res as unblessed objects.
- if (ref($x->{_n}) ne $MBI && ref($x->{_n}) ne 'ARRAY')
+ if ( my $c = $MBI->_check($x->{_n}) )
{
- require Carp; Carp::croak ("n is not $MBI but (".ref($x->{_n}).') in bnorm()');
+ require Carp; Carp::croak ("n did not pass the self-check ($c) in bnorm()");
}
- if (ref($x->{_d}) ne $MBI && ref($x->{_d}) ne 'ARRAY')
+ if ( my $c = $MBI->_check($x->{_d}) )
{
- require Carp; Carp::croak ("d is not $MBI but (".ref($x->{_d}).') in bnorm()');
+ require Carp; Carp::croak ("d did not pass the self-check ($c) in bnorm()");
}
# no normalize for NaN, inf etc.
{
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->{_d} = $MBI->_zero() unless defined $self->{_d};
+ $self->{_n} = $MBI->_zero() unless defined $self->{_n};
Carp::croak ("Tried to set $self to NaN in $class\::_bnan()");
}
$self->{_n} = $MBI->_zero();
{
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->{_d} = $MBI->_zero() unless defined $self->{_d};
+ $self->{_n} = $MBI->_zero() unless defined $self->{_n};
Carp::croak ("Tried to set $self to inf in $class\::_binf()");
}
$self->{_n} = $MBI->_zero();
# 4 3 4*3 12
# we do not compute the gcd() here, but simple do:
- # 5 7 5*3 + 7*4 41
+ # 5 7 5*3 + 7*4 43
# - + - = --------- = --
# 4 3 4*3 12
# and bnorm() will then take care of the rest
+ # 5 * 3
$x->{_n} = $MBI->_mul( $x->{_n}, $y->{_d});
+ # 7 * 4
my $m = $MBI->_mul( $MBI->_copy( $y->{_n} ), $x->{_d} );
+ # 5 * 3 + 7 * 4
($x->{_n}, $x->{sign}) = _e_add( $x->{_n}, $m, $x->{sign}, $y->{sign});
+ # 4 * 3
$x->{_d} = $MBI->_mul( $x->{_d}, $y->{_d});
- # normalize and round
+ # normalize result, and possible round
$x->bnorm()->round(@r);
}
$s . $MBI->_as_hex($x->{_n});
}
+sub as_oct
+ {
+ my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_);
+
+ return $x unless $x->is_int();
+
+ my $s = $x->{sign}; $s = '' if $s eq '+';
+ $s . $MBI->_as_oct($x->{_n});
+ }
+
+##############################################################################
+
+sub from_hex
+ {
+ my $class = shift;
+
+ $class->new(@_);
+ }
+
+sub from_bin
+ {
+ my $class = shift;
+
+ $class->new(@_);
+ }
+
+sub from_oct
+ {
+ my $class = shift;
+
+ my @parts;
+ for my $c (@_)
+ {
+ push @parts, Math::BigInt->from_oct($c);
+ }
+ $class->new ( @parts );
+ }
+
##############################################################################
# import
my $self = shift;
my $l = scalar @_;
my $lib = ''; my @a;
- $IMPORT++;
+ my $try = 'try';
for ( my $i = 0; $i < $l ; $i++)
{
$downgrade = $_[$i+1]; # or undef to disable
$i++;
}
- elsif ($_[$i] eq 'lib')
+ elsif ($_[$i] =~ /^(lib|try|only)\z/)
{
$lib = $_[$i+1] || ''; # default Calc
+ $try = $1; # lib, try or only
$i++;
}
elsif ($_[$i] eq 'with')
{
- $MBI = $_[$i+1] || 'Math::BigInt::Calc'; # default Math::BigInt::Calc
+ # this argument is no longer used
+ #$MBI = $_[$i+1] || 'Math::BigInt::Calc'; # default Math::BigInt::Calc
$i++;
}
else
{
$_ =~ tr/a-zA-Z0-9://cd; # limit to sane characters
}
- # MBI already loaded, so feed it our lib arguments
- $MBI->import('lib' => $lib . join(",",@c), 'objectify');
+ $lib = join(",", @c);
}
+ my @import = ('objectify');
+ push @import, $try => $lib if $lib ne '';
+
+ # MBI already loaded, so feed it our lib arguments
+ Math::BigInt->import( @import );
$MBI = Math::BigFloat->config()->{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
- # even if @_ is empty, to give it a chance
+ # any non :constant stuff is handled by our parent, Exporter (loaded
+ # by Math::BigFloat, even if @_ is empty, to give it a chance
$self->SUPER::import(@a); # for subclasses
$self->export_to_level(1,$self,@a); # need this, too
}
=head2 MATH LIBRARY
-Math with the numbers is done (by default) by a module called
-Math::BigInt::Calc. This is equivalent to saying:
+You can change the underlying module that does the low-level
+math operations by using:
- use Math::BigRat lib => 'Calc';
+ use Math::BigRat try => 'GMP';
-You can change this by using:
-
- use Math::BigRat lib => 'BitVect';
+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 lib => 'Foo,Math::BigInt::Bar';
+ use Math::BigRat try => 'Foo,Math::BigInt::Bar';
-Calc.pm uses as internal format an array of elements of some decimal base
-(usually 1e7, but this might be different for some systems) with the least
-significant digit first, while BitVect.pm uses a bit vector of base 2, most
-significant bit first. Other modules might use even different means of
-representing the numbers. See the respective module documentation for further
-details.
+If you want to get warned when the fallback occurs, replace "try" with
+"lib":
-Currently the following replacement libraries exist, search for them at CPAN:
+ use Math::BigRat lib => 'Foo,Math::BigInt::Bar';
- Math::BigInt::BitVect
- Math::BigInt::GMP
- Math::BigInt::Pari
- Math::BigInt::FastCalc
+If you want the code to die instead, replace "try" with
+"only":
+
+ use Math::BigRat only => 'Foo,Math::BigInt::Bar';
=head1 METHODS
-Any methods not listed here are dervied from Math::BigFloat (or
+Any methods not listed here are derived from Math::BigFloat (or
Math::BigInt), so make sure you check these two modules for further
information.
Return a list consisting of (signed) numerator and (unsigned) denominator as
BigInts.
-=head2 as_int()
+=head2 numify()
+
+ my $y = $x->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 as_int() instead.
+
+This routine is automatically used whenever a scalar is required:
+
+ my $x = Math::BigRat->new('3/1');
+ @array = (1,2,3);
+ $y = $array[$x]; # set $y to 3
+
+=head2 as_int()/as_number()
$x = Math::BigRat->new('13/7');
print $x->as_int(),"\n"; # '1'
Returns the BigRat as binary string. Works only for integers.
+=head2 as_oct()
+
+ $x = Math::BigRat->new('13');
+ print $x->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->from_hex('0x10');
+ my $b = Math::BigRat->from_bin('0b10000000');
+ my $o = Math::BigRat->from_oct('020');
+
+Create a BigRat from an hexadecimal, binary or octal number
+in string form.
+
+=head2 length()
+
+ $len = $x->length();
+
+Return the length of $x in digitis for integer values.
+
+=head2 digit()
+
+ print Math::BigRat->new('123/1')->digit(1); # 1
+ print Math::BigRat->new('123/1')->digit(-1); # 3
+
+Return the N'ths digit from X when X is an integer value.
+
+=head2 bnorm()
+
+ $x->bnorm();
+
+Reduce the number to the shortest form. This routine is called
+automatically whenever it is needed.
+
=head2 bfac()
$x->bfac();
Set $x to the remainder of the division of $x by $y.
+=head2 bneg()
+
+ $x->bneg();
+
+Used to negate the object in-place.
+
=head2 is_one()
print "$x is 1\n" if $x->is_one();
Return true if $x is exactly zero, otherwise false.
-=head2 is_pos()
+=head2 is_pos()/is_positive()
print "$x is >= 0\n" if $x->is_positive();
C<is_positive()> is an alias for C<is_pos()>.
-=head2 is_neg()
+=head2 is_neg()/is_negative()
print "$x is < 0\n" if $x->is_negative();
Calculate the square root of $x.
-=head2 config
+=head2 broot()
+
+ $x->broot($n);
+
+Calculate the N'th root of $x.
+
+=head2 badd()/bmul()/bsub()/bdiv()/bdec()/binc()
+
+Please see the documentation in L<Math::BigInt>.
+
+=head2 copy()
+
+ my $z = $x->copy();
+
+Makes a deep copy of the object.
+
+Please see the documentation in L<Math::BigInt> for further details.
+
+=head2 bstr()/bsstr()
+
+ my $x = Math::BigInt->new('8/4');
+ print $x->bstr(),"\n"; # prints 1/2
+ print $x->bsstr(),"\n"; # prints 1/2
+
+Return a string representating this object.
+
+=head2 bacmp()/bcmp()
+
+Used to compare numbers.
+
+Please see the documentation in L<Math::BigInt> for further details.
+
+=head2 blsft()/brsft()
+
+Used to shift numbers left/right.
+
+Please see the documentation in L<Math::BigInt> for further details.
+
+=head2 bpow()
+
+ $x->bpow($y);
+
+Compute $x ** $y.
+
+Please see the documentation in L<Math::BigInt> for further details.
+
+=head2 config()
use Data::Dumper;
undef
round_mode RW Global round mode
even
- div_scale RW Fallback acccuracy for div
+ div_scale RW Fallback accuracy for div
40
trap_nan RW Trap creation of NaN (undef = no)
undef
=head1 AUTHORS
-(C) by Tels L<http://bloodgate.com/> 2001 - 2005.
+(C) by Tels L<http://bloodgate.com/> 2001 - 2007.
=cut