User-Defined Constants

Home, Up: Variables and User-Defined Constants

 

On this page:

User Constants Are Variables, Defining Your Own Constants, Constants as Insert Files.

 

User Constants Are Variables

In addition to Hypatia's four built-in constants you can define as many constants as you need.

Technically your constants will be variables -- that is, their names begin with $, and you assign their values the same way you do with variables.

Your constants will not be protected against accidental overwriting, you have to take care that their values are not changed by mistake.

For instance, as a technician or physicist you may want to have this constant:

$kelvin = 273.15

22 $kelvin + will then, for instance, convert 22 degrees Celsius to kelvins.

 

Defining Your Own Constants

Basically there are three ways in which you can define your constants -- $myc (short for $myconstant) stands here for any constant you may want to use:

1. Add $myc = ... lines to hy.ini (you can open hy.ini with the command EDINI).

This way, you will always have your constant or constants available when you use Hypatia.

2. Ceate a file, for instance with the command EDIT myconstants, and write your $myc = ... lines in this file. The file name must not contain spaces.

You will then have to load your constants from this file the same way you retrieve variables that you have saved, by entering _myconstants (that is, putting an underscore character before the file name).

3. You can combine those two methods: create your myconstants file and add the line _myconstants (underscore character before the file name) to hy.ini.

 

Because constants technically are variables, they will be shown by the SHOW command, and will be saved along with other variables by the SAVE command.

Since variables are shown and saved in the order in which they were created, constants will appear before other variables if they were defined with the help of hy.ini.

 

Constants as Insert Files

As an alternative to defining a constant as a variable, in case that it is important to protect if from accidentally being overwritten, you can create a file that contains its value. You can write this value in any format that Hypatia can read.

The name of this file is the name of your constant. It can contain one or more comment line(s).

If, for example, you want to use the "kelvin" constant from the above example in this form, you can use EDIT kelvin to create the file kelvin, and write:

# Difference between kelvins and degrees Celsius

273.15

You can then use your constant by writing its name in parentheses:

22 (kelvin) + will convert 22 degrees Celsius to kelvins.

 

The background of how this works will be explained in chapter "Insert Files and User Defined Elements", page "Insert Files".

In most cases defining your constants as variabes will be more convenient. If a user-defined constant is used in a loop, it will also be faster.

 

Home, Up: Variables and User-Defined Constants, Prev: Saving Variables, Next Chapter: Insert Files and User Defined Elements