**Previous:** Using object classes

**Up:** Tutorial

**Next:** Pointers and Indexes

**Previous Page:** Using object classes

**Next Page:** Pointers and Indexes

Calculator is installed as a built-in class in * DSPACK*. The following examples
demonstrate how the calculator may be used interactively. If the object we
want to calculate is undefined - the standard definition template is used.
The expression is stored as an object resource and the object is assigned to
** calculator** class. If the new object is defined, it's type is decided
by the type of the result, otherwise the result is converted to the type needed.

**Calculate mean of all entries**

dsdb> calc mean_y = SUM(point_t.pos.y)/NENT(point_t) dsdb> mean_y mean_y = SUM(point_t.pos.y)/NENT(point_t) Definition of data set: mean_y (mean) ID: 23 Type: Real Section: 1 Machine words: 1 Items: 1 1 CALCtypedef struct mean_y { float CALC; }; dsdb> ls -l Name Type Id Sect Size Items Nent Bytes V3 R 18 0 3 3 0 0 track_t S 19 1 20 11 0 0 point_t S 20 1 9 4 10 360 vertex_t S 21 1 10 5 0 0 POINT_C_T R 22 2 4 4 2 32 mean_y R 23 1 1 1 1 4

dsdb> *mean_y Data set: mean_y Entry: 1 Type: Real (at: 5705336) 1 CALC : 2.04000

In this case the 10 entries of ** point_t** result in a single number.
Usually the result is a vector, like in the following example.

**Example - Calculating distance from Z axis**

dsdb> calc r_point = SQRT(point_t.pos.x^2+point_t.pos.y^2) dsdb> *r_point.CALC 1 r_point.CALC = 1.11803 2 r_point.CALC = 3.90512 3 r_point.CALC = 1.13600 4 r_point.CALC = 3.94098 5 r_point.CALC = 1.15412 6 r_point.CALC = 3.97683 7 r_point.CALC = 1.17239 8 r_point.CALC = 4.01269 9 r_point.CALC = 1.19080 10 r_point.CALC = 4.04855 dsdb>

If we use 'logical' expression, we get 'logical' result:

**Example - Logical expressions**

dsdb> calc distant = r_point.CALC>2 dsdb> distant distant = r_point.CALC>2 Definition of data set: distant (dist) ID: 25 Type: Logical Section: 1 Machine words: 1 Items: 1 1 CALC

typedef struct distant { int CALC; }; dsdb> *distant.CALC 1 distant.CALC = F 2 distant.CALC = T 3 distant.CALC = F 4 distant.CALC = T 5 distant.CALC = F 6 distant.CALC = T 7 distant.CALC = F 8 distant.CALC = T 9 distant.CALC = F 10 distant.CALC = T dsdb>

Calculator objects are filled with data following the stored expressions. In all other respects they behave like all other objects.

In order to perform the calculations the calculator driver makes requests
for all source objects needed. This means that - the ** FILL** message
will be sent to ** point** driver (or ** REFILL** if ** point_t**
has data. It is demonstrated in the following example, where
** point_t** is filled with data following a message from ** calculator**.
Note ** Nent** column for ** point_t**.

**Example - indirect filling of objects**

dsdb> ls -l Name Type Id Sect Size Items Nent Bytes V3 R 18 0 3 3 0 0 track_t S 19 1 20 11 0 0 point_t S 20 1 9 4 0 0 vertex_t S 21 1 10 5 0 0 POINT_C_T R 22 2 4 4 2 32dsdb> calc mean_y = SUM ( point_t.pos.y ) /NENT ( point_t ) dsdb> ls -l Name Type Id Sect Size Items Nent Bytes V3 R 18 0 3 3 0 0 track_t S 19 1 20 11 0 0 point_t S 20 1 9 4 0 0 vertex_t S 21 1 10 5 0 0 POINT_C_T R 22 2 4 4 2 32 mean_y R 23 1 1 1 0 0

dsdb> *mean_y Data set: mean_y Entry: 1 Type: Real (at: 5705320) 1 CALC : 2.04000 dsdb> ls -l Name Type Id Sect Size Items Nent Bytes V3 R 18 0 3 3 0 0 track_t S 19 1 20 11 0 0 point_t S 20 1 9 4 10 360 vertex_t S 21 1 10 5 0 0 POINT_C_T R 22 2 4 4 2 32 mean_y R 23 1 1 1 1 4

dsdb>

In the process of calculating ** mean_y** ** point_t** has been filled
with data.

Fri Nov 4 23:36:02 MET 1994