You can annotate literals with a unit formula in angle brackets, as shown in the following examples. If you write a floating point expression that uses units, the units in the expression must match. Using floating point numbers together with associated units of measure adds another level of type safety and helps avoid the unit mismatch errors that can occur in formulas when you use weakly typed floating point numbers. You use units of measure in floating point expressions. Therefore, the compiler converts unit formulas into a consistent form, which converts negative powers to reciprocals, groups units into a single numerator and a denominator, and alphabetizes the units in the numerator and denominator.įor example, the unit formulas kg m s^-2 and m /s s * kg are both converted to kg m/s^2. Unit formulas that mean the same thing can be written in various equivalent ways. You do not specify numeric conversion constants in the unit formulas however, you can define conversion constants with units separately and use them in unit-checked computations. Parentheses are not used in unit formulas. For example, the units for a rate would be written as 1/s, where s indicates seconds. You can use 1 in unit expressions, either alone to indicate a dimensionless quantity, or together with other units, such as in the numerator. Units separated by spaces after a / are interpreted as being part of the denominator, but any units following a * are interpreted as being part of the numerator. Multiple units in the denominator should be surrounded by parentheses. For a reciprocal unit, you can either use a negative integer power or a / that indicates a separation between the numerator and denominator of a unit formula. In formulas that involve units, integral powers are supported (positive and negative), spaces between units indicate a product of the two units, * also indicates a product of units, and / indicates a quotient of units. In the previous syntax, measure is a formula that involves units.
The following line defines the measure ml (milliliter) as a cubic centimeter ( cm^3). For example, the following line defines the measure cm (centimeter). The optional part is used to define a new measure in terms of previously defined units. The previous syntax defines unit-name as a unit of measure. These examples demonstrates correctness in arithmetic computations involving units of measure, the feature can also be leveraged for adding type safe annotation with zero representation costs to other types, with approach such as FSharp.UMX project.