REXX program #1: CALC

This REXX program runs on all REXX platforms and is an easy tool to do calculations in all four basic arithmetics (including parentheses).

Here is program CALC:

/* REXX - CALC */
/* Copyright Gerhard Mayer 2001,2002 */
/* Supply the expression to be computed (using all four basic arithmetics
   including parentheses) as a parameter during call, z.B. CALC 17+4
   Get the results on standard output (STDOUT) (usually terminal)
   Note 1: Decimal values MUST be supplied using American notation (using
           DecimalPOINT), e.g. CALC 100*1.16
   Note 2: The expression to be calculated may contain special values:
           PI  represents 3.14159... to compute e.g. the area of a circle
*/
Parse Upper Arg line

Numeric Digits 20
/* Define constants and number of significant decimal digits */
PI = 3.141592654 ; pisuffx = 9

If line = '' Then line = 0
Interpret 'res = ' line
Select
   When Pos('PI',line) > 0 Then suffx = pisuffx
   Otherwise suffx = 0
End
If suffx > 0 Then res = Format(res,,suffx)
Say res
Exit


Examples:

CALC 17+4                  ==> 21
CALC 100*1.16              ==> 116.00


Using special values:
a) Compute circumference of a circle of radius 20
(Formula: C = 2 * radius * pi):
CALC 2*20*PI               ==> 125.663706160

b) Compute area of a circle of radius 20
(Formula: A = radius * radius * pi):
CALC 20*20*PI              ==> 1256.637061600


Conditions of Use ©Gerhard Mayer 2001,2002
Download Back Homepage