Math::...action(3pm)

✨ NAME ✨

Math::Cephes::Fraction β€” the Perl interface to the cephes math fraction routines πŸ’– (yes, that's the whole name, and yes it's iconic)

This page shows up in your terminal as Math::...action(3pm) from the User Contributed Perl Documentation, bestie. That's just the header doing its thing. πŸŽ€


πŸ¦‹ 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

okay so literally three lines in and you already have fractions that add themselves. main character behavior fr fr. ✨


πŸ’… DESCRIPTION πŸ’…

So this module is a lil layer sitting on top of the basic routines in the cephes math library, and its whole job is handling fractions. A fraction object gets created via any of these 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

and that last one is available by importing :fract. And if you pass no arguments at all, like this:

my $h = fract();

then the defaults $z = 0/1 are assumed. no thoughts, just zero over one. πŸ“

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;
🫢 ngl, that last line is written exactly how the original has it β€” including the way it is. We keep code character-for-character around here, no cap.

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. That 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. she's giving math professor energy and honestly? we're obsessed. πŸ‘‘

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

add, subtract, multiply, divide, print, reduce. five little baddies carrying the whole module on their backs. πŸ”₯


πŸ’€ BUGS πŸ’€

Found something cursed? Please report any to Randy Kobes <randy@theoryx5.uwinnipeg.ca>. do it kindly, okay bestie 😭


🌸 SEE ALSO 🌸

For the basic interface to the cephes fraction routines, see Math::Cephes. See also Math::Fraction if you want a more extensive interface to fraction routines β€” iykyk, sometimes you need the deluxe version. πŸŽ€


The C code for the Cephes Math Library is Copyright 1984, 1987, 1989, 2002 by Stephen L. Moshier, and is available at http://www.netlib.org/cephes/. Direct inquiries to 30 Frost Street, Cambridge, MA 02140.

The perl interface is copyright 2000, 2002 by Randy Kobes. This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


πŸ’– that's the whole page, bestie β€” this one's perl v5.42.3, dated 2026-08-19, straight from the User Contributed Perl Documentation to your terminal. go forth and reduce some fractions. slay. πŸ’…βœ¨
πŸ“¦ where this page comes from