Lecture 14: Mathematical Functions



Most languages already have many procedures and functions implemented. As an example, we will discuss the most usefull functions of PASCAL. It is not very important to remember the exact format and use of these functions. Much better is to remember that such things exist and when you need them, read the manual of the language you are using and chose the function you need.
 
 Function 
 description
 argument 
 result
 examples
 Abs  absolute value of parameter
 parameter can be real or integer
 Abs returns the same type
 real
 integer
 real
 integer
 Abs(-23.2) = 23.1
 Abs(12.3) = 12.3
 Abs(-10) = 10
 Cos  Cosine of argument. Argument in 
 radians (2p rad = 360o)
 real  real  Cos(1.0) = 0.5403
 Sin  Sine of argument. Argument in 
 radians (2p rad = 360o)
 real  real  Sin(1.0) = 0.8415
 ArcTan  Inverse tangent of argument  real  real  ArcTan(1.0) = p/4
 Exp  Exponent of argument   real  real  Exp(1.0) = 2.718 
 Ln  Natural logarithm of argument  real (>0)  real  Ln(10.0) = 2.303
 Odd  Test whether argument is odd or even   integer  boolean   Odd(3) = TRUE
 Round  Rounding of argument to closest
 complete number
 real  integer  Round(3.4) = 3
 Round(3.5) = 4
 Int  Rounding down to complete number   real  real  Int(3.99) = 3.00 
 Frac  returns fractional part of argument  real  real  Frac(3.99) = 0.99
 Trunc  Rounding down to complete number  real  integer  Trunc(3.99) = 3 
 Sqrt  Square root of argument   real (>0)  real  Sqrt(3.0) = 1.732 
 Sqr  Square of argument   real  real  Sqr(2.0) = 4.0
 Random  Generates random number
 integer
 real
 integer
 Random = 0.0234
 Random(10) = 3
 Randomize  Randomizes the random number
 generator.
     


Trigonometric functions

As trigonometric functions we have Sin and Cos which return the sine and cosine of the argument, respectively. Note that the Tangent function doesn't exist, but as we all know (hopefully) Tan(x) is equal to Sin(x)/Cos(x). Note that the arguments (the parameters to pass to the function) have units radian (2p rad = 360o). As an example:
program code

PROGRAM Trigonometry;

Var xdeg, xrad, ysin, ycos, ytan: real;

begin
  xdeg := 45.0;
  xrad := Pi*xdeg/180.0; 
   (* convert to radians! Note that the 
    value Pi is defined in PASCAL *) 
  ysin := Sin(xrad);
  ycos := Cos(xrad);
  ytan := ysin/ycos;
  Writeln('x = ',xdeg:0:3);
  WriteLn('Sin = ', ysin:0:4);
  WriteLn('Cos = ', ycos:0:4);
  WriteLn('Tan = ', ytan:0:4);
end.

output

x = 45.000
Sin = 0.7071
Cos = 0.7071
Tan = 1.0000

There exists only one inverse trigonometric function, namely tan-1 which is called ArcTan in PASCAL (and in some languages ATan). The others (ArcCos and ArcSin) can be derived from ArcTan. In the next lecture I will prove this.
program code

PROGRAM InverseTrigonometry;

Var x, y, angle: real;

begin
  x := 0.5;
  y := ArcTan(x);
  angle := 180.0*y/Pi;
  Writeln('x = ', x:0:3);
  WriteLn('ArcTan = ', angle:0:4);
end.

output

x = 0.500
ArcTan = 26.5651


Exponential and power functions

The function Exp returns the exponent of the argument:
  Exp(x) = ex
Ln returns the (natural) logarithm of the argument. The argument should be larger than zero, of course. The 10Log(x) function or in general the nLog(x) function and the general power fucntion xn do not exist in PASCAL, but we can easily derive them from the functions above:
   xn = Exp(Ln(xn)) = Exp(n*Ln(x))
  nLog(x) = nLog(eLn(x)) = Ln(x)* nLog(e) = Ln(x) / eLog(n) = Ln(x) / Ln(n)
In many languages the power function (for example in C: pow(x,n)) is implemented in this way any way.
In most modern computers with mathematical coprocessors (all Pentium processors have one built in) these complicated calculations of Ln, Exp, but also Cos, Sin and ArcTan are executed very fast because they make use of tables with precalculated values inside the processor.
In some languages, confusingly, with Log, the natural logarithm Ln is meant. Therefore, always READ THE INSTRUCTIONS!
Example:
program code

