Arithmetical Expressions

23.08.2021

An arithmetical expression consists of operators, which determine the operations, and operands, which supply the data to be calculated. Most operations have two operands. The operator is then written between the operands. For instance, to add two figures, write:

1.5 + 2.3

For the following expression, you would have to program a loop already if you were using Basic:

DataSeries * 2

DataSeries is the name of a data set that could, for instance, contain a series of measurements with 10,000 measurement values. This is multiplied by a factor of 2. FPScript carries out this multiplication on a per-element basis, which means that each value in DataSeries is multiplied by 2. The result of the expression is then once again a data series with 10,000 values.

Expressions with the following structure are also very common:

DataSeries1 + DataSeries2

Here, two data series are added. Again, FPScript proceeds on a per-element basis, i.e. the first value from DataSeries1 is added to the first value from DataSeries2, etc. The result is once again a data series. The two operands should have the same number of values. If this is not the case, the number of calculations matches the number of values in the shorter data set.

You can also string several operations together in a single expression; for instance:

2.5 + 3 * DataSeries

The above example shows linear scaling of a data series. The Y axis division is 2.5 and the gradient is 3. If operations are strung together, operator priority and associativity determine the order in which the individual operations are carried out. Multiplication always takes precedence over addition. Therefore, FPScript first multiplies the DataSeries by the value 3 and then adds 2.5 to the result. If you would like to determine the order of calculation yourself, you have to use parentheses, as you would in standard mathematics:

(2.5 + 3) * DataSeries

In this case, all values in DataSeries are multiplied by the value (2.5 + 3), which equals 5.5.

The associativity of operations plays a role if several operations with the same priority are strung together. Most operations are left-associative, meaning that the operations are carried out from left to right:

DataSeries1 + DataSeries2 - DataSeries2

corresponds to the following expression using parentheses

(DataSeries1 + DataSeries2) - DataSeries2

Some operations only need one operand. In this case the operator is written before the operand:

-DataSeries

negates all values in DataSeries, for instance.

Share article or send as email:

You might be interested in these articles