Math::Fraction v.4a Purpose: To Manipulate Exact Fractions Copyright 1997 by Kevin Atkinson (kevina@cark.net) Version .4a (24 Mar 1997) Alpha Release: Not Very Well Tested Developed with Perl v 5.003_37 for Win32. Has been testing on Perl Ver 5.003 on a solaris machine. 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|DEF_MIXED Controls How the fraction is displayed, NORMAL will display it in the #/form MIXED_RAW will display in the #/form; MIXED is the same as the above but if one part is equal to 0 it will leave it off. 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. 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) - 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. (Note: 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) 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 and returns a unique id you need to keep. temp_set(ID) - restores the original set based on the is 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; Also 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; Notes on 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. Notes and Bugs: It won't run with -w switch if anybody can help figure out why I would appreciate it.