Math::Calculator -- a multi-stack calculator class
$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.
This class method returns a new Math::Calculator object with one stack ("default").
This method sets the current stack to the named stack. If no stack by the given name exists, one is created and begins life empty. Stack names are strings of word characters. If no stack name is given, or if the name is invalid, the stack selection is not changed.
The name of the selected stack is returned.
This method returns a (array) reference to the stack named, or the current selected stack, if none is named.
This method returns the value of the top element on the current stack without altering the stack's contents.
This clears the current stack, setting it to
push pushes the given elements onto the stack in the order given.
push_to is identical to
push, but pushes onto the named stack.
This method pops
$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 ])
This pops a value from one stack and pushes it to another.
dupe duplicates the top value on the current stack. It's identical to
This method, which is only semi-private because it may be slightly refactored or renamed in the future (possibly to operate on n elements), pops two elements, feeds them as parameters to the given coderef, and pushes the result.
This reverses the position of the top two elements on the current stack.
x = pop; y = pop; push x + y;
This pops the top two values from the current stack, adds them, and pushes the result.
x = 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.
x = pop; y = pop; push x * y;
This pops the top two values from the current stack, multiplies them, and pushes the result.
x = 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.
x = 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.
x = 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.
x = 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.
This method pops the top value from the current stack and pushes its square root.
This method pops two values from the stack and divides them. It pushes the integer part of the quotient, and then the remainder.
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, <firstname.lastname@example.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.