scientific calculator

From: Abraham Nemeth 356-5353 (anemeth@ece.eng.wayne.edu)
Date: Tue Mar 29 1994 - 15:27:16 PST


                                       March 29, 1994

Greetings from Dr. Nemeth to the members and friends of the NFB
R&D Committee

  Here is my first communication concerning the NFB scientific
programmable calculator. I'm looking forward to feedback
resulting in a first-class calculator. -- A.N.

=================================================================

Planned features of the NFB scientific programmable calculator
  Runs on a PC with any combination of screen access software
and speech synthesizer.
  Supports fixed, scientific, engineering, and complex numbers
for data and results.
  Is capable of 15-digit precision for data and results.
  Functions within a magnitude range from 10 to the negative 306
power to 10 to the 306 power for data and results.
  Responds in real time to operations from the keyboard.
  Emits a warning boop but otherwise ignores errors which result
from pressing unassigned keys.
  Reports syntax and data errors.
  Supports ten binary operations and 16 unary operations.
  Produces graphs on a braille embosser.
  Is programmable, and programs can be edited.

Other features will be added in later postings.

Binary operations:
  If y is the first operand entered and x is the second, a
binary operation must be entered in the form y <op> x, where <op>
is a binary operator. Each operation is checked for validity
before it is performed. Thus, division is not performed if the
divisor is 0, no operation is performed if it will cause the
calculator to overflow, and exponentiation is not performed if
the base and the exponent do not make mathematical sense (see
item 5 below.) When an invalid operation is detected, the
offending operator is identified and a data error is reported. An
intermediate result or a final result can be elicited from the
calculator by pressing = or <enter> when such a result is
desired. The following ten binary operations are supported:

  1. Addition: + Computes y plus x.
  2. Subtraction: - Computes y minus x.
  3. Multiplication: * Computes y times x.
  4. Division: / Computes y divided by x.
  5. Exponentiation: ^ Computes y to the x power. If y is
negative, x must be an integer, whether 0, negative, or positive.
If y is 0, x must be positive.
  6. Modulus: m Computes the remainder which results when y is
divided by x. y and x must be integers, and x must not be 0.
  7. Pythagorean operator: Alt-p Computes the square root of the
quantity y squared plus x squared.
  8. Percentage: % (Shift-5) Computes the percentage that y is
of x. For example, to compute 16 percent of 50, enter 16 % 50.
The result will be 8. It is interesting to observe that y percent
of x and x percent of y are equal. In the above example, 16
percent of 50 and 50 percent of 16 both equal 8.
  9. Percent change: Alt-5 (related to percent) Computes the
percentage change from y (old value) to x (new value.) For
example, to compute what the percentage change is from 50 to 80,
enter 50 Alt-5 80. The computation is made according to the
formula: (x-y)/y. This produces a decimal result which can
participate in further calculations. If the user needs the result
as a percentage, he must mentally or with the calculator multiply
the result by 100. For the above example, the result is .6 as a
decimal or 60 percent. The same formula works if there has been a
decrease from y to x. If we apply the formula when y is 80 and x
is 50, we obtain a result of minus .375 or minus 37.5 percent.

  10. (Optional) Add h.ms: Alt-H Adds two time periods in
hour-minute-second format. A period of time can be expressed
either as a number with an integral and a fractional part, like
5.6875 hours, or it can be expressed in terms of hours, minutes,
seconds, and a fractional part of a second. The above time period
of 5.6875 hours would be expressed as 5 hours, 41 minutes, 15
seconds. In this example, the number of seconds has no fractional
part. The standard method for storing such a number in the
calculator is 5.4125. In this form, the integer preceding the
decimal point is interpreted as the number of hours, the two
digits that follow the decimal point are interpreted as the
number of minutes, and the remaining digits are interpreted as
the number of seconds. If there are more than two digits for
specifying the number of seconds, an implied decimal point exists
between the second and the third of these digits. Thus,
103.353788 is interpreted as 103 hours, 35 minutes, 37.88
seconds. The purpose of this proposed operator is to add two
numbers, both of which are in the hours-minutes-seconds format,
to obtain a result in the same format. Thus, if we use our two
examples above and enter 5.4125 Alt-H 103.353788, the result is
109.170288, which is interpreted as 109 hours, 17 minutes, 2.88
seconds. I have been examining the manual for the model HP79
Hewlett-Packard scientific programmable printing calculator which
was popular in the late Seventies. That calculator has such a
key. Its keytop bears the legend h.ms+. This calculator has two
other related keys. One converts hours-minutes-seconds from
fractional form into the hours-minutes-seconds format described
above. The other key performs the conversion in the other
direction. Both of these are conversion operations which will not
be discussed in this posting. Should our calculator implement
this feature? If so, is Alt-H a suitable key assignment? Before
making a final judgment, consider that this feature applies as
well to the degree-minute-second format in exactly the same way.
The latter is used in navigation. Those of us who use the Braille
'n Speak know that the stopwatch and the countdown timer report
elapsed or remaining time in the form of hours, minutes, seconds,
and tenths of a second.

