# Math Functions

Physical quantities may be manipulated by some math functions ..

### MATH FUNCTIONS

```   FUNCTION          DESCRIPTION
===============================================================

cos (x)           Compute cosine of argument x.
sin (x)           Compute sine of argument x.
tan (x)           Compute tangent of argument x.
atan (x)          Compute arc-tangent of argument x.

abs(x)            Return absolute value of quatity x.

exp(x)            Compute exponential of quantity x.
log(x)            Compute logarithm to "base e" of quantity x.
log10(x)          Compute logarithm to "base 10" of quantity x.

sqrt(x)           Return square root of quatity x.

===============================================================
```

Points to note:

• Default units for arguments to the triginometic functions is radians.
• Most of these functions include checks for range and domain errors. For details, see the examples below.

Example 1 : Let's exercise a couple of the trigonometric functions. The script of code

```    angle = PI/6; s = sin (angle); c = cos (angle);

print "angle         = ", angle, "\n";
print "sin ( angle ) = ", s, "\n";
print "cos ( angle ) = ", c, "\n";
print "sin^2 + cos^2 = ", s^2 + c^2, "\n";
```

generates the output:

```    angle         =     0.5236
sin ( angle ) =        0.5
cos ( angle ) =      0.866
sin^2 + cos^2 =          1
```
Here we compute the sine and cosine for 30 degrees, and verify that sine squared plus cos squared equals 1 (and thank goodness it does).

Example 2 : Arguments to trigonometric functions may also be defined in terms of degrees. The script of code:

```    a1 = 30 deg;
a2 = 15 deg;

s1 = sin (a1); c1 = cos (a1);
s2 = sin (a2); c2 = cos (a2);

print "sin( a1 + a2 )                        = ", sin(a1 + a2), "\n";
print "sin(a1) * cos(a2) + sin(a2) + cos(a1) = ", s1*c2 + s2*c1,"\n";
```

generates the output:

```    sin( a1 + a2 )                        =     0.7071
sin(a1) * cos(a2) + sin(a2) + cos(a1) =     0.7071
```

and numerically verifies the well known double angular formula.

Example 3 : The abs function returns the absolute value of a physical quantity -- as a case in point, the script

```    x = -10 cm/sec;
print "x = ", x , " abs(x) = ", abs(x) , "\n";
print "x = ", x , " abs(x) = ", abs(x) (cm/sec), "\n";
```

generates the output:

```    x =       -0.1 m/sec  abs(x) =        0.1 m/sec
x =       -0.1 m/sec  abs(x) =         10 cm/sec
```

You should notice how in the second example, we have cast the output of abs to an appropriate set of scaled units before printing.

Example 4 : The log and log10 functions compute the logarithms on physical quantities, after their units have been truncated. For example, the two-part script:

```x = 100;
print "x = ", x , "   log(x) = ", log(x) , "\n";
print "x = ", x , " log10(x) = ", log10(x) , "\n";

y = 1000 cm^2;
print "y = ", y , "   log(y) = ", log(y) , "\n";
print "y = ", y , " log10(y) = ", log10(y) , "\n";
```

generates the output:

```x =        100    log(x) =      4.605
x =        100  log10(x) =          2
y =        0.1 m^2    log(y) =     -2.303
y =        0.1 m^2  log10(y) =         -1
```

The result of a logarithm computation is a dimensionless physical quantity.

Example 5 : The sqrt function returns the square root of a physical quantity, with units adjusted accordingly. As a case in point, the script

```x = 16 cm^2;
print "x = ", x , " sqrt(x) = ", sqrt(x) , "\n";
print "x = ", x (cm^2), " sqrt(x) = ", sqrt(x) (cm), "\n";
```

generates the output:

```x =     0.0016 m^2  sqrt(x) =       0.04 m
x =         16 cm^2  sqrt(x) =          4 cm
```

Now let's try to compute the square root of a negative physical quantity. If we redefine

```x = - 16 cm^2;
```

and re-run the sqrt demonstration script, the new block of generated output is:

```x =    -0.0016 m^2  sqrt(x) = sqrt: DOMAIN error
ERROR >> argument out of domain in file 'input-temp' near line 3
FATAL ERROR >> "In Warning()"
```

After telling you where the cause of the run-time error seems to be, ALADDIN terminates its execution.

### RANDOM NUMBERS

```   FUNCTION          DESCRIPTION
================================================================

random ()         Return a random number selected from a uniform
distribution covering [0,1].

===============================================================
```

Points to note:

• The random number generator seed is based on a long int returned by the "time()" function.
• Currently, our algorithm for computing random no's only works for 32-bit architectures.

Example 6 : Here we simply

```    print "Random 1 : ", random(), "\n";
print "Random 2 : ", random(), "\n";
print "Random 3 : ", random(), "\n";
print "Random 4 : ", random(), "\n";
```

call random() four times, and print the results:

```    Random 1 :     0.9187
Random 2 :     0.3461
Random 3 :     0.5872
Random 4 :     0.6017
```

Because the seed for the random number generator is initialized by the "time()" function, each sequence of random numbers will be different (we should probably change this in a future version).

Developed in April 1996 by Mark Austin