|
Math::Calculator -- a multi-stack calculator class |
Math::Calculator -- a multi-stack calculator class
version 1.021
$Id: /my/cs/projects/calc/trunk/lib/Math/Calculator.pm 27812 2006-11-11T02:47:48.077668Z rjbs $
use Math::Calculator;
my $calc = Math::Calculator->new;
$calc->push(10, 20, 30); $calc->add; $calc->root; # 1.0471285480509 (50th root of 10)
Math::Calculator is a simple class representing a stack-based calculator. It can have an arbitrary number of stacks.
newcurrent_stack($stackname)The name of the selected stack is returned.
stack($stackname)topclear().
push(@elements)push pushes the given elements onto the stack in the order given.
push_to($stackname, @elements)push_to is identical to push, but pushes onto the named stack.
pop($howmany)$howmany elements off the current stack, or one element, if
$howmany is not defined.
pop_from($stackname, [ $howmany ])pop_from is identical to pop, but pops from the named stack.
from_to($from_stack, $to_stack, [ $howmany ])dupedupe duplicates the top value on the current stack. It's identical to <
$calc-push($calc->top) >>.
_op_two($coderef)twiddleaddx = pop; y = pop; push x + y;
This pops the top two values from the current stack, adds them, and pushes the result.
subtractx = pop; y = pop; push x - y;
This pops the top two values from the current stack, subtracts the second from the first, and pushes the result.
multiplyx = pop; y = pop; push x * y;
This pops the top two values from the current stack, multiplies them, and pushes the result.
dividex = pop; y = pop; push x / y;
This pops the top two values from the current stack, divides the first by the second, and pushes the result.
modulox = pop; y = pop; push x % y;
This pops the top two values from the current stack, computes the first modulo the second, and pushes the result.
raise_tox = pop; y = pop; push x ** y;
This pops the top two values from the current stack, raises the first to the power of the second, and pushes the result.
rootx = pop; y = pop; push x ** (1/y);
This pops the top two values from the current stack, finds the yth root of x, and pushes the result.
sqrtquoremdivmod
I'd like to write some user interfaces to this module, probably by porting Math::RPN, writing a dc-alike, and possibly a simple Curses::UI interface.
I want to add BigInt and BigFloat support for better precision.
I'd like to make Math::Calculator pluggable, so that extra operations can be added easily.
Ricardo SIGNES, <rjbs@cpan.org>
Thanks, also, to Duan TOH. I spent a few days giving him a crash course in intermediate Perl and became interested in writing this class when I used it as a simple example of how objects in Perl work.
Math::Calculator is available under the same terms as Perl itself.
|
Math::Calculator -- a multi-stack calculator class |