Quick Navigator

 Search Site Miscellaneous Server Agreement Year 2038 Credits

# Manual Reference Pages  -  MATH::CEPHES::FRACTION (3)

.ds Aq ’

### NAME

```

Math::Cephes::Fraction - Perl interface to the cephes math fraction routines

```

### SYNOPSIS

```

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

```

### DESCRIPTION

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);

```

### BUGS

Please report any to Randy Kobes <randy@theoryx5.uwinnipeg.ca>

For the basic interface to the cephes fraction routines, see Math::Cephes. See also Math::Fraction for a more extensive interface to fraction routines.