Pascal Math


Home

About the Author

General Programming

Pascal

Delphi

C&C++

Visual Basic

SQL

JAVA Script

Links

 

Pascal Tutor ] Pascal Graphics ] [ Pascal Math ] Pascal Misc ]

  1. A power function eg. X^Y
  2. Sine and Cos functions in degrees
  3. Why are the same random numbers used each time I run my program
  4. How do I obtain high and low parts of a byte variable?

Any questions send them to pascalmath@teentwo.8m.com


1. A power function e.g. X^Y

This is one function that Turbo Pascal does not have and it can be quite useful to save you time.

Function Power(number,top:integer) : real;
begin
    Power := Exp(number*Ln(top));
end;

So instead of writing a loop to times x with itself y times you can use this.

Any questions send them to pascalmath@teentwo.8m.com


2. Sine and Cos functions in degrees

The sin and cos function in pascal is in radians. A complete rotations is 180 degrees but it is also 2*Pi radians. So to get sin and cos to degress just divide by 180 / pi.
Here are the functions:

Function Sine(num:word) : real;
Begin
   Sine := sin(num) / (180 / Pi);
end;

Function Cose(num:word);
   Cose := cos(num) / (180 / Pi);
end;

Any questions send them to pascalmath@teentwo.8m.com


3. Why are the same random numbers used each time I run my program

Software random number generators apply a function to a RandSeed which cycles the seed through its possible values in a quasi-random way. Each call to the random number generator does one iteration of the function, and returns a result based on the new seed value.

When your program loads, this seed will have some default value (probably 0). If you do not change the seed, a series of calls to the random number generator will yield the same series of "random" numbers every time your program is run. (Obviously, this will make it easier to track down the bugs!) Typically, the system clock is used to provide a value for the random number seed: Even if a given task is always run at the same time of day, a difference of a few milliseconds is enough to put a good random number generator in an entirely different part of its sequence.

In Borland Pascal, the command to randomize the seed is (surprise!) Randomize; in Think Pascal for the Mac, the equivalent command is QwertyUiop. Note that you should only call this routine once per program, when it first loads, or at the very least at times separated by minutes or hours - calling it on every timer tick (say) will just reset the 'random' sequence several times a second!

Any questions send them to pascalmath@teentwo.8m.com


4. How do I obtain high and low parts of a byte variable?

function HIBYTEFN (x : byte) : byte;
begin
    hibytefn := x Shr 4;            {Shift right by four bits}
end;

function LOBYTEFN (x : byte) : byte;
begin
    lobytefn := x and 15;            {x and 00001111}
end;

Any questions send them to pascalmath@teentwo.8m.com

Pascal Tutor ] Pascal Graphics ] [ Pascal Math ] Pascal Misc ]

Link of the week 
http://www.cpp-
programming.
com/
Newsgroups
comp.lang.c
comp.lang.c++
comp.programming

This page is best viewed in 800x600x256. This page was last edited Tuesday, June 27, 2000