How Binary Operations Work
  1. The calculator maintains two push-down stacks. One stack is
for numbers, and the other is for binary operators. Each stack
has a stack pointer which always points to the top of the stack.
Recall that a push-down stack is a data structure in which items
can only be pushed down on to the top of the stack or popped up
from the top of the stack. The interior of the stack is not
accessible. At the beginning of a calculation, both of these
stacks are empty. This means that each of the stack pointers has
a value of 0.
  2. When a number has been keyed in, it is pushed onto the
number stack. This process consists of increasing the value
of the pointer to the number stack by 1 and then assigning the
number just keyed in to the position in the stack array indexed
by the pointer. Similarly, when a binary operator has been
detected, it is eventually pushed onto the operator stack using
the same strategy as for numbers. I will shortly explain (in item
7) the implication of the word "eventually."
  3. When it is time to perform a binary operation, the number
just below the top of the number stack (the y operand) is
regarded as the first operand, and the number on top of the
number stack (the x operand) is regarded as the second operand.
These operands are combined in the manner prescribed by the
binary operator on top of the operator stack.
  4. The binary operator is then popped off of the operator
stack. This is accomplished merely by reducing the value of the
pointer to that stack by 1. If the value of that pointer is 0
when a pop is called for, the operator stack is empty, there is
no operator to pop, and a syntax error is reported.
  5. After the result of the binary operation has been computed
and saved, the top two operands are removed from the number
stack, and the result of the binary operation is pushed onto the
number stack. This is achieved with equal simplicity. First the
value of the pointer to the number stack is reduced by 1. This is
equivalent to popping up the x operand. Then the y operand is
replaced by the saved result of the binary operation. This is
equivalent to popping the y operand off of the number stack and
pushing the result of the binary operation onto the number stack.
  6. The maneuver of performing a binary operation in the manner
just described is called a reduction. After a reduction has been
performed, the sizes of the number stack and of the operator
stack has each been reduced by 1.
  7. Binary operations are not necessarily performed in the
order in which they are encountered. Some binary operations have
precedence over otherst and must be performed first. This means
that some operations, even though they come first in point of
time, must be held in abbeyance pending the performance of
another operation. This is why I used the word "eventually" in
item 2 above. For example, in the expression 2+3*4, the
multiplication (*) must be performed first, even though it comes
after the addition (+) operation. In the above example, the
result is 14, not 20. Most four-function calculators do not
recognize the priorities among binary operators and will, in
fact, report a result of 20. Try it on your Braille 'n Speak
calculator or on any other calculator you have.

Precedence Among the Binary Operators
  To each binary operator there is an assigned rank, as follows:

  Left parenthesis: 0.
  Addition, subtraction: 1.
  Multiplication, division, modulus, percent, percent change: 2.
  Exponentiation: 3.
  Pythagorean operator: 4.
  Add h.ms: 5.

If the operator stack is empty, its (nonexistent) operator at
the top of the stack is taken to have a rank of 0. If op1 has a
higher rank than op2, it must be performed first, even if it was
entered into the calculation at a later time than op2. Thus in
the expression 2+3*4 presented earlier, the multiplication must
be performed before the addition because the rank of the
multiplication operator, which is 2, is higher than the rank
of the addition operator, which is 1.

