[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 = 
	Rational->new(
		      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};
}

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

=head1 NAME

Rational - Perl extension for manipulating rational numbers

=head1 SYNOPSIS

  use Rational;

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

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

  $rs->print;

=head1 DESCRIPTION

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

perl(1).

=cut
--------------------------------------------------------------------------
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