Quantities in FPScript

23.04.2021

Physical Quantities

One of the strengths of FPScript is the ability to perform calculations using physical quantities composed of a value and unit. FPScript not only manages the unit symbol, but also the SI dimension, which makes it possible to check and, if necessary, transform different units for compatibility.

Entering Quantities

By appending a unit to a constant, the constant becomes a quantity. The value of a quantity can be of the 32-bit or 64-bit floating point data type and real or complex. Quantities of the integer data type are not supported.

The unit is written after the constant and separated by spaces, such as in:

1.3 N

Degrees (°), minutes (´) and seconds (´´) are an exception. These units are written immediately after the numeric value without spaces:

15° + 30´ + 45´´

or

15° + 30' + 45"

New units can be formed from most units using an SI prefix:

1 kHz

1 MHz

1 µm

FPScript also accepts the notation frequently used with u instead of µ for the prefix "micro":

1 um equals 1 µm

However, u is written alone as the symbol for interpreting the atomic mass unit:

1 u equals 1 Da equals 1 Dalton

FPScript accepts the symbol or the name of a unit. Symbols are case sensitive, but case is irrelevant for names. The following are correct forms of notation:

5 V

5 mV

5 Volt

5 volt

5 Millivolt

2 Ω

2 Ohm

The following, on the other hand, are not accepted:

5 mVolt

5 Volts

Derived units can be constructed as a product of powers of known units.

1 N m

1 N*m

1 N·m

Exponents are introduced with the ^ character:

1 m s^-2

For the exponents 2 and 3, FPScript also accepts the superscript numbers:

1 m²

1 m³

Instead of negative exponents, you can also use the / character (division):

1 m/s^2

1 1/min

Make sure to use parentheses correctly:

1 m/(s kg)

If you use Unit monitoring on the Unit Manager tab of the project database Properties dialog box and set it to Tolerant , you can omit the empty spaces or multiplication sign between the basic units:

1 Ws

However, the result of this is that it could be interpreted incorrectly, such as in the following:

5 Volts

is then accepted as

5 V·o·l·t·s

equals 5 volts·octet·liter·ton·second.

Note: You can avoid misinterpretations by using unit symbols only and separating them with spaces. Use µ and not u for the "micro" prefix. Check your use of upper/lower case in the prefix and unit symbol. Typical errors include S (Siemens=conductance) instead of s (seconds=time) or M (mega=106) instead of m (milli=10-3).

You can also use rational exponents. The following example depicts the franklin from the Gaussian unit system in SI units:

1 g^(1/2) cm^(3/2) s^-1

Units may contain a prefactor and an offset. The following depicts the unit ° Fahrenheit in the Kelvin SI unit:

1 0.555555555555555K+255.3722222222222

In the event of ambiguities, you will have to place the unit between apostrophes:

Dim s = 2

v = 3 'Vs'*s

This is also required for a negative prefactor:

5 '-1.602176487E-19 C'

Checking the Unit Symbol

FlexPro breaks a unit down into its elements during compiling and stores the unit symbols of the elements individually with their corresponding exponents. In addition, the unit symbol as specified is saved as a custom unit symbol. For

1 N m/(1/min)

the custom symbol "Nm/(1/min)" is saved with the exponents 1, 1, 1 as the symbols of the basic units "N", "m", "min". When calculating different units, the custom symbol is discarded and the result unit symbol is reconstructed from the symbols of the basic units:

1 N m/(1/min) * 1 min^-1

results in:

1 N m

You can replace the custom symbol with the ChangeUnitSymbol function without affecting the unit. You can also use the same function to delete it. FlexPro will then regenerate the unit symbol from the basic units. For the example above, the result is "N m min".

Using Quantities for Calculations

FPScript adjusts the units to each other before processing the quantities. The following FPScript expression

1 V + 2 mV

provides the result 1.002 V, for instance, and not the value of 3, which would provide a calculation that does not account for the unit. The right operand is transformed to the unit of the left operand prior to addition.

The same applies to comparison operations:

5 mV < 1 V

provides TRUE, for instance, and not FALSE.

Another frequent source of error when performing purely numeric calculations of quantity comparisons is calculating incompatible units. The expression

1 A + 2 V

generates an error message in FPScript, since the units Ampere and Volt have different SI dimensions.

1 km + 5 NM

is permitted, however, since the nautical mile can be converted to km.

If the values, i.e. pure numbers are calculated with quantities, the value then implicitly takes the unit of the quantity:

1 V + 2 = 2 + 1 V = 3 V

This basic principle, which applies to comparison and other operations as well as to function arguments, makes it easier to write "unit-neutral" FPScript code.

For some operations, the unit of the result is formed from the units of the arguments:

1 m / 2 s

provides 0.5 m/s as the result.

(1 m) ^ 2

provides 1 m^2 as the result.

For vector data, i.e. data series and data matrices, all elements always have the same unit. The individual components of an aggregate data structure can, however, have different units, thus, for instance, the Y component of a signal can have the unit Nm and the X component can have the unit s.

The units are accounted for not only in FPScript operators, but also in all FPScript functions.

Derivative(Path)

provides, for instance, the speed in the unit m/s for the case in which Path is a time signal with the X unit s and the Y unit m.

Depending on the unit monitoring setting, FPScript also permits unknown units for the result, although they cannot be transformed. For instance:

1 Dummy + 3 Dummy

results in 4 Dummy.

The value of a quantity determines the Value operator:

Value 5 mm

returns the 64-bit floating point value 5.

With the Unit operator you can transform the unit of a quantity:

Unit<mm> 2 cm

provides 20 mm as the result.

Note: FlexPro's behavior with regard to processing units depends on the Unit Manager tab of the project database Properties dialog box Unit monitoring settings.

Unit Calculation

With the Unit function you can extract the unit of a quantity:

Unit(5.3 V)

A data series with two values is returned here:

Unit({56 V, 7.8 V})

Making these two calls provides 1 V as the result.

Thus, you can carry out unit calculations and use the AdjustUnit function to transform a quantity to the result unit:

AdjustUnit(Frequency, 1 / Unit(Time))

The unit of Frequency is transformed to the reciprocal value of the unit of Time.

Percentage Calculation with the Units % and ppm

When the option below is enabled File > Info > Project Database Properties > Unit Manager > Percentage Calculation with the Units % and ppm the basic arithmetic operations addition and subtraction are calculated in a special way for operands with the unit % or ppm on the right side

200 m + 10 % = 200 m + 200 m * (10/100) = 220 m

200 m - 10 % = 200 m - 200 m * (10/100) = 180 m

For all other operations, the units % and ppm are first converted to the unit 1:

200 m * 20 % = 200 m * (20/100) = 40 m

200 m / 20 % = 200 m / (20/100) = 1000 m

The only case in which the units % and ppm remain is if the right operand has no unit:

10 % * 3 = 30 %

For the unit ppm, the calculation is carried out in a similar manner with the factor 1.000.000 instead of 100.

See Also

Unit Manager

Share article or send as email:

You might be interested in these articles