An arbitrary-precision calculator
Contents
PAL Tutorial - An arbitrary-precision calculator
In release 0.7.x, only the PROSE Assembly Language (PAL) is available, and then only a subset of those instructions. So be aware, it's very low-level programming at this time. To learn more about the PROSE Programming Language, visit http://prose.sourceforge.net.
It is suggested you begin with the following articles before attempting this tutorial.
A full list of available tutorials for the PROSE Assembly Language can be found on the tutorials index page.
This tutorial works through an example PAL program that implements a simple arbitrary-precision calculator.
Objectives
The objective is to write a PAL program that will allow for simple two-operand calculations on integers, floating-point numbers and rational numbers. This will demonstrate the use of variables and functions as well as basic integration with the GNU MP (Multi-Precision) library. Note that this is based on GMP 4.3.2.
The program will understand the following commands supplied on the standard input:
M+N adds two numbers M and N and displays the result M*N multiplies two numbers M and N and displays the result M-N subtracts two numbers M and N and displays the result M/N divides two numbers M and N and displays the result M~N divides two numbers M and N and displays the result (when the number type is rational) int set number type to integer flt set number type to floating-point rat set number type to rational help displays this help page debug toggles debug mode ON and OFF quit exits this program
Integers, floating-point numbers and rational numbers will all take the form accepted by the GNU MP library. Floating-point numbers at this time have a default precision set to at least 64 bits. Rational numbers are represented by X/Y
where /
separates the numerator from the denominator. Because of this, the ~
symbol will be used by the program for requesting a division operation with rational numbers.
Step 1 - Displaying a title
The first step is to display a title when the program is launched. This should be straightforward if you have followed previous tutorials:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Simple arbitrary-precision calculator % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ._init func/def [main], &[.main] func/def [title], &[.func_title] local/rtn %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % main() %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% .main func/call NULL, [title] func/rtn %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % title() % % Display title of program to stderr % % Arguments: % None % % Returns: % None %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% .func_title attr/mod ![.prose.sys.io], [psStreamError], [ pscalc: a simple arbitrary-precision calculator Type 'help' for a list of commands or 'quit' to exit ] func/rtn
Step 2 - Accepting basic commands
Resources from this tutorial
Further reading
See the other tutorials available for the PROSE Assembly Language on the tutorials index page.
PROSE is released with detailed manual pages that describe how PAL operates, and how each instruction is used. These manual pages can be read using the man
command, for example man pal_intro
or man pal_commands
, or from the project links on the main page of this wiki.