Re: Package Proposal: Math_RPN
| From: | Jesus M. Castagnetto | Date: | Tue, 10 Jun 2003 22:48:57 +0000 |
| Subject: | Re: Package Proposal: Math_RPN | ||
| References: | 1 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-17285@lists.php.net to get a copy of this message | ||
Looks good, and works like a charm. +1 for inclusion in PEAR, but read some
comments below.
Giving as input a standard infix expression does the right think, e.g.:
$rpn = new Math_RPN();
$input = '(4 + 2) * 3';
$res = $rpn->calculate($input, '', false);
but when I used a standard postfix (aka RPN) expression, it gives an error:
$input = '4 2 + 3 *';
I am forced to use the following to make it work:
$res = $rpn->calculate(explode(' ',$input), '', true);
That does not seem consistent to me. Also, being that this is an RPN class,
shouldn't the parameter $is_rpn be true by default? (currently is false by
default).
There is an error in your evaluation of the $angle var in the calculate()
method; you are using '=' instead of '==' for a comparison, it should read:
if ($angle == 'deg') $this->_angle = false;
else $this->_angle = true;
or better yet:
$this->_angle = (boolean) ($angle == 'deg');
Finally, I would modify the part where you force the RPN expression to be an
array (which you then proceed to implode), to just omit that at all, unless
there is a reason that I do not understand for needing an array instead of a
string.
So you could then replace lines 227-233 by:
$this->_input = $input;
$this->_output = $this->_input_array = explode(' ',$input);
and if you need to accept and input array, just do the conversion:
if (is_array($input)) {
$input = implode(' ', $input);
}
but just accepting a run of the mill postfix string expression is simpler.
BTW, how many +1's did you have before? I lost track.
--- Maciej Szczytowski <admin@e-rower.pl> wrote:
> Hi,
>
> I changed the addOperator() function. Now you can add function name (or
> class name and function name) as parameter.
> So, for example:
>
> <?php
>
> include('Math/Rpn.php');
> $rpn = new Math_Rpn();
>
> function mysign($number) {
> return ($number >=0 ) ? 1 : -1;
> }
>
> class random {
> function mysum($a,$b) {
> return $a + $b;
> }
> }
>
> $rpn->addOperator('sum','random::mysum',3,2,'sum [user
> class]');
> ^^^^^^^^^^^
> $rpn->addOperator('sign','mysign',3,1,'sign [user function]');
> ^^^^^
>
> $input = 'sign((3 sum 2)*(7-sin(30)*log(10)))';
> $value = $rpn->calculate($input,'deg',false);
>
> ?>
>
> What do you think about it?
>
> New version is here ->
> http://www.maciek.maribex.wizja.net.pl/rpn/
>
> Regards,
> Maciek admin@e-rower.pl
>
> --
> PEAR Development Mailing List (http://pear.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
=====
--- Jesus M. Castagnetto (jcastagnetto@yahoo.com)
Research:
http://metallo.scripps.edu/
Personal: http://www.castagnetto.org/
__________________________________
Do you Yahoo!?
Yahoo! Calendar - Free online calendar with sync to Outlook(TM).
http://calendar.yahoo.com