95fdb59ea6
Former-commit-id: b39a328747c2f3414dc52e009fb6f0aa80ca2492
77 lines
2.1 KiB
Forth
77 lines
2.1 KiB
Forth
module UnitsOfMeasure
|
|
|
|
// The previous syntax defines unit-name as a unit of measure. The optional part is used to define a new measure in terms of previously defined units. For example, the following line defines the measure cm (centimeter).+
|
|
// Distance, cm
|
|
[<Measure>] type cm
|
|
|
|
// The following line defines the measure ml (milliliter) as a cubic centimeter (cm^3).
|
|
// Volume, milliliters.
|
|
[<Measure>] type ml = cm^3
|
|
|
|
|
|
// Mass, grams.
|
|
[<Measure>] type g
|
|
// Mass, kilograms.
|
|
[<Measure>] type kg
|
|
// Weight, pounds.
|
|
[<Measure>] type lb
|
|
|
|
// Distance, meters.
|
|
[<Measure>] type m
|
|
|
|
// Distance, inches.
|
|
[<Measure>] type inch
|
|
// Distance, feet
|
|
[<Measure>] type ft
|
|
|
|
// Time, seconds.
|
|
[<Measure>] type s
|
|
|
|
// Force, Newtons.
|
|
[<Measure>] type N = kg m / s
|
|
|
|
// Pressure, bar.
|
|
[<Measure>] type bar
|
|
// Pressure, Pascals
|
|
[<Measure>] type Pa = N / m^2
|
|
|
|
// Volume, liters.
|
|
[<Measure>] type L
|
|
|
|
// Define conversion constants.
|
|
let gramsPerKilogram : float<g kg^-1> = 1000.0<g/kg>
|
|
let cmPerMeter : float<cm/m> = 100.0<cm/m>
|
|
let cmPerInch : float<cm/inch> = 2.54<cm/inch>
|
|
|
|
let mlPerCubicCentimeter : float<ml/cm^3> = 1.0<ml/cm^3>
|
|
let mlPerLiter : float<ml/L> = 1000.0<ml/L>
|
|
|
|
// Define conversion functions.
|
|
let convertGramsToKilograms (x : float<g>) = x / gramsPerKilogram
|
|
let convertCentimetersToInches (x : float<cm>) = x / cmPerInch
|
|
|
|
|
|
// Using Generic Units
|
|
let genericSumUnits ( x : float<'u>) (y: float<'u>) = x + y
|
|
|
|
let v1 = 3.1<m/s>
|
|
let v2 = 2.7<m/s>
|
|
let x1 = 1.2<m>
|
|
let t1 = 1.0<s>
|
|
|
|
// OK: a function that has unit consistency checking.
|
|
let result1 = genericSumUnits v1 v2
|
|
// Error reported: mismatched units.
|
|
// Uncomment to see error.
|
|
// let result2 = genericSumUnits v1 x1
|
|
|
|
// Creating Aggregate Types with Generic Units
|
|
// Define a vector together with a measure type parameter.
|
|
// Note the attribute applied to the type parameter.
|
|
type vector3D<[<Measure>] 'u> = { x : float<'u>; y : float<'u>; z : float<'u>}
|
|
|
|
// Create instances that have two different measures.
|
|
// Create a position vector.
|
|
let xvec : vector3D<m> = { x = 0.0<m>; y = 0.0<m>; z = 0.0<m> }
|
|
// Create a velocity vector.
|
|
let v1vec : vector3D<m/s> = { x = 1.0<m/s>; y = -1.0<m/s>; z = 0.0<m/s> } |