Chain Calculations, $ and $$

Home, Up: First Steps

 

On this page:

Chain Calculations, The Previous Result $, The Penultimate Result $$.

 

Chain Calculations

When the first operator in a calculation is misssing one argument, then it uses the result of the previous calculation as the first argument in the new calculation.

For example (as always, the question mark is Hypatia's input prompt, you do not type it):

? 3 4 +

= 7

? 2 *

= 14

 

or:

? 8 2 6 4 SUM

= 20

? SQ

= 400

 

This only works with the first operator in the calculation, and only with 1- and 2-argument operators -- an n-argument operator would not know if one argument were missing.

 

You can turn this behavior on or off:

AUTO$ ON turns it on (this is default), AUTO$ OFF turns it off, AUTO$ displays the current state.

The meaning of the $ sign in these commands will be explained below.

Turning AUTO$ OFF protects against accidentally omitting an argument and getting a wrong result instead of an error message.

If you want it to be turned off by default, you have to add the line AUTO$ OFF to the file hy.ini.

 

The Previous Result $

In each calculation, $ can be used for the result of the previous calculation.

With the AUTO$ feature on (as by default), $ is automatically inserted at the beginning of the calculation line when an argument is missing.

In the examples above, $ could have been written, instead of automatically using the previous result in the new calculation (with AUTO$ OFF, you would need to do it):

? 3 4 +

= 7

? $ 2 *

= 14

 

$ can be used at any place within a calculation, and can also be used more than once.

? 4

= 4

? 2 $ * $ SQ +

= 24

(4 is the previous result, 2 times 4 is 8, then 4 squared is 16, finally 8 plus 16 is 24.)

 

For Hypatia, $ is a "pseudo variable". We will hear more about $ in the context of variables and scripts.

Hypatia does not remember $ when you close the program. (The last calculation result is still preserved in the file hy, see chapter "Results".)

 

The Penultimate Result $$

As $ stands for the previous result, $$ stands for the one before it. With each new calculation, $$ assumes the previous value of $.

Example:

? 3 6 *

= 18

? 4 SQ

= 16

? $$ $ -

= 2

($$ is the result of the penultimate calculation which is 18, $ is the result of the previous calculation which is 16, 18 minus 16 is 2.)

Like $ also $$ is a "pseudo variable", and we will see more of it in the context of variables and scripts.

 

When Hypatia starts, both $ and $$ are zero. After the first calculation, $$ is still zero.

 

Home, Up: First Steps, Prev: Simple Calculations, Next: Copy to Clipboard