Watch, Follow, &
Connect with Us

Developer Tools Community.

# ID: 17180, Algebra between physical quantities

A useful template class to represent physical quantities such as frequency, time, length, mass or power.

Size: 54,921 bytes
Updated on Mon, 07 Jan 2002 14:14:48 GMT
Originally uploaded on Fri, 14 Dec 2001 14:23:37 GMT
SHA1 Hash: 33F69AE643C661341C02CCF6092686CA1B7798D0

### Explore the files in this upload

Description
Based on the MKSA (Meter, Kilogram, Second, Ampere) metric system, it allows managing a physical data as any other intrinsic type of data of the programming language. The framework is extendible to any kind of quantity foreseen by the MKSA metric system. Only the dimension declaration of the physical quantity is required. All the operations (sum, difference, division, multiplication) and admissible conversion (e.g. from mm to cm) between different units of measurement have been implemented. The quantities can be expressed both in a linear and logarithmic scale (dB). The framework is going to be extended in such away that it will be complaint with the IS (International System) and will allow conversion between different system of measurement (e.g. from feet to meters).
In the following declaration it will be evident the analogy between the type of data and unit of measure (MHz), between the variable and the quantity (frequency) and between the value and the measurement (350) of the quantity:

MHz frequency = 350;

The framework redefines the arithmetic (*, /, +, -) and comparisons (<, >, !=, ==) operators so that we can write simple and safe code as the following:
m L1; // declares L1 to be meters
cm L2 = 20; // declares L2 to be 20 centimetres
mm L3 = L2; // here the measurement of L3 is 200 millimetres
L1 = 2; // assigns to L1 the value of 2 meters
L1 += L2 + 60; // it is equivalent to sum 20 cm + 60 cm + 2 metres;
// the result 2.8 meters is held in L1
if(L1 > L3) // compares two homogenous quantities having different units
cm2 Area = L1*L2; //The measurement of the Area is 56000 square centimetres

Any illegal operation (such as to sum MHz and millimetres) will be detected run time rising an exception.
The trick of the previous code is related to the possibility to define different types of data (m, cm, cm2, Mhz, …) initialising in a different way the same template class (see the code for more details). 