You are viewing the version of this documentation from Perl 5.41.5. This is a development version of Perl.

CONTENTS

NAME

Math::BigRat - arbitrary size rational number math package

SYNOPSIS

use Math::BigRat;

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

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

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

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

DESCRIPTION

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

MATH LIBRARY

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

use Math::BigRat try => 'GMP';

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

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

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

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

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

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

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

METHODS

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

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

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

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

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

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

denominator()
$d = $x->denominator();

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

parts()
($n, $d) = $x->parts();

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