NAME

Math::BigFraction - Arbitrary size floating point fractions

SYNOPSIS

  use Math::BigFraction;

  # Number creation	
  $x = Math::BigFraction->new($str);	# from string, like "-0.3333333333"
  $x = Math::BigFraction->new($u,$v);	# from $u / $v, like 1/3, if $v 
					# is missing, +1 is assumed
  $nan  = Math::BigFraction->bnan(); 	# create a NotANumber
  $zero = Math::BigFraction->bzero();	# create a "+0"

  # Testing
  $x->is_zero();		# return whether arg is zero or not
  $x->is_one();			# return true if arg is +1
  $x->is_one('-');		# return true if arg is -1
  #$x->is_odd();		# return true if odd, false for even
  #$x->is_even();		# return true if even, false for odd
  $x->bcmp($y);			# compare numbers (undef,<0,=0,>0)
  $x->bacmp($y);		# compare absolutely (undef,<0,=0,>0)
  $x->sign();			# return the sign, either +,- or NaN

	# rest not ready yet
  # The following all modify their first argument:

  # set 
  $x->bzero();			# set $i to 0
  $x->bnan();			# set $i to NaN

  $x->bneg();			# negation
  $x->babs();			# absolute value
  $x->bnorm();			# normalize (no-op)
  $x->bnot();			# two's complement (bit wise not)
  $x->binc();			# increment x by 1
  $x->bdec();			# decrement x by 1
  
  $x->badd($y);			# addition (add $y to $x)
  $x->bsub($y);			# subtraction (subtract $y from $x)
  $x->bmul($y);			# multiplication (multiply $x by $y)
  $x->bdiv($y);			# divide, set $i to quotient
				# return (quo,rem) or quo if scalar

  $x->bmod($y);			# modulus
  $x->bpow($y);			# power of arguments (a**b)
  $x->blsft($y);		# left shift
  $x->brsft($y);		# right shift 
				# return (quo,rem) or quo if scalar
  
  $x->band($y);			# bit-wise and
  $x->bior($y);			# bit-wise inclusive or
  $x->bxor($y);			# bit-wise exclusive or
  $x->bnot();			# bit-wise not (two's complement)
  
  # The following do not modify their arguments:

  bgcd(@values);		# greatest common divisor
  blcm(@values);		# lowest common multiplicator
  
  $x->bstr($precision);		# return $precision digits after . (default
				# is $Math::BigFloat::precision

DESCRIPTION

All operators (inlcuding basic math operations) are overloaded if you declare your big fractions as

$i = Math::BigFraction->new('123456789','1234567');

Operations with overloaded operators preserve the arguments which is exactly what you expect.

Canonical notation

Big integer values are strings of the form /^[+-]\d+$/ with leading zeros suppressed.

'-0'                            canonical value '-0', normalized '+0'
'   -123 123 123'               canonical value '-123123123'
'1 23 456 7890'                 canonical value '+1234567890'
Input

Input values to these routines may be either Math::BigInt objects or strings of the form /^\s*[+-]?[\d\s]+$/.

Math::BigInt::new() defaults to 0, while Mah::BigInt::new('') results in 'NaN'.

bnorm() on a BigInt object is effectively a no-op, since the numbers are always stored in normalized form. On a string, it creates a BigInt object.

Output

Output values are BigInt objects (normalized), except for bstr(), which returns a string in normalized form. Some routines (is_odd(), is_even(), is_zero(), is_one()) return true or false, while others (bcmp(), bacmp()) return either undef, <0, 0 or >0 and are suited for sort.

Actual math is done in an internal format consisting of two Math::BigInts, named 'a' and 'b'. 'a' and 'b' are always the smallest possible numbers.

EXAMPLES

use Math::BigFraction;
$x = new Math::BigFraction 1,3;	# 1/3
$x *= 3;				# 1/1
$x /= 5;				# 1/5
$x += new Math::BigFraction 3,-7;	# -22/35
$x->bneg();				# 22/35
$x++;					# 47/35

Autocreating constants

After use Math::BigInt ':constant' all the integer decimal constants in the given scope are converted to Math::BigInt. This conversion happens at compile time.

In particular

perl -MMath::BigInt=:constant -e 'print 2**100,"\n"'

prints the integer value of 2**100. Note that without conversion of constants the expression 2**100 will be calculated as floating point number.

SPEED

Greatly enhanced ;o) SectionNotReadyYet.

PERFORMANCE

SectionNotReadyYet.

BUGS

None known yet.

PITFALLS

bdiv

The following will probably not do what you expect:

print $c->bdiv(10000),"\n";

It prints both quotient and reminder since print works in list context. Also, bdiv() will modify $c, so be carefull. You probably want to use

print $c / 10000,"\n";
print scalar $c->bdiv(10000),"\n";  # or if you want to modify $c

instead.

bpow

bpow() now modifies the first argument, unlike the old code which left it alone and only returned the result. This is to be consistent with badd() etc. The first will modify $x, the second one won't:

print bpow($x,$i),"\n"; # modify $x
print $x ** $i,"\n";	# leave $x alone 

AUTHORS

(c) Copyright by Tels http://bloodgate.com in late 2000, 2001.