Math::Fraction - To Manipulate Exact Fractions (v.53b, Beta Release)
use Math::Fraction; $a = frac(1,2); $b = frac(6,7); print "$a + $b = ", $a + $b, "$a * $b = ", $a * $b; print $a->num;
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:
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:
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
Controls How the fraction is displayed:
Controls the automatic reduction of fraction after operations are performed on it.
Set rether to automatically convert between BIG and SMALL as needed see "Notes on the AUTO tag".
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)
returns a reduced fraction but leaves the original object untouched.
returns the fraction as a string. if no parameters are given the objects default display method will be used.
returned the decimal value of the fraction
returns a list containing the fraction if MIXED is used than a 3 item list is returned.
returns 1 is that tag exists in the fraction undef if is set to the default -1 otherwise
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.
returns a list of the objects tags
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.
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
same as reduce but it modifies the object instead of returning a fraction.
modifies the fraction's numerator.
modified the fraction's denominator.
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
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)
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.
returns a list of all the sets;
return the name of the current set.
name the current set
saves the current sent based on its name as given above
save the current set as NAME
save the current set using a unique name
loads a set.
copies a set. Returns true if successful.
deletes a set.
returns true if the set exists.
uses a set, that is any changes you make to the used set will also change the original set, like a link.
loads a temp set using the default default values and returns a unique id you need to keep.
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
lists all the tags in SET.
returns true if TAG exists in SET
returns what digits is set to in SET;
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 however some might return decimals like the sin and cos;
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.
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]\n"; $ans = eval $_[0]; print " $ans\n"; } sub evelp {print ">$_[0]\n"; 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 neither 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 effect. 0 >$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->modify_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->modify_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 previous 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->digits('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->digits('temp1')) MIXED NO_REDUCE SMALL NO_AUTO 60 > # ^^ Notice how this also modifies the temp1 tag becuase it is > # being used if it was just loaded it would not do this > # becuase there is no link.
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 little). 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.
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.