5. Quantities

A quantity data value consists of a number and a quantity unit. Examples of physical quantity data values (or, simply, quantities) are 1.86 m, 76.5 kg, 44 s, 3.9 m/s, 21 ° C, and 200 mV. Other important examples are monetary amounts like $24.95 or 7€.

In model object attributes and user interface fields, quantities can be represented by plain numbers, if their unit is retrievable from an underlying datatype definition. It may be desirable that a user interface allows the user to change the unit of a quantity such that the numeric value of the user interface field is automatically converted to the chosen unit.

In a simple approach for supporting quantities, there is a (possibly extensible) set of predefined quantity datatypes such as Length, Mass, Time, Temperature and Amount, each of them associated with a fixed standard unit (such as "m" for Length and "$" for Amount) and an optional base unit such that a quantity data value is a plain number defining the quantity in the base unit, if a base unit is defined, otherwise in the standard unit.

Figure 13.3. Quantity types.

Quantity types.

When defining the properties of a class, a quantity-valued attribute would have to be defined by specifying a quantity datatype definition like Length()or Time(Y) as its range. This is, however, not possible in a standard JS constructor definition or an ES6 class definition. Rather, a class definition approach supporting range specifications like cLASSjs, discussed in Volume 2 of the book, is required.

In this approach, the data value of a quantity attribute would be a plain number that is interpreted in combination with the base unit of the attribute's quantity datatype. Quantities could be easily converted to other preferred units in a user interface in a generic manner by invoking the conversion functions defined by the quantity unit instances of the QuantityUnit class.

Gibson2016 reports on a proposal for C++, which allows expressing quantities in a natural way as number/unit pairs in arithmetic expressions based on the C++ features of operator overloading and user-defined literals. For instance, if someone would run 100 meter in 9.58 seconds, a variable for computing her speed could be assigned like so

Speed sp1 = 100.0_m / 9.58_s ;

A general approach to supporting physical quantity datatypes would have to be based on the seven fundamental physical quantity dimensions Length, Mass, Time, Temperature, ElectricCurrent, LuminousIntensity and AmountOfSubstance, which are the basis for defining composite physical quantities such as Area in m2 or Velocity in m/s by defining a map {d1:e1, ..., d7:e7} from the fundamental quantity dimensions di to integers ei as exponents of the generic quantity dimension expression

d1e1 * d2e2 * ... * d7e7