# Quantity Arithmetic

Physical quantities may be manipulated with basic multiply, division and power operations.

Let Variable1 and Variable2 be physical quantities of the form

```    Variable1 = Number1 [Length]^a . [Mass]^b . [Time]^c . [Temp]^d

Variable2 = Number2 [Length]^e . [Mass]^f . [Time]^g . [Temp]^h
```

where a-g are exponent constants. The quentity sum/difference arithmetic operation

```    Quantity Sum          ==>      Variable1 + Variable2
Quantity Difference   ==>      Variable1 - Variable2
```

will be defined only if a = e, b = f, c = g, and d = h.

ALADDIN computes, by default, a physical quantity result with units matching the second physical quantity in the arithmetic operation.

Example 1 : The script of code:

```    x = 1.0  m;
y = 100 cm;

print "x + y = ", x + y, "\n";
print "y + x = ", y + x, "\n";
```

generates the output:

```    x + y =        200 cm
y + x =          2 m
```

Example 2 : An important feature of quantity operations is the check for consistent units. Suppose that we try to add a quantity with time units to a second quantity having units of length, as in the script:

```    x = 1 in; y = 1 sec;
z = x + y;
```

ALADDIN provides an appropriate fatal error message

```    FATAL ERROR >> In Add() : Inconsistent Dimensions.
FATAL ERROR >> Compilation Aborted.
```

followed by the termination of program execution.

### MULTIPLICATION AND DIVISION

Let Variable1 and Variable2 be physical quantities of the form

```    Variable1 = Number1 [Length]^a . [Mass]^b . [Time]^c . [Temp]^d

Variable2 = Number2 [Length]^e . [Mass]^f . [Time]^g . [Temp]^h
```

where a-g are exponent constants. The quentity product and quentity division arithmetic operations are given by

```    Quantity Product      ==>      Variable1 * Variable2
Quantity Division     ==>      Variable1 / Variable2
```

The units in Variable1 * Variable2 will be

```    [Length]^(a+e) . [Mass]^(b+f) . [Time]^(c+g) . [Temp]^(d+h)
```

and the in Variable1 / Variable2

```    [Length]^(a-e) . [Mass]^(b-f) . [Time]^(c-g) . [Temp]^(d-h)
```

Example 3 : Suppose that the velocity and distance traveled by a free-falling object is given by

```    velocity = gravity * time
distance = 1/2 . gravity * time * time
```

where ``gravity'' is the acceleration due to gravity, and ``time'' is the time of free-fall.

The script of code

```    gravity = 9.81 m/sec^2;
t = 3 sec;

print "acceleration = ", gravity , "\n";
print "velocity     = ", gravity*t , "\n";
print "distance     = ", 1/2*gravity*t*t, "\n";
```

generates the output:

```    acceleration =      9.81 m/sec^2
velocity     =      29.43 m/sec
distance     =      44.14 m
```

Example 4 : The script of code

```    force = 20    N;
mass  =  5   kg;
area  = 20 cm^2;

print "force      = ", force       , "\n";
print "mass       = ", mass        , "\n";
print "area       = ", area (cm^2) , "\n";
print "force/mass = ", force/mass , "\n";
print "force/area = ", force/area , "\n";
```

generates the output:

```    force      =         20 N
mass       =          5 kg
area       =         20 cm^2
force/mass =          4 m/sec^2
force/area =      1e+04 Pa
```

### EXPONENTIALS

Power operations on quantities do not work in quite the same way as the basic add, subtract, multiply and divide operations on quantities. If quantity1 and quantity2 are physical quantities, then the operation

```
quantity1 ^ quantity2;
```

is defined for dimensionless quantity1 and quantity2 , and cases where one of the two operands, but not both operands, has units.

Order of Evaluation

Unlike the basic arithmetic operations, which are evaluated left-to-right, expressions involving power operations are evaluated from right-to-left. Power operations also have higher precedence than the add/subtract operators, and the muldiply/divide operators.

Example 5 : The statement

```    print "2^2^3 = ", 2^2^3 , "\n";
```
generates the output
```    2^2^3 =        256
```
Becasue expressions involving power operations are evaluated from right-to-left, the expression
```   2^2^3
```
is evaluated as if it were written
```   2^(2^3)
```
The expression evaluates to 2^8 , and then 256 .

Example 6 : The following statements show how power operations apply to quantities having units -- the script:

```    print "(2 m)^2    = ", (2 m)^2      , "\n";
print "2^2 m      = ", 2^2 m        , "\n";
print "10^2^2 N/m = ", 10^2^2  N/m  , "\n";
```
generates the output
```    (2 m)^2    =          4 m^2
2^2 m      =          4 m
10^2^2 N/m =      1e+04 N/m
```

You should should carefully note how parentheses are used to bind a unit to a constant, and how the right-to-left evaluation of numerical quantities takes precedence over the unit itself.

Example 7 : Now let's look at a family of expressions where power operations are mixed in with multiply and divide operations. The block of code

```    print "0.25*2^2   m  = ", 0.25*2^2   m, "\n";
print "1/2*2      m  = ",  1/2*2      m, "\n";
print "(1/2*2     m) = ", (1/2*2     m), "\n";
print "(1/2^2     m) = ", (1/2^2     m), "\n";
print "(1/2^2)*(1 m) = ", (1/2^2)*(1 m), "\n";
print "1/2^2*(1 m)   = ",  1/2^2 *(1 m), "\n";
```
generates the output
```    0.25*2^2 m    =     1 m
1/2*2    m    =     1 m
(1/2*2  m)    =     1 m
(1/2^2  m)    =  0.25 1/m
(1/2^2)*(1 m) =  0.25 m
1/2^2*(1 m)   =  0.25 m
```

Notice how parentheses can be used to adjust the precedence of evaluation, and how the multiply and divide operations are evaluated left-to-right.

Example 8 : In this example, we demonstrate the exponential operator on quantity variables:

```    x = 100 kg; z = 10 m;
```
The block of input
```    print "z^-3          = ", z^-3, "\n";
print "(x*z)^2       = ", (x*z)^2, "\n";
```
generates the output
```    z^-3          =  0.001 1/m^3
(x*z)^2       =  1e+06 m^2.kg^2
```

Example 9 : Power operations such as

```      x = (1 m)^(2 sec);
```

are illegal, and will cause ALADDIN to terminate its execution.

Developed in April 1996 by Mark Austin