Parentheses
  Parentheses may be used to control the order in which
computations are performed, particularly if that order differs
from that which would be in effect on the basis of operator rank
alone. Thus, to perform the addition first in the most recent
example, we would enter (2+3)*4. Parenthesis groups must be
evaluated first, before the result of that evaluation can
participate in the rest of the calculation. In a long calculation
containing several parenthesis groups, the groups are evaluated
from left to right. If a parenthesis group contains nested
parenthesis groups, the innermost group must be evaluated first
before the result of that evaluation can participate in the
evaluation of the next outer parenthesis group.

Flowchart
  The flowchart below will handle only binary operators, since
these are the only ones discussed so far. Up to this point, the
following entry types are the only ones allowed:

  1. A number: A complex number is not permitted at this point.
A real number, however, may be in fixed, scientific, or
engineering format.
  2. A left parenthesis.
  3. A binary operator: Any of the ten binary operators
presented above may be used. It is the user's responsibility
to avoid using the result of the Add h.ms operator in a longer
calculation because that result is in hour-minute-second format
rather than in decimal format.
  4. a right parenthesis: Note that no rank was assigned to the
right parenthesis because it is never pushed onto the operator
stack.
  5. = or <enterwh> Recall that either of these keys may be used
interchangeably to elicit an intermediate or a final result from
the calculator. After such a result has been elicited from the
calculator and the next entry is a number, the calculator assumes
that you wish to start a new calculation. It clears the number
stack and pushes that entry onto the number stack as the first
operand. But if the next entry is a binary operator, the
calculator assumes that you wish to continue the calculation
using the intermediate result just obtained as the first operand.

The following flowchart handles parentheses and the precedence
of binary operators in the manner described above. See items 5
and 6 in "How Binary Operations Work" to recall what a reduction
is and what it does.

1. Start -- 2.
2. Supply an entry to the calculator -- 3.
3. What is the type of this entry?
  -- A number -- 4.
  -- a left parenthesis -- 5.
  -- a binary operator -- 6.
  -- a right parenthesis -- 9.
  -- = or <enter> -- 12.
4. Push the number onto the number stack -- 2.
5. Push the
left parenthesis onto the operator stack -- 2.
6. How does the rank of this binary operator compare with the
rank of the operator on top of the operator stack?
  -- greater -- 7.
  -- less than or equal -- 8.
7. Push the binary operator onto the operator stack -- 2.
8. Perform a reduction -- 6.
9. Is there a left parenthesis on top of the operator stack?
  -- yes -- 10.
  -- no -- 11.
10. Pop the left parenthesis off of the operator stack -- 2.
11. Perform a reduction -- 9.
12. Is the operator stack empty?
  -- yes -- 14.
  -- no -- 13.
13. Perform a reduction -- 12.
14. Display the number on top of the number stack -- 15.
15. Stop.

This flowchart contains no traps for detecting errors. In real
life, these must be added all along the way. Experience teaches
that the detection of errors and the corresponding procedures
to deal with them constitute a larger part of a program than the
straightforward process required to achieve the program
objective.

Help
  Of course, I know how to test whether an ASCII character has
been entered. I simply enter that character between quotes in an
IF clause, or I can use the decimal or the hex equivalent of that
character. But how should one test for a non-ASCII keystroke? How
can I test for an Alt-H, an Alt-P, or an Alt-5? Also, please let
me know whether this is of interest, whether it contains too much
or too little detail, etc.

Coming attractions
  If this first posting catches on, I plan to add to this
document by describing the following features:

unary operators.
memory for data.
memory management.
various conversions:
  between radians and degrees.
  between Fahrenheit and Celsius.
  between the hour-minute-second format and the integer-fraction
format for periods of time and for angles.
  between polar and rectangular coordinates.
  between decimal and hex numbers.
canned programs:
  common financial calculations.
  common statistical calculations.
writing and editing your own program:
  labels.
  branching.
  flags.
  subroutines.
logging and printing a calculator session.
producing the graph of a function on a braille embosser.
and others as they arise.



This archive was generated by hypermail 2b29 : Sun Dec 02 2012 - 01:30:03 PST