Pseudo Constants

Home, Up: Operators and Constants

 

In Hypatia's syntax pseudo constants appear like constants, even though their values are not constant at all. Like Hypatia's actual constants, they can be seen as zero-argument operators.

 

On this page:

Pseudo constants RAND, DUP, I, ISLOOP, PASSES, TIME.

 

The last four of the six pseudo constants are meant to be used in the context of scripts and loops -- they are explained here, but you will need to be familiar with scripts and loops to understand their purposes and how to use them.

About scripts and loops see chapters Scripts and Loops.

 

Pseudo Constant RAND

RAND returns a random floating point number in the range of 0.0 to 1.0 (inclusive).

Unlike RANDINT and RANDND, which are 2-argument operators, the pseudo constant RAND does not have an argument.

If you need a random variable within a different range you have to derive it from RAND -- if the random values have to be in the range of a to b, you have to multiply RAND with the difference b minus a, and add a.

For instance, for a random number in the range of -3 to 3:

RND 6 * -3 +

or RND 6 * 3 -

 

Pseudo Constant DUP

DUP duplicates the closest number, constant or variable to its left before any calculation is done, which allows this value to be used in a calculation twice.

A simple example:

? 4 SQ DUP +

= 20

(DUP has the value of 4, the closest number to its left, not 16!)

When there is no number, constant or variable to the left of DUP in the input line, then DUP has the value of the most recent calculation result, the variable $.

You can use DUP several times in one calculation line, referencing the same or different values.

The pseudo operator WHISK is a more powerful but also more complex alternative to DUP, but there is no need for WHISK when DUP does what you need.

DUP (like WHISK) can be used in user-defined operators, see page User-Defined Operators in chapter "Insert Files and User-Defined Elements".

 

Pseudo Constant I

I is the loop index in a DO loop.

In a REPEAT n loop I is the loop index plus 1.

Outside of loops the loop index I is always 1.

Even though I is not a variable, you can display its current value with SHOW I.

 

Pseudo Constant ISLOOP

ISLOOP can be used in a script to determine whether the script is called in a loop or not.

In a DO or REPEAT n loop ISLOOP is 1, outside of a loop (when the script is called directly) ISLOOP is zero.

 

Pseudo Constant PASSES

PASSES returns the number of passes with which a script is called in a loop.

PASSES can be used in an I1: line at the start of a script to prevent the script from being mistakenly called with a too high or too low number of passes.

The value of PASSES could also be used to determine how often in a loop intermediate results are to be shown.

 

Pseudo Constant TIME

In a DO or REPEAT n loop TIME returns the time in seconds since the start of the loop.

Outside of a loop TIME is always zero.

When your script is meant to run in a loop with a large number of passes (Hypatia's maximum is 10 million) TIME can help you to optimize its speed. You can also use TIME to display status messages at given time intervals, or to end a loop after a specified amount of time has elapsed.

Even though TIME is not a variable, in a loop you can display its current value with SHOW TIME.

 

Home, Up: Operators and Constants, Prev: Constants, Next Chapter: Numbers