[mercury-users] Perl implementation of Rational numbers (source code)

Terrence Brannon princepawn at earthlink.net
Thu May 3 22:16:19 AEST 2001

package Rational;

use strict;

our $VERSION = '0.01';

sub new {
    my $class  = shift;
    my %config = @_;
    my $self = bless \%config, $class;
sub add {
    my ($r1, $r2) = @_;

    my $rational = 
		      numerator => 
		         $r1->{numerator} * $r2->{denominator}
		       + $r2->{numerator} * $r1->{denominator},
		      denominator => 
		         $r1->{denominator} * $r2->{denominator}

sub print {
  my $self = shift;

  printf "%d/%d", $self->{numerator}, $self->{denominator};

# Below is stub documentation for your module. You better edit it!

=head1 NAME

Rational - Perl extension for manipulating rational numbers


  use Rational;

  my $r1 = Rational->new(numerator=>1, denominator=>3);
  my $r2 = Rational->new(numerator=>2, denominator=>5);

  my $rs = $r1->add($r2);



This is a Perl extension allowing you to manipulate rational numbers via
an object-oriented Perl API.

=head2 EXPORT

None by default.

=head1 AUTHOR

T. M. Brannon - tbone at cpan.org

princepawn at http://www.perlmonks.org

=head1 SEE ALSO


mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe

More information about the users mailing list