PROGRAM ExponentAndLogarithm;

Var x, y: real;

begin
  x := 100.0;
  y := Ln(x)/Ln(10.0);
  WriteLn('Log(',x:0:1,') = ',y:0:1);
  x := 3.0;
  y := Exp(x*Ln(10));
  WriteLn('10^',x:0:1,' = ',y:0:1);
end.

output

Log(100.0) = 2.0
10^3.0 = 1000.0
Although any power function xn can be expressed with the functions Exp and Ln as described above, two of these powers are so often used that they are implemented with their own functions: Sqr gives x2 and Sqrt gives x1/2. Example:
program code

PROGRAM SquareRootAndSquare;

Var x, y: real;

begin
  x := 3.0;
  y := Sqrt(x);
  WriteLn('The square-root of ', x:0:3,
    ' is ',y:0:1);
  y := Sqr(x);
  WriteLn('The square of ',x:0:3,
    ' is ',y:0:1);
end.

output

The square-root of 3.0 is 1.732
The square of 3.0 is 9.000


Rounding Functions

Four rounding fucntions are implemented in PASCAL, Round, Int, Frac, and Trunc.
Round returns the closest complete number. The returned value is of integer type. Examples: Round(1.2) = 1, Round(2.5) = 3, Round(4.99) = 5.
Int returns the part before the floating point, so, the closest complete number below. The returned value is of type real. This may cause some confusion; Int doesn't convert the number to integer, like in some other languages (C).  Examples Int(1.2) = 1.0, Int(2.5) = 2.0, Int(4.99) = 4, Int(5.0) = 5.0.
Frac returns the part of the number after the floating point, so a number between 0 and 1. It is therefore also of real type. Examples: Frac(1.2) = 0.2, Frac(2.5) = 0.5, Frac(4.99) = 0.99, Frac(5.0) = 0.0.
Trunc does the same as Int, but converts to integer. Examples: Trunc(1.2) = 1, Trunc(2.5) = 2, Trunc(4.99) = 4, Trunc(5.0) = 5.

The odd function

An odd function is the function Odd. It returns TRUE if the argument is odd, FALSE otherwise. This function Odd(x) is therefore equal to(x MOD 2)>0 which also returns TRUE and FALSE, depending on the oddness of the argument x.

Random numbers

One of the nicest things in programming is the use of random numbers. With randowm numbers even the programmer doesn't know the outcome of his own program. It is like flipping a coin or throwing dice. Even though you know exactly what a coin or a die is, you do not know the outcome of an exepriment with them. Generating random numbers with a computer is not easy. Computers are good at doing things in a predictable way, unlike humans, but not good at doing things randomly. There exist fucntions in PASCAL which generate random numbers that we can use. For all our purposes we can consider these numbers to be really random. Only upon closer scrutiny do they prove to be not random at all.
To generate a random number between 0 and 1 we can use the function Random. For example, calling this function five times might generate the series 0.3354, 0.2134, 0.2200, 0.9876, 0.0230.
If we want to have integer numbers between 0 and n-1 we can convert this number with the functions shown in the previous section: Trunc(n*Random). This result can also be obtained directly with the Random fucntion by supplying a parameter to the function: Random(n) returns integer numbers between 0 and n-1.
Note that everytime the program is ran, the random numbers follow the same sequence. If we want to start another sequence, we can call the procedure Randomize.
program code

PROGRAM RandomNumbers;

Var x, y: real;
    i: integer;

begin
  Randomize;
  for i := 1 to 5 do
    Writeln(Random:0:4);
  for i := 1 to 5 do
    WriteLn(Random(10));
end.

output

0.7132
0.5111
0.0638
0.7837
0.3810
8
5
0
8
1
With these functions we can make cardgames, simulate nuclear decay, simulate traffic or any other kind of random process. The function Random is a homogeneous distribution; all numbers between 0 1nd 1 are equally likely to occur. If we want other distributions we can use the Random function as a starting function and convert it to these distributions. In other programming courses these things will be discussed further. For who wants to know more, I can recommend the book "Numerical Recipes in Pascal" (or in C or in Fortran).
 


Quick Test

To test your knowledge of what you have learned in this lesson, click here for an on-line test.

Peter Stallinga. Universidade do Algarve, 17 março 2002