Ricardo SIGNES > Math-Calculator-1.021 > Math::Calculator

Download:
Math-Calculator-1.021.tar.gz

Dependencies

Annotate this POD

View/Report Bugs
Module Version: 1.021   Source   Latest Release: Math-Calculator-1.022

NAME ^

Math::Calculator -- a multi-stack calculator class

VERSION ^

version 1.021

 $Id: /my/cs/projects/calc/trunk/lib/Math/Calculator.pm 27812 2006-11-11T02:47:48.077668Z rjbs  $

SYNOPSIS ^

 use Math::Calculator;

 my $calc = Math::Calculator->new;

 $calc->push(10, 20, 30);
 $calc->add;
 $calc->root; # 1.0471285480509  (50th root of 10)

DESCRIPTION ^

Math::Calculator is a simple class representing a stack-based calculator. It can have an arbitrary number of stacks.

METHODS ^

new

This class method returns a new Math::Calculator object with one stack ("default").

current_stack($stackname)

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.

stack($stackname)

This method returns a (array) reference to the stack named, or the current selected stack, if none is named.

top

This method returns the value of the top element on the current stack without altering the stack's contents.

clear

This clears the current stack, setting it to ().

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)

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

dupe duplicates the top value on the current stack. It's identical to $calc->push($calc->top).

_op_two($coderef)

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.

twiddle

This reverses the position of the top two elements on the current stack.

add
 x = pop; y = pop;
 push x + y;

This pops the top two values from the current stack, adds them, and pushes the result.

subtract
 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.

multiply
 x = pop; y = pop;
 push x * y;

This pops the top two values from the current stack, multiplies them, and pushes the result.

divide
 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.

modulo
 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.

raise_to
 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.

root
 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.

sqrt

This method pops the top value from the current stack and pushes its square root.

quorem
divmod

This method pops two values from the stack and divides them. It pushes the integer part of the quotient, and then the remainder.

TODO ^

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.

SEE ALSO ^

AUTHOR ^

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.

COPYRIGHT ^

Math::Calculator is available under the same terms as Perl itself.

syntax highlighting: