Re: Math_Fraction

From: Date: Mon, 25 Oct 2004 18:59:57 +0000
Subject: Re: Math_Fraction
References: 1 2  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-34068@lists.php.net to get a copy of this message
Hi, > Could you put a link to the .phps files so people do not need to > donwload/install the package to check it out? I have just browsed quickly Sure, I added appropriate links to the draft: http://saparev.com/php/Fraction.phps http://saparev.com/php/FractionOp.phps > > > 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(). > > Another option, instead of the 3 comparison methods, is to have one compare() > or cmp() method that returns -1, 0, or 1 depending on whether the first > fraction is less, equal or greater than the second. Just more usual for those > who have use some of the known sci/numerical libs out there. I have to agree that using that interface is much better, so I added a compare() method and removed the previous lt(), eq() and gt() methods. > > - I'm using "friendly" behaviour when creating new fraction - I'm just > > converting the numerator and the denominator to integer. > > That is a good thing IMHO. I meant that maybe it might be good to check if the parameter given "is_int" and raise an error otherwise. > > > - 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. > > Perhaps we could use for Math_Integer, which I would like to redo to support > not just gmp and bcmath, but perhaps a PHP native implementation for arbitrary > integers (something along the lines of java.math.BigInteger). BTW, the GMP lib > does have the those methods. For some of the mangling I've done on Math_Integer > code (still maintaining the released API) see the stuff on CVS, I've not had > time to do a native PHP implementation for big integers, so I had not released > it yet. Yeah. I know that in the GMP library exists such a function, but it seems that it's an external library and currently I don't have it on my WinNT installed by default - so I supposed that the average (win) user will also don't have it. That's the reason I implemented GCD internally. I looked at Math_Integer in the CVS and saw that you're invoking Math_Integer->gcd() but it's not defined in the Math_Integer class. However, I also think that it might be good for Math_Fraction to use Math_Integer and the latter to take care of what libraries are installed, etc. So once GCD is implemented there, we could add a dependency to Math_Integer and remove gcd() from Math_Fraction. > > - 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? > > I think that it should be setup either using a method to switch the behaviour > and/or as a parameter in the operation. Better yet, it can be another method > accessible so the user has the option of invoking it on the result if she so > desires. Also, usually this operation is also known as reduction of a fraction. I added a third optional parameter: boolean ($return_simplified) to the add(), sub(), mult() and div() methods. I can't figure out how to create a general method for switching the behaviour because the class Math_FractionOp is static. Maybe it exists a better way to do it - to define a constant 'MATH_FRACTION_RETURN_SIMPLIFIED' and pass it as a third parameter instead of that boolean...I really didn't found anything on that subject on the docs for contributors. IMHO the "simplify" behaviour should be used by default. I'm also not sure about the name of the method, because I have studied maths in my native language - bulgarian and in french partially (the verb "simplifier" is used there), so I concluded that the same name is used in english. :) However, I added an alias called reduce(). > > - 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. > > Yep, that looks OK. BTW, did you consider expansion of functions to unit > fractions (aka Egyptian fractions)? Currently it's possible to use Math_Fraction as a base for creating whatever extensions are needed. :) > > - Currently new fractions 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 think that instead of the second and third options, perhaps there could be > one that accepts a float and converts it to a fraction (continuous or not). > Although it might be better to have a factory method in Math_FractionOp that > does that, and keep the Math_Fraction constructor simple. Yeah, I also thought of that - to have a method for converting a float to a fraction - that way we could have different constructors - it might be useful, but I think we always could add it in the future without BC breaks. > Your general operation method might be interesting to explore, and you could > implement right now with a switch and some calls to your current methods. I > usually prefer binary operation mehods to just take the two operands, but it > is a question of preferences/styles. For this too I would like to suspend it for future versions as I want to see if it will be needed at all. > > Any other comments, oppinions and advises would be also welcome. > > One option would also be to accept or convert to/from improper/mixed fractions, > e.g. from 5/4 to 1 + 1/4 > > Might want to take a look at some URLs such as: > > http://mathworld.wolfram.com/ContinuedFraction.html > > http://mathworld.wolfram.com/EgyptianFraction.html > > http://www.merriampark.com/fractions.htm > Thank you. Those comments and URLs were really useful to me. I've made all the changes and incremented the version to 0.2. Regards, Kouber Saparev

« previous php.pear.dev (#34068) next »