NAME

Math::Calculator - a multi-stack calculator class

VERSION

version 1.023

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.

PERL VERSION

This library should run on perls released even a long time ago. It should work on any version of perl released in the last five years.

Although it may work on older versions of perl, no guarantee is made that the minimum required version will not be increased. The version may be increased for any reason, and there is no promise that patches will be accepted to lower the minimum required perl.

METHODS

new

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

current_stack

  $calc->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

  $calc->stack($stackname)

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

top

  $calc->top

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

clear

  $calc->clear

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

push

  $calc->push(@elements);

push pushes the given elements onto the stack in the order given.

push_to

  $calc->push_to($stackname, @elements)

push_to is identical to push, but pushes onto the named stack.

pop

  $calc->pop($howmany)

This method pops $howmany elements off the current stack, or one element, if $howmany is not defined.

pop_from

  $calc->pop_from($stack, $howmany);

pop_from is identical to pop, but pops from the named stack. $howmany defaults to 1.

from_to

  $calc->from_to($from_stack, $to_stack, [ $howmany ])

This pops a value from one stack and pushes it to another.

dupe

  $calc->dupe;

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

_op_two

  $calc->_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

THANKS

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.

AUTHOR

Ricardo SIGNES <cpan@semiotic.systems>

CONTRIBUTORS

  • Ricardo SIGNES <rjbs@codesimply.com>

  • Ricardo Signes <rjbs@semiotic.systems>

COPYRIGHT AND LICENSE

This software is copyright (c) 2005 by Ricardo SIGNES.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.