NAME
Math::FractionManip - Manipulate fractions
VERSION
version 0.5504
SYNOPSIS
use Math::FractionManip;
my $w = Math::FractionManip->new(0.66667); # 2/3
my $x = Math::FractionManip->new(1, 2); # 1/2
my $y = Math::FractionManip->new(4, 3); # 4/3
my $z = Math::FractionManip->new(4, 3, 'MIXED'); # 1 1/3
print "$x + $y = ", $x + $y, "\n";
print "$x * $y = ", $x * $y, "\n";
print $x->num, "\n"; # 0.5
DESCRIPTION
This module is the resurrection of Math::Fraction
which disappeared from CPAN and wouldn't install anyway. Now that is fixed.
The decimal to fraction functionality of this module is too handy to not have imho. The module Number::Fraction does a proper job of overloaded arithmetic. But it does not handle decimals in the constructor. So the main reason for this module re-appearing is its decimal to fraction functionality.
The source code is extremely ugly and throws warnings all over. But it seems to work! Beware looking under the hood, though.
Fwiw, parts of the following original documentation are confusing. YMMV
Add, subtract, multiply, and divide, etc. fractions just like you would normal numbers.
Convert a decimal, including repeating ones, into a fraction.
Control how the fraction is displayed.
Use arbitrary size numbers in the numerator and the denominator.
Usage
The constructor can have the following arguments:
Numerator, Denominator[, TAGS]
Number, Numerator, Denominator[, TAGS]
Decimal[, TAGS]
"Numerator/Denominator"[, TAGS]
"Number Numerator/Denominator"[, TAGS]
These can be any real number; however if you enter a negative number for anything but the First Number, that negative will be ignored.
TAGS can equal 0 or one or more of the following:
- NORMAL|MIXED|MIXED_RAW|RAW|DEF_MIXED
-
Control how the fraction is displayed.
- NORMAL
-
Display in the #/# form.
- MIXED_RAW
-
Display 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 NORMAL 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 rather to automatically convert between BIG and SMALL as needed.
- 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 packages. (The
**
operator will not work 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
- new
-
The constructor.
- abs
-
Overloaded for fractional arithmetic.
- add
-
Overloaded for fractional arithmetic.
- cmp
-
Overloaded for fractional arithmetic.
- div
-
Overloaded for fractional arithmetic.
- mul
-
Overloaded for fractional arithmetic.
- pow
-
Overloaded for fractional arithmetic.
- sqrt
-
Overloaded for fractional arithmetic.
- sub
-
Overloaded for fractional arithmetic.
- modify_digits
-
modify_digits(NUM)
- reduce
-
Return a reduced fraction but leave the original object untouched.
- string(NORMAL|MIXED|MIXED_RAW|RAW)
-
Return the fraction as a string.
If no parameters are given the default display method will be used.
- decimal|num
-
Return the decimal value of the fraction.
- list|list(MIXED)
-
Return a list containing the fraction. If MIXED is used, then a 3 item list is returned.
- is_tag
-
Return 1 if the tag exists in the fraction. Otherwise return -1.
- is_tag(INC_DEF)
-
Return 1 if the tag exists in the fraction. Return -1 if the tag does not exist but the default is set to that, 0 otherwise.
-
Return a list of the object tags.
-
Return a list of the object tags. If a particular tag is set to read a default the default tag is returned instead.
- modify
-
Modify 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)
-
Modify the fraction numerator.
- modify_den(Denominator)
-
Modify the fraction denominator.
- modify_tag(TAGS)
-
Modify the fraction tags.
The following methods will always modify or read the Class defaults:
- digits
-
Return the default number of digits to return when doing floating point operations with BIG numbers, if set to undef Math::BigFloat will decide.
Default Sets
Default sets are way of modifying the defaults with out effecting other functions. Functions that rely on the default values or modify the default should start with a $set_id = temp_set
and end with a temp_set($set_id)
.
The following methods are meant to manage default sets and will always work on the Class defaults:
- sets
-
Return a list of all the sets.
- name_set
-
Return the name of the current set.
- name_set(NAME)
-
Name the current set.
- save_set
-
Save the current set 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)
-
Load a set.
- copy_set(NAME_ORG, NAME_NEW)
-
Copy a set. Return true if successful.
- del_set(NAME)
-
Delete a set.
- exists_set(NAME)
-
Return true if the set exists.
- use_set(NAME)
-
Use a set. Any changes you make to the used set will also change the original set, like a link.
- temp_set
-
Load a temporary set using the default values and return a unique id.
- temp_set(ID)
-
Restore the original set based on the id.
Unless otherwise specified all the set methods will return the name of the set being worked on if it was successful, false otherwise.
-
List all the tags in SET.
- is_tag(TAG, SET)
-
Return true if TAG exists in SET.
- digits(SET)
-
Return 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 access a fraction as a string the string method will be called. When you access it as a number the decimal method will be called.
Notes on the AUTO tag
With the AUTO tag set Fractions will be converted between SMALL and BIG as needed. The BIG and SMALL tags will be ignored unless you explicitly specify NO_AUTO in order 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, Math::FractionManip->new(7823495784957895478,781344567825678454)
will be stored as a SMALL with some of the digits lost.
When calculating to SMALL numbers that results in a number that is too 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 things down if it constantly converts between the two. Thus in some cases it may be wise to turn it off.
SEE ALSO
The t/01-methods.t program in this distribution
MAINTAINER
Gene Boggs <gene@cpan.org>
AUTHOR
Kevin Atkinson <kevina@clark.net>
COPYRIGHT AND LICENSE
This software is copyright (c) 2022 by Kevin Atkinson.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.