Documentation contents
Language guide

Units of measure

Say what each number measures, let the compiler check the arithmetic, and convert only where you say so.

On this page

Myceta 2.0. These pages describe the language as planned for 2.0, the first public release, which is still in development. Details may change before release. The examples were type-checked and tested with the development compiler.

Dimensions, quantities and units

Measurement in Myceta has three levels.

Declare them in that order.

dimension Length
dimension Time

quantity Rainfall = Length
quantity Speed = Length / Time

unit m: Length
unit km: Length = 1000.0 * m
unit s: Time
unit mm: Rainfall
unit cm: Rainfall = 10.0 * mm
unit mps: Speed = m / s

Each unit names what it measures after the colon. A further unit is defined from others, by scale as km and cm are, or by combining them as mps is. The compiler checks that metres divided by seconds really is a speed.

Dimensions, quantities and units are shared with any module that imports the one declaring them, so a common set is declared once.

Numbers that carry a unit

A unit in angle brackets is part of a number's type. A literal carries its unit the same way.

node GustDistance = 1500.0<m>
node GustTime = 60.0<s>
node Track = 2.0<km>
node Rain = 12.5<mm>

Empty brackets mark a number that is deliberately dimensionless, such as a ratio or a scale factor. The type is f64<> and a literal is written 2.0<>.

Arithmetic the compiler checks

A function states the units it accepts and the unit it returns. The compiler works through the unit algebra of the body and checks that it arrives at the declared result.

fn speed(distance: f64<m>, duration: f64<s>) -> f64<mps> =
    distance / duration

node GustSpeed = speed(GustDistance, GustTime)

Dividing a length by a length leaves no unit, which the annotation on Share states.

node Share: f64<> = GustDistance / TrackInMetres

Same dimension, different quantity

The quantity level is what stops a number being used as something it is not. A storm track and a depth of rain are both lengths, but they are different quantities, so the compiler refuses to combine them.

node Mixed = Track + Rain                 // refused: a length and a rainfall
node AsDistance = convert<f64<m>>(Rain)   // refused: conversion does not change the quantity

The error names both quantities. Had rainfall been declared as a plain length, both lines would compile and the mistake would pass unnoticed.

Converting between units

convert changes a value to another unit of the same quantity. The target type says which.

node TrackInMetres = convert<f64<m>>(Track)
node Combined = GustDistance + TrackInMetres

node Shower = 1.0<cm>
node ShowerInMillimetres = convert<f64<mm>>(Shower)
node RainTotal = Rain + ShowerInMillimetres

Two kilometres becomes two thousand metres, and one centimetre of rain becomes ten millimetres.

Functions that work for any unit

A unit can be a parameter. scale multiplies a value by a plain factor and returns the same unit it was given.

fn scale<U::Unit>(factor: f64<>, value: f64<U>) -> f64<U> = factor * value

node Doubled = scale(2.0<>, GustDistance)

Crossing a boundary without units

Some code has no units to offer, such as a raw number from a file or a routine that works on plain values. strip_unit removes a unit and attach_unit adds one to a dimensionless value. Use them once at the boundary so the rest of the program stays checked.

node RawSpeed = strip_unit(GustSpeed)
node Limit = attach_unit<f64<mps>>(30.0<>)
node WithinLimit = GustSpeed <= Limit

Settings for a module

The example module opens with two directives. They ask for strict unit checking and require dimensionless values to be marked with <>, as they are above.

directives {
    units.strict = strict
    units.unitless = explicit
}

Units in tests

An expectation can state a value and a tolerance with their units.

test UnitsBehave {
    expect GustSpeed : OK with value = 25.0<mps> within 0.001<mps>
    expect TrackInMetres : OK with value = 2000.0<m> within 0.001<m>
    expect Combined : OK with value = 3500.0<m> within 0.001<m>
    expect Doubled : OK with value = 3000.0<m> within 0.001<m>
    expect RainTotal : OK with value = 22.5<mm> within 0.001<mm>
    expect RawSpeed : OK with value = 25.0 within 1e-9
    expect WithinLimit : OK with value = true
}

Run the example

The complete module is station-units.myc, run by the test manifest. The types reference has the rules in full. The next page covers inputs, scenarios and tests.