On this page:
1.1 Operations
1.1.1 Operations on Measures
m:
m+
m-
m*
m1/
mexpt
mabs
convert
measure=?
->measure
measure->num/  vec
measure->number
measure->real
measure->vector
1.1.2 Operations on Units and Dimensions
u*
u1/
u/
uexpt
u+
->unit
unit-rename
unit=?
d*
d1/
d/
dexpt
dimension=?
1.2 Types and Structs
Measure
Measureof
Number-Measure
Vector-Measure
Number-Measureof
Vector-Measureof
Measureish
Number-Measureish
Vector-Measureish
Real-Measureish
Unit
Unitof
Unitish
Dimension
Sig-Figs
dimension
make-dimension
unit
measure
make-measure
measure-dimension
6.3.90.900

1 Operations, Types, and Structs

1.1 Operations

1.1.1 Operations on Measures

syntax

(m: expr-or-op ...)

(m: expr-or-op ... : type)
 
expr-or-op = expr
  | op
     
op = +
  | -
  | *
  | /
  | ^
This macro lets you write measures and operations on measures using infix notation with the symbols +, -, *, /, and ^.

It is syntactic sugar for using m+, m-, m*, m1/, and mexpt in prefix notation.

Examples:
> (require typed/racket)
> (require typed/measures-with-dimensions)
> (m: 1 meter)

- : Length-Real-Measure

(m: 1 meter)

> (m: 50 centimeter)

- : Length-Real-Measure

(m: 50 centimeter)

> (m: 1 meter + 50 centimeter)

- : Length-Real-Measure

(m: 3/2 meter)

> (m: 1 meter - 50 centimeter)

- : Length-Real-Measure

(m: 1/2 meter)

> (m: 1 meter ^ 2)

- : Measure

(m: 1 (usqr meter))

> (m: 1 meter ^ 2 + 100 centimeter ^ 2)

- : Measure

(m: 101/100 (usqr meter))

> (m: 1 foot + 3 inch)

- : Length-Real-Measure

(m: 5/4 foot)

> (m: 1 meter - 50 centimeter : Length-Real-Measure)

- : Length-Real-Measure

(m: 1/2 meter)

procedure

(m+ m ...)  Measure

  m : Measureish
adds the measures together.

Note for typed racket users: when the typechecker typechecks m+, it doesn’t actually check that its arguments have the same dimensions (it does check at runtime). Instead it typechecks as returning a measure with a union of the dimensions of its arguments. For example, (m+ (make-measure 1 meter) (make-measure 1 second)) typechecks as something that could have either the length dimension or the time dimension. You can fix this by either requiring the type of the result to be what you want, or using inst to instantiate it for a certain dimension.

Examples:
> (require typed/racket)
> (require typed/measures-with-dimensions)
> (ann (m+) Zero-Measure)

- : Zero-Measure

(m: 0 1-unit)

> (m+ (m: 1 meter))

- : Length-Real-Measure

(m: 1 meter)

> (m+ (m: 1 meter) (m: 50 centimeter))

- : Length-Real-Measure

(m: 3/2 meter)

> (m: 1 foot + 3 inch)

- : Length-Real-Measure

(m: 5/4 foot)

> ((inst m+ Length-Dimension) (m: 1 meter) (m: 50 centimeter))

- : Length-Real-Measure

(m: 3/2 meter)

procedure

(m- m)  Measure

  m : Measureish
Negates a measure. To do subtraction, either use the m: macro or use a pattern like (m+ a (m- b)).

Examples:
> (require typed/racket)
> (require typed/measures-with-dimensions)
> (m- (m: 1 meter))

- : Length-Real-Measure

(m: -1 meter)

> (m: 1 meter - 50 centimeter)

- : Length-Real-Measure

(m: 1/2 meter)

procedure

(m* m ...)  Measure

  m : Measureish
Multiplies the measures.

procedure

(m1/ m)  Number-Measure

  m : Number-Measureish
Takes the multiplicative inverse of the measure. To do division, either use the m: macro or use a pattern like (m* a (m1/ b)).

procedure

(mexpt m1 m2)  Number-Measure

  m1 : Number-Measureish
  m2 : Number-Measureish
Takes m1 to the exponent of m2.

procedure

(mabs m)  Real-Measure

  m : Real-Measure
Takes the absolute value of m.

procedure

(convert m u)  Measure

  m : Measure
  u : Unitish
(convert n u1 u2)  Measure
  n : (U Number (Vectorof Real))
  u1 : Unitish
  u2 : Unitish
