Math::Cephes::Fraction(3pm)

Math::Cephes::Fraction3pm

Usage

  use Math::Cephes::Fraction qw(fract);
  my $f1 = fract(2,3);          # $f1 = 2/3
  my $f2 = fract(3,4);          # $f2 = 3/4
  my $f3 = $f1->radd($f2);      # $f3 = $f1 + $f2

Overview

This module is a layer on top of the basic routines in the cephes math library to handle fractions. A fraction object is created via any of the following syntaxes:

  my $f = Math::Cephes::Fraction->new(3, 2);  # $f = 3/2
  my $g = new Math::Cephes::Fraction(5, 3);   # $g = 5/3
  my $h = fract(7, 5);                        # $h = 7/5

the last one being available by importing :fract. If no arguments are specified, as in

  my $h = fract();

then the defaults $z = 0/1 are assumed. The numerator and denominator of a fraction are represented respectively by

   $f->{n}; $f->{d}

or, as methods,

  $f->n;  $f->d;

and can be set according to

  $f->{n} = 4; $f->{d} = 9;

or, again, as methods,

 $f->n(4)  ; $f->(d) = 9;

The fraction can be printed out as

  print $f->as_string;

or as a mixed fraction as

  print $f->as_mixed_string;

These routines reduce the fraction to its basic form before printing. This uses the euclid routine which finds the greatest common divisor of two numbers, as follows:

 ($gcd, $m_reduced, $n_reduced) = euclid($m, $n);

which returns the greatest common divisor of $m and $n, as well as the result of reducing $m and $n by $gcd

A summary of the basic routines is as follows.

 $x = fract(3, 4);       #  x = 3 / 4
 $y = fract(2, 3);       #  y = 2 / 3
 $z = $x->radd( $y );    #  z = x + y
 $z = $x->rsub( $y );    #  z = x - y
 $z = $x->rmul( $y );    #  z = x * y
 $z = $x->rdiv( $y );    #  z = x / y
 print $z->{n}, ' ', $z->{d};  # prints numerator and denominator of $z
 print $z->as_string;         # prints the fraction $z
 print $z->as_mixed_string;   # converts $z to a mixed fraction, then prints it
 $m = 60;
 $n = 144;
 ($gcd, $m_reduced, $n_reduced) = euclid($m, $n);

Releases

3 releases since Debian 11 (Bullseye); the text changed 2 times.

  1. 11
  2. 12
  3. 13

Bugs

Please report any to Randy Kobes <[email protected]>

From libmath-cephes-perl 0.5307-1 in Debian 13 (Trixie). Parsed by carta from the page's man source.