Unit Statement

Identifies a unit of measure.

Syntax

Usual construct

[ @SI [ prefix_type ] ] _
Unit unit_names [ = expression ]

Interval-scale construct

Unit unit_names ( parameter In unit ) = expression

Block construct

block_start
{ When condition
    [ statements ] }
…
End [ Unit ]

Parts

@SI
Optional indication of an SI (metric) unit.
prefix_type
Optional after @SI, one of these identifiers:
  • BINARY – Gives these prefixes: kibi or Ki (2^10), mebi or Mi (2^20), gibi or Gi (2^30), tebi or Ti (2^40), pebi or Ti (2^50), exbi or Ei (2^60), zebi or Zi (2^70), yobi or Yi (2^80).
  • LARGE – Gives these prefixes: deca or deka or da (10^1), hecto or h (10^2), kilo or k (10^3), mega or M (10^6), giga or G (10^9), tera or T (10^12), peta or P (10^15), exa or E (10^18), zetta or Z (10^21), yotta or Y (10^24), ronna or R (10^27), quetta or Q (10^30).
  • SMALL – Gives these prefixes: deci or d (10^−1), centi or c (10^−2), milli or m (10^−3), micro or µ or μ or u (10^−6), nano or n (10^−9), pico or p (10^−12), femto or f (10^−15), atto or a (10^−18), zepto or z (10^−21), yocto or y (10^−24), ronto or r (10^−27), quecto or q (10^−30).

If not supplied, the default prefixes are those given by LARGE and small put together.

unit_names
Mandatory one or more names with spaces between each. All names identify the same unit.

Unit name

[ number ] { name | name + suffix | prefix - name }
name
Mandatory basic name for a unit of measure.
number
One of the integer literals that follow:
  • 1name is irregular and grammatically singular. For example, «1 foot».
  • 2name is irregular and grammatically plural. For example, «2 feet».
  • 0name is grammatically singular for zero and one. This occurs in some languages, for example, French. You can use 0 (and no others) with suffix or prefix.
suffix
Mandatory after +. Identifies the regular plural suffix for name. For example, «metre+s» or «inch+es».
prefix
Mandatory before a hyphen (-). Identifies the regular plural prefix for name. This occurs in some languages, most frequently in Africa. For example, in Swahili, «ma-debe».
expression
A conversion factor.
Optional in the usual construct.
Mandatory in the interval-scale construct.
parameter
Mandatory in the interval-scale construct. You use it with expression for a unit with an interval scale, for example, a unit of temperature.
unit
A different unit that is related to this unit.
block_start
The usual construct or interval-scale construct that starts a block construct.
When
Mandatory keyword in the block construct that you can use again and again. It starts a block with a condition and statements.
condition
A clause used to do one or more tests when you try to change a unit variable.

Range construct

When expression_1 To expression_2

Comparison construct

When Is comparison_op expression

Equality construct

When expression
  • The range construct lets you compare a range of values. The value of expression_1 must be less than or equal to expression_2.
  • The comparison construct lets you use all the comparison operators: <, <=, >, >=, =, or <>.
  • The equality construct operates the same as «Is =».
statements
Optional one or more statements. Frequently only the statement Raise is necessary to cause an exception. The variable value holds the new value.
End
Completes the block construct. You can also use End Unit.

Instructions

TODO

Examples

// Make meter, metres, kilometer, millimetres, m, km, mm ...
@SI Unit meter+s metre+s m

// Derived unit.
Unit light_year+s ly = 9.4605284 petameters

// in. is not permitted because IN is a keyword
// and a name cannot have a period,
// but you can write IN between backticks.
Unit inch+es `in` = 2.54 cm

// Kelvin cannot be plural.
Unit Kelvin K
When Is < 0.0
    // Cannot be negative.
    Raise RangeError(value)
End Unit

// °C is not given because it is not easy to write.
// 1 makes a singular name and 2 makes a plural name.
// This is an interval-scale unit, thus the parameter k.
// Also gets the range check from Kelvin.
Unit Celsius 1 degree_C 2 degrees_C (k In Kelvin) = k + 273.15

// Make second, secs, milliseconds, s, ms, ...
@SI SMALL Unit second+s sec+s s

// Make byte, kibibytes, mebibyte, B, KiB, MiB, ...
// Does not permit Kilobyte, megabytes, KB, MB, ...
@SI BINARY Unit byte+s B

See also