Math_Fraction
| From: | Kouber Saparev | Date: | Fri, 22 Oct 2004 18:13:21 +0000 |
| Subject: | Math_Fraction | ||
| Groups: | php.pear.dev | ||
| Request: | Send a blank email to pear-dev+get-34035@lists.php.net to get a copy of this message | ||
Hi folks,
I just finished with a simple package for manipulating fractions (n = x/y,
where x and y are integers). The draft is in the PEPr system:
http://pear.php.net/pepr/pepr-proposal-show.php?id=168
I followed the interface of Math_Complex, i.e. I have two classes: Fraction
and FractionOp (static). The second one is used to define all the possible
arithmetic operations with fractions: addition, substracion, multiplication
and division. It also have 3 methods for comparing fractions: eq(), lt() and
gt().
I'm wish to have some comments and oppinions on the following topics before
moving from "draft" to "proposal":
- I'm using "friendly" behaviour when creating new fraction - I'm just
converting the numerator and the denominator to integer.
- I added LCM (least common multiple) and GCD (greatest common divisor) to
the Math_FractionOp class in order to implement addition, substraction and
comparing methods. Maybe it would be useful to "upper" these two to a higher
"Math" level because they're not for fractions only, so their place is
somewhere else in the Math directory.
- I'm invoking the simplify() method after each arithmetic operation - for
example when adding 1/6 + 3/6, it returns 2/3 and not
4/6. Maybe there could be a third parameter to each method for allowing the
user to tell whether or not to simplify and to have the "simplify" behaviour
by default?
- Currently fraction with denominator 1 are returned as they are, for
example 10/1 instead of 10 (int). I think that's the right way.
- Currently new fraction are created that way: new Fraction($numerator,
$denominator);, but maybe it might be useful to have some alternative ways,
for example: new Fraction("$numerator/$denominator"); new
Fraction($numerator) - and assuming denominator is 1 by default; etc...
- I'm thinking also of a generic Math_FractionOp method that will accept the
operand as a string: "+", "-", "*", "/", "<",
">", "="...and maybe more
combinations, and thus allowing the code to be more readable: $n =
Math_FractionOp::op($n1, "+", $n2);, or so. However that's an advanced and
important topic not only for Math_Fraction. For eaxmple in Perl there's a
possibility to overwrite operators - something that adds another level of
unclearness, but something similar in PEAR might be useful.
Any other comments, oppinions and advises would be also welcome.
Regards,
Kouber Saparev