fraction(3)

NAME

Math::Fraction - To Manipulate Exact Fractions (v.53b,
Beta Release)

SYNOPSIS

use Math::Fraction;
$a = frac(1,2); $b = frac(6,7);
print "$a + $b = ", $a + $b, "$a * $b = ", $a * $b;
print $a->num;

DESCRIPTION

This program is meant to replace the old bigrat perl
library. It can do everything it can do and a lot more.

Some of its features include:

· Being able to add, subtract, multiply, and divide,
among other things just like you would normal numbers
that's to the overload module.
· Being able to convert a decimal, including repeating
ones, into a fraction. For example, 1.142857142857
would become 8/7.
· Being able to control how the fraction is displayed.
For example 8/7 verses 1 1/7
· Being able to use arbitrary size numbers in the numer
ator and the denominator.
· Being able to covert between SMALL (using normal
floats/integers) and BIG (using arbitrary size
floats/integers) as needed so you do not have to worry
about it. (New as of ver .4a)
· Being able to have multiple default sets so that a
function can modify the defaults with out effecting
other functions (New as of ver .4a)
Usage

frac(FRACTION, TAGS) || Math::Fraction->new(FRACTION,
TAGS)
ex $f1=frac(2,3); $f2=frac(7,3,MIXED); $f3=frac("-10
3/4");
FRACTION can equal any of the following:
(Numerator, Denominator)
(Number, Numerator, Denominator)
Decimal
"Numerator/Denominator"
"Number Numerator/Denominator"
Any of these numbers can be any real number however if you
enter in negative numbers in anything but the First Number
for the Mixed (3 numbers) the negative will be ignored.
TAGS can equal 0, one or more of the following
NORMAL|MIXED|MIXED_RAW|RAW|DEF_MIXED
Controls How the fraction is displayed:
NORMAL display it in the #/# form
MIXED_RAW
display it in the # #/# form
MIXED the same as MIXED_RAW but if one part is equal
to 0 it will leave it off
RAW the same as NORAML but always includes the
sign
DEF_MIXED
will let it be what ever the default value is
at the time
REDUCE|NO_REDUCE|DEF_REDUCE
Controls the automatic reduction of fraction after
operations are performed on it.
AUTO|NO_AUTO|DEF_AUTO
Set rether to automatically convert between BIG and
SMALL as needed see "Notes on the AUTO tag".
SMALL|BIG|DEF_BIG
When the AUTO tag is NOT set it will set whether to
use Arbitrary-Length numbers using the Math::BigInt
and Math::BigFloat package. (Not the ** operator will
not work however due to limitations of the packages.)
When the AUTO tag is set these tags will have NO
effect. (Note the default tags are NORMAL REDUCE AUTO
and SMALL)
Methods
reduce
returns a reduced fraction but leaves the original
object untouched.
string(NORMAL|MIXED|MIXED_RAW|RAW)
returns the fraction as a string. if no parameters
are given the objects default display method will be
used.
decimal|num
returned the decimal value of the fraction
list|list(MIXED)
returns a list containing the fraction if MIXED is
used than a 3 item list is returned.
is_tag
returns 1 is that tag exists in the fraction undef if
is set to the default -1 otherwise
is_tag(INC_DEF)
returns 1 is that tag exists in the fraction -1 if is
tag does not exist but the default is set to that, 0
otherwise.
tags
returns a list of the objects tags
tags(INC_DEF)
returns a list of the objects tags if a particular tag
is set to read a default the default tag is returned
instead.
All of the above methods may all be exported so that they
can be used as functions with a fraction as their first
parameter. The string, decimal|num functions can be
imported with the tag STR_NUM instead of having to list
each one.
modify
modifies the object. Works almost the same as the new
method but it doesn't return anything and preserves
the objects tags unless overridden by new entries
modify_reduce
same as reduce but it modifies the object instead of
returning a fraction.
modify_num(Numerator)
modifies the fraction's numerator.
modify_den(Denominator)
modified the fraction's denominator.
modify_tag(TAGS)
modifies the fraction tags.
The is_tag, tags, and modify_tags methods can be used on
the class its self to get at or modify the default tags.
The following methods will always modify or read the Class
defaults
digits
returns the default number of digests to return when
doing floating point operations with BIG numbers, if
set to undef Math::BigFloat will decide.

modify_digits(NUM)
Dafault Sets
Default sets are way of modifying the defaults with out
effecting other functions. Functions that relay on the
default values or modify the default should start with a
"$set_id = temp_set" and end with a "temp_set($set_id)".
See "EXAMPLES" for examples of how default sets work.
The following methods are met to manage default sets and
will always work on the Class defaults.
sets
returns a list of all the sets;
name_set
return the name of the current set.
name_set(NAME)
name the current set
save_set
saves the current sent based on its name as given
above
save_set(NAME)
save the current set as NAME
save_set(RAND)
save the current set using a unique name
load_set(NAME)
loads a set.
copy_set(NAME_ORG, NAME_NEW)
copies a set. Returns true if successful.
del_set(NAME)
deletes a set.
exists_set(NAME)
returns true if the set exists.
use_set(NAME)
uses a set, that is any changes you make to the used
set will also change the original set, like a link.
temp_set
loads a temp set using the default default values and
returns a unique id you need to keep.
temp_set(ID)
restores the original set based on the id you should
of kept.
Unless otherwise specified all the set methods will return
the name of the set being worked on if it was successful,
false otherwise
tags(SET)
lists all the tags in SET.
is_tag(TAG, SET)
returns true if TAG exists in SET
digits(SET)
returns what digits is set to in SET;
Overloaded Operators
The following operations have been overridden and will
return a fraction:

+ - / * + += -= *= /= ++ -- abs
The following operations have also been overridden:

<=> == != < <= > >=
The following operations have also been overridden however
they may spit out nasty fractions.

** sqrt
Whenever you try to access a fraction as a string the
string method will be called and when try to access it as
a number the decimal method will be called.
This means that almost all other operations will work how
ever some might return decimals like the sin and cos;
Notes on the AUTO tag
With the AUTO tag set Fractions will be converted between
SMALL and BIG as needed. The BIG and SMALL tag will be
*ignorded* unless you explicitly specify NO_AUTO in auto to control how the fraction is stored.
When you give it a number it will decide if it is small
enough to be stored as a SMALL or if the fraction needs to
converted to BIG. However, in order for it to recognize a
big fraction the number needs to be in quotes, thus
"frac(7823495784957895478,781344567825678454)" will still
be stored as a small with some of the digits lost.
When calculating to SMALL numbers that results in a number
that is to big for SMALL the calculation is done AGAIN but
this time with BIG numbers (so that it will calculate all
the digits correctly) and the new fraction will become a
BIG.
When calculating to BIG numbers that results in a number
that is small enough to be a SMALL the new fraction will
become a SMALL.
Normally, the AUTO tag will save time as calculating with
BIG numbers can be quite time consuming however it might
slow thinks down if it constantly converts between the two
thus in some cases it may be wise to turn it off.

EXAMPLES

This is a small demonstration of what the fraction module
can do.

It is run for the most part with these two functions.
sub pevel {print ">$_[0]0; $ans = eval $_[0]; print "
$ans0; }
sub evelp {print ">$_[0]0; eval $_[0]; }
You can see it for yourself my typing in "perl -e "use
Math::FractionDemo; frac_calc;"" then frac_demo.

>frac(1, 3)
1/3
>frac(4, 3, MIXED)
1 1/3
>frac(1, 1, 3)
4/3
>frac(1, 1, 3, MIXED)
1 1/3
>frac(10)
10/1
>frac(10, MIXED)
10
>frac(.66667)
2/3
>frac(1.33333, MIXED)
1 1/3
>frac("5/6")
5/6
>frac("1 2/3")
5/3
>frac(10, 20, NO_REDUCE)
10/20
>$f1=frac(2,3); $f2=frac(4,5);
>$f1 + $f2
22/15
>$f1 * $f2
8/15
>$f1 + 1.6667
7/3
>$f2->modify_tag(MIXED)
>$f2 + 10
10 4/5
>frac($ans, NORMAL) # trick to create new fraction with
different tags
54/5
>$f1 + $f2 # Add two unlikes it goes to default
mode
22/15
>$f1**1.2
229739670999407/373719281884655
>$f1->num**1.2
0.614738607654485
>frac(1,2)+frac(2,5)
9/10
>$f1=frac(5,3,NORMAL); $f2=frac(7,5);
>"$f1 $f2"
5/3 7/5
>Math::Fraction->modify_tag(MIXED)
>"$f1 $f2"
5/3 1 2/5
>$f1 = frac("3267893629762/32678632179820")
3267893629762/32678632179820
>$f2 = frac("5326875886785/76893467996910")
5326875886785/76893467996910
>$f1->is_tag(BIG).",".$f2->is_tag(BIG) # Notice how nei
ther is BIG
0,0
>$f1+$f2
21267734600460495169085706/125638667885089122116217810
>$ans->is_tag(BIG) # But this answer
is.
1
>$f1*$f2
1740766377695750621849517/251277335770178244232435620
>$ans->is_tag(BIG) # And so is this
one.
1
>$f1 = frac("3267893629762/32678632179820", BIG)
3267893629762/32678632179820
>$f1->is_tag(BIG) # Notice how the big tag had no ef
fect.
>$f1->modify_tag(NO_AUTO, BIG)
>$f1->is_tag(BIG) # But now it does. You have to turn
off AUTO.
1
>$f1->num
.10000093063197482237806917498797382196606
>Math::Fraction->modify_digits(15)
>$f1->num
.1000009306319748
>$f1 =
frac("0.1231231234564564564564564564561234567891234567891234")
13680347037037036999999999999963000037/
111111111000000000000000000000000000000
>Math::Fraction->modify_digits(65)
>$f1->num
.123123123456456456456456456456123456789123456789123456789123456789
>$f1 = frac(7,5);
>$f2 = frac("3267893629762/32678632179820", NO_AUTO, BIG)
>Math::Fraction->modify_tag(MIXED); Math::Fraction->modi
fy_digits(60)
>"$f1 ".$f2->num
1 2/5
.1000009306319748223780691749879738219660647769485035912494771
>Math::Fraction->load_set(DEFAULT)
>"$f1 ".$f2->num
7/5 .10000093063197482237806917498797382196606
>Math::Fraction->modify_digits(25)
>"$f1 ".$f2->num
7/5 .10000093063197482237806917
>$s = Math::Fraction->temp_set
>Math::Fraction->modify_tag(MIXED); Math::Fraction->modi
fy_digits(15)
>"$f1 ".$f2->num
1 2/5 .1000009306319748
>Math::Fraction->temp_set($s)
>Math::Fraction->exists_set($s)
>"$f1 ".$f2->num # Notice how it goes back to the previ
ous settings.
7/5 .10000093063197482237806917
>Math::Fraction->name_set('temp1')
>Math::Fraction->modify_tag(MIXED, NO_AUTO)
>Math::Fraction->modify_digits(60)
>&s(Math::Fraction->tags, Math::Fraction->digits)
MIXED REDUCE SMALL NO_AUTO 60
>Math::Fraction->save_set # If no name is given it will
be saved via
> # its given name
>Math::Fraction->load_set(DEFAULT)
>&s(Math::Fraction->tags, Math::Fraction->digits)
NORMAL REDUCE SMALL AUTO undef
>&s(Math::Fraction->tags('temp1'), Math::Fraction->dig
its('temp1'))
MIXED REDUCE SMALL NO_AUTO 60
> # ^^ Notice how this lets you preview other sets w/out
loading them.
>Math::Fraction->load_set(DEFAULT)
>Math::Fraction->use_set('temp1')
>Math::Fraction->modify_tag(NO_REDUCE)
>&s(Math::Fraction->tags, Math::Fraction->digits)
MIXED NO_REDUCE SMALL NO_AUTO 60
>&s(Math::Fraction->tags('temp1'), Math::Fraction->dig
its('temp1'))
MIXED NO_REDUCE SMALL NO_AUTO 60
> # ^^ Notice how this also modifies the temp1 tag be
cuase it is
> # being used if it was just loaded it would not do
this
> # becuase there is no link.

NOTES

Beta Release

Originally Developed with Perl v 5.003_37 for Win32.

Has been testing on Perl Ver 5.003 on a solaris machine
and perl 5.004 on Windows 95.

Built on a Linux 2 machine with perl v5.003.

Please send me feedback at kevina@clark.net

This is my first real attempt at writing a Perl Module and
at Object-Oriented Programming. (Although I know this is
not a true-true Object-Oriented Module as I cheated a lit
tle). I mainly wrote it to teach my self how to program
Object-Oriently in Perl and for the challenge.

If you know of any faster or simpler way of doing any this
please let me know.

SEE ALSO

Math::FractionDemo, perl(1b)

AUTHOR and COPYRIGHT

Kevin Atkinson, kevina@clark.net

Copyright (c) 1997 Kevin Atkinson. All rights reserved.
This program is free software; you can redistribute it
and/or modify it under the same terms as Perl itself.
Copyright © 2010-2025 Platon Technologies, s.r.o.           Home | Man pages | tLDP | Documents | Utilities | About
Design by styleshout