Defining new constants¶
This page explains how to define new constants that aren’t included in the library.
Tip
If you think it should be included in the library, feel free to file an issue: there’s no harm in asking.
In order for us to consider it, it should be relatively widely used. It also needs to have an exact value: we don’t have a great way to deal with values that can change over time.
If these conditions don’t apply, then follow the directions in this guide to define a custom constant for your project.
Methods of definition¶
There are two ways to define a new constant. The only difference is whether you need a label for
your constant. In either case, you create a constant by calling make_constant(...)
. The argument
is a unit slot, so you can pass it anything that goes into
a unit slot.
Ad hoc (no special label)¶
Pass any ad hoc unit expression to the unit slot. The constant will produce correct results in code, in every situation. It will even have a label, which will identify it exactly. The label will simply be cumbersome.
Example: speed of light in an ad hoc manner
Here’s how to create a constant for the speed of light, without giving it a special symbol.
Here’s an example use case, in user code:
The above prints "0.8 [299792458 m/s]"
. Notice how the unit label, "[299792458 m/s]"
, is
correct, but cumbersome.
Full unit definition (includes custom label)¶
First, follow a stripped down version of the new unit instructions to define a unit for the constant. The only thing you need to give it is a label; you can omit the instructions for quantity makers and so on.
Next, pass an instance of this custom unit to make_constant
.
Example: speed of light with full unit definition
Here’s how to create a constant for the speed of light using a full custom unit, with label.
Here’s an example use case, in user code:
The above prints "0.8 c"
.