Why Rationalise or Canonise Fractions before Addition | Maths Explanation for Perl Kids
In this Perl tutorial for junior secondary students, we explore how to add fractions. Before performing the addition,
we rationalise or canonise the fractions to ensure accuracy. This method uses Finding LCM in Perl
class to align denominators, making it ideal for math programming beginners.
This Perl tutorial teaches young students how to add fractions with different denominators.
Before fractions are added, they are rationalised; i.e., they are put in a form where their denominators become
the same. This identical denominator is the LCM of the previous denominators of all the separate fractions.
After this is done, the new numerators can then be added together.
Step-by-Step Guide for Addition of Fractions - Perl Algorithm
The following steps will guide us in writing our Perl code for adding fractions.
Let's illustrate the steps to follow with the example fractional expression
2/5 + 7/4
Step 1:
Using the Find LCM in Perl
class from the Primary Category, find the LCM of the denominators.
⇒ LCM of 5 & 4 = 20
Step 2:
In a turn by turn fashion, divide the found LCM from Step 1
by each denominator, multiplying the quotient by the corresponding numerator.
⇒
((2 x 4) + (7 x 5))/20
= (8 + 35)/20
Step 3:
Go ahead and add the numerators.
⇒
43/20
Create a new Perl module file;
Call it AddFraction.pm.;
Type out the adjoining Perl code for adding fractions.
Note: The code module for
Learn how to find LCM in Perl
is from the Primary Category.
Just make a copy of it into the current folder (project).
You can comment out the DivideFraction Perl object
code in the main class from the previous lesson or simply continue from where it stopped.
So! Perl Fun Practice Exercise - Add 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 adds these fractions.
Perl Code for Adding Fractions - Module File
BEGIN {
require Exporter;
# for the sake of standard
our $VERSION = 2016.12;
# Inherit from exporter to export functions and variables
our @ISA = qw(Exporter);
# Functions and variables to be exported by default
our @EXPORT_OK = qw(doAdd canoniseFraction);
}
use warnings;
use strict;
my $answer;
my (@numerators, @denominators, @new_numerators);
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}};
$answer = 0;
}
# transforms fractions so they all have same denominator
# takes two references --
# to your numerators and denominators in that order
#
# returns an array --
# reference to new numerators and
# new denominator(LCM) in that order
sub canoniseFraction {
my @new_nums;
# STEP 1:
# find their LCM
use LCM;
my $lcm = LCM->new(\@denominators);
$lcm = $lcm->getLCM();
# STEP 2:
# transform fractions so they all have same denominator
for (0 .. $#denominators) {
push @new_nums, ($lcm / $denominators[$_] * $numerators[$_]);
}
return (\@new_nums, $lcm);
}
# returns an array --
# new numerator and new denominator(LCM) in that order
sub doAdd {
my @help = canoniseFraction();
@new_numerators = @{$help[0]};
# STEP 3:
# add all transformed numerators
$answer += $new_numerators[$_] for 0 .. $#new_numerators;
return ($answer, $help[1]);
}
1;
Perl Code for Adding Fractions - Main Class
use strict;
use warnings;
use ADDFRACTION "doAdd";
# Useful variables
my (@numerators, @denominators, @solutions);
my %fractions;
##
# Adding fractions
##
@numerators = (1, 1, 1, 1);
@denominators = (4, 4, 4, 4);
%fractions = (
numerators => \@numerators,
denominators => \@denominators
);
print "\n Solving:\n";
# Print as fraction
printf("%13u", $_) for @numerators;
printf("\n%12s", " ");
print "- + " for 1 .. $#numerators;
printf("%2s\n", "-");
printf("%13u", $_) for @denominators;
print "\n";
# use the AddFraction class
my $add_fract = ADDFRACTION->new(\%fractions);
@solutions = $add_fract->doAdd();
printf("\n%25u\n", $solutions[0]);
printf("%25s\n", "Answer = -");
printf("%25u\n", $solutions[1]);
print "\n\n";