Turbo Pascal 7.0 Guide
Built-In Arithmetic Functions
The sqrt() function is built into the Pascal programming language. The format of this function is as follows,
myroot :=sqrt(mysquare)
The variables myroot and mysquare have been previously declared as real and integer respectively. In this example, mysquare is the parameter sent to the function. The real variable, myroot is assigned the value returned by the function.
Function | Description | Parameter Type | Return Value Type |
---|---|---|---|
abs(x) | absolute value of x (removes the sign) | integer or real | same as x |
arctan(x) | Inverse tangent of x (in radians) | integer or real | real |
cos(x) | Cosine of x (in radians) | integer or real | real |
exp(x) | Exponential function of x (ex) | integer or real | real |
ln(x) | Natural logarithm of x | integer or real | real |
round(x) | x rounded to the nearest integer | real | integer |
sin(x) | sine of x (in radians) | integer or real | real |
sqr(x) | x2 | integer or real | same as x |
sqrt(x) | square root of x | integer or real | real |
trunc(x) | x rounded to its integer part | real | integer |
To convert from degrees to radians, multiply by PI/180. To convert from radians to degrees, multiply by 180/PI.
General purpose high-level languages tend not to include an exhaustive list of trigonometric functions. Most other functions can be derived from the ones in the list above.
Random Numbers
Computers cannot generate actual random numbers. The term pseudorandom is used to reflect the fact that the numbers appear random.
The built-in function RANDOM, returns a pseudorandom number.
x:= RANDOM | random number in the range 0<=x<1 |
x:= RANDOM(RANGE) | random number in the range 0<=x<RANGE |
In order to ensure that the 'random' numbers are not predictable in a program, we use the RANDOMIZE function at the beginning of the program. Random numbers are generated from a seed value. The randomize function changes the seed value for random numbers based on the current value of the computer's clock.
Example Program
PROGRAM lottery;
VAR a: integer;
BEGIN
randomize;
a:= trunc(random(49)) +1;
writeln(a);
readln;
END.
Exercises
- Write a program to simulate the throwing of 2 dice.
- Write a program which allows the user to input the length of 2 sides of a triangle. Use Pythagoras's theorem to find the length of the longest side.