The first form converts the measure m to the unit u. The second form converts the number or vector n from u1 to u2.

Examples:
> (require typed/racket)
> (require typed/measures-with-dimensions)
> (convert (m: 1 meter) centimeter)

- : Length-Real-Measure

(m: 100 centimeter)

> (convert 1 meter centimeter)

- : Length-Measure

(m: 100 centimeter)

procedure

(measure=? m1 m ...)  Boolean

  m1 : Measureish
  m : Measureish
Returns true if the measures represent the same quantities.

Examples:
> (require typed/racket)
> (require typed/measures-with-dimensions)
> (measure=? (m: 1 meter)
             (m: 100 centimeter))

- : Boolean

#t

> (measure=? (m: 1 meter)
             (m: 1 kilogram))

- : Boolean

#f

> (measure=? (m: 1 radian)
             (m: 1 degree))

- : Boolean

#f

procedure

(->measure m)  Measure

  m : Measureish
Converts from something "measureish" to a Measure.

procedure

(measure->num/vec m)  (U Number (Vectorof Real))

  m : Measureish

procedure

(measure->number m)  Number

  m : Number-Measureish

procedure

(measure->real m)  Real

  m : Real-Measureish

procedure

(measure->vector m)  (Vectorof Real)

  m : Vector-Measureish
Functions for converting dimensionless measures to numbers or vectors. For measures that have a unit with a scalar, it converts to 1-unit first. So mols will be multiplied by approx. 6.02*10^23, percents will be divided by 100, degrees will be multiplied by 2π/360, etc.

1.1.2 Operations on Units and Dimensions

procedure

(u* u ...)  Unit

  u : Unitish

procedure

(u1/ u)  Unit

  u : Unitish

procedure

(u/ u1 u ...+)  Unit

  u1 : Unitish
  u : Unitish

procedure

(uexpt u n)  Unit

  u : Unitish
  n : Exact-Rational

procedure

(u+ u ...+)  Unit

  u : Unitish

procedure

(->unit u)  Unit

  u : Unitish

procedure

(unit-rename u n)  Unit

  u : Unit
  n : Any

procedure

(unit=? u1 u ...)  Boolean

  u1 : Unitish
  u : Unitish
Operations on units

procedure

(d* u ...)  Dimension

  u : Dimension

procedure

(d1/ d)  Dimension

  d : Dimension

procedure

(d/ d1 d ...+)  Dimension

  d1 : Dimension
  d : Dimension

procedure

(dexpt d n)  Dimension

  d : Dimension
  n : Exact-Rational

procedure

(dimension=? d1 d ...)  Boolean

  d1 : Dimension
  d : Dimension
Operations on dimensions

1.2 Types and Structs

equivalent to (Measureof (U Number (Vectorof Real)) Unit).

type

(Measureof n u)

equivalent to (measure n u Sig-Figs).

equivalent to (Number-Measureof Unit).

equivalent to (Vector-Measureof Unit).

equivalent to (Measureof Number u).

equivalent to (Measureof (Vectorof Real) u).

type

Measureish

type

Number-Measureish

type

Vector-Measureish

type

Real-Measureish

type

Unit

equivalent to (Unitof Dimension).

type

(Unitof d)

the type for a unit with the dimension d.

type

Unitish

equivalent to the type (dimension Integer Integer Integer Integer Integer).

equivalent to (U Positive-Integer +inf.0).

struct

(struct dimension (M-expt L-expt T-expt Q-expt Θ-expt))

  M-expt : Integer
  L-expt : Integer
  T-expt : Integer
  Q-expt : Integer
  Θ-expt : Integer

procedure

(make-dimension #:M^ M-expt    
  #:L^ L-expt    
  #:T^ T-expt    
  #:Q^ Q-expt    
  #:Θ^ Θ-expt)  Dimension
  M-expt : Integer
  L-expt : Integer
  T-expt : Integer
  Q-expt : Integer
  Θ-expt : Integer
a struct representing a dimension.

struct

(struct unit (name scalar dimension))

  name : Any
  scalar : Positive-Real
  dimension : Dimension
a struct representing a unit.

struct

(struct measure (number unit sig-figs))

  number : (U Number (Vectorof Real))
  unit : Unit
  sig-figs : Sig-Figs

procedure

(make-measure number unit [sig-figs])  Measure

  number : (U Number (Vectorof Real))
  unit : Unit
  sig-figs : Sig-Figs = +inf.0

procedure

(measure-dimension m)  Dimension

  m : Measure
a struct representing a measure.