usingMaths.com
From Theory to Practice - Math You Can Use.







<< Previous Next >>

How to Divide Fractions in Perl | Step-by-Step Tutorial with Code Example



Understanding the Math Behind Fraction Division | Maths Explanation for Perl Kids

Perl makes it easy to perform arithmetic operations like dividing fractions. Whether you're simplifying fractions, multiplying them, or coding a math class project, Perl provides clear syntax and powerful tools for beginners and students alike.

Dividing fractions in Perl is a great way to combine coding with math skills. In this tutorial, junior secondary students will learn how to use Perl to divide fractions step-by-step. We'll explore how to invert and multiply fractions, write a Perl class for fraction operations, and understand the logic behind the algorithm.

Division is the inverse operation of multiplication; That is exactly what we'll do with for fractions.
Invert the fractions that come after a division sign, as well as change the division sign to multiplication, and then follow through with multiplication, as already explained in the Multiplying Fractions with Perl tutorial.


Algorithm Steps to Divide Fractions in Perl

Say we are to implement a Perl algorithm to divide the given fractional expression
                  21/8 ÷ 7/2;

Inverting this will yield
                  21/8 ÷ 7/2;

Then we can go ahead and multiply.


Create a new Perl module file; Call it DivideFraction.pm.
Type out the adjoining Perl code for dividing fractions.


Note: You can comment out the MultiplyFraction Perl object code in the main class from the previous lesson or simply continue from where it stopped.



So! Perl Fun Practice Exercise - Divide Fractions

As a fun practice exercise, feel free to try out your own fractions with different numerators and denominators, and see how the Perl code divides those fractions.







Perl Code for Dividing Fractions - Module File

package DIVIDEFRACTION;

BEGIN {
    require Exporter;

    # for the sake of standard
    our $VERSION = 2016.12;

    # Functions and variables to be exported by default
    our @EXPORT_OK = qw(doDivide);
}

use warnings;
use strict;
use parent "MULTIPLYFRACTION"

my (@numerators@denominators);
my %fractions;

# simulate an object construct
# takes one argument  -- besides its package name;
# hash reference to array references for numerators and denominators
sub new {
    no warnings "all";
    
    my $this = shift;
    my $parameters = shift#this is already a hash reference
    bless $parameters$this;
    $this->_init($parameters);
    return $this;
}

# Simulate a constructor
sub _init {
    my $self = shift;
    my $aux = shift;
    
    $fractions{numerators}        = $aux->{numerators};
    $fractions{denominators}    = $aux->{denominators};
    @numerators        = @{$fractions{numerators}};
    @denominators    = @{$fractions{denominators}};
}

# Returns a scalar of the new numerator
sub doDivide {
    my $temp;
    # Invert every other fraction but the first
    for (1 .. $#numerators) {
        $temp = $numerators[$_];
        $numerators[$_] = $denominators[$_];
        $denominators[$_] = $temp;
    }
    %fractions = (
        numerators     => \@numerators,
        denominators => \@denominators
    );
    my $call = MULTIPLYFRACTION->new(\%fractions);
    return $call->doMultiply();
}


1;


Perl Code for Dividing Fractions - Main Class

#!/usr/bin/perl;
use strict;
use warnings;
use DIVIDEFRACTION;

# Useful variables
my (@numerators@denominators);
my (%fraction%fractions);

##
 # Dividing fractions
 ##
@numerators      = (1696407);
@denominators = (9202720);
%fractions = (
    numerators     => \@numerators,
    denominators => \@denominators
);

print "\n    Solving:\n";
# Print as fraction
printf("%13u", $_) for @numerators;
printf("\n%11s"" ");
print "-      /     " for 1 .. $#numerators;
printf("%2s\n""-");
printf("%13u", $_) for @denominators;
print "\n";

# use the DivideFraction class
my $div_fract = DIVIDEFRACTION->new(\%fractions);
%fraction = %{$div_fract->doDivide()};

printf("\n%25u\n"$fraction{numerator});
printf("%25s\n""Answer =         -");
printf("%25u\n"$fraction{denominator});


print "\n\n";



<< Previous Next >>