Mathematical Functions Unit

This is a new library unit featuring routines to implement various transcendental functions and the square root function using fixed point arithmetic. In particular, the output of each is a number in which the decimal point is assumed to be two places from the right. Or if you prefer, simply think of each result as being scaled up by 100.

Overview


--- Square Root Function:

This uses a binary approach involving only bit-shifting, addition and subtraction for greatest speed. The algorithm is based upon an abacus method described in a 1930's pamphlet, "Fundamental Operations in Bead Arithmetic" by C. C. Woo. It was then adapted to the C language in 1985 by Martin Guy. See "Methods of Computing Square Roots" in Wikipedia for additional details.

The input and output are 16-bits, but the internal computation is carried out at 32-bits. The returned value is accurate to two decimal places, with four exceptions at the very high end (4282, 4286, 4290 and 4294).

The maximum argument is 4294. There is no error detection; it is the duty of the calling program to see that the argument lies between 0 and 4294, inclusive.

--- Sine, Cosine and Tangent Functions

These functions are completely general: the arguments may be positive, negative or zero, in one degree increments. In other words, the domains for all three functions are -32768 to 32767 degrees. The values are completely accurate to two decimal places in all cases.

Keep in mind that tangent is not defined for 90 degrees, 270 degrees and all of their coterminal angles. It is the calling program's responsibility to avoid these.

Tables along with trigonometric identities are used.

--- Logarithmic Functions

These functions compute base 2, base e and base 10 logarithms accurate to +/- 0.01. The input arguments are words greater than zero. So, the domains in all cases are 1 to 65535.

A table driven approach with linear interpolation is used.

Commands


There are eight commands, including one to print out a fixed point result with the decimal point properly placed.

procedure F_write(number : integer);
write a fixed point number, decimal point two places from right

function F_log2(arg : word) : integer;
return base-2 logarithm rounded to 2 decimal places

function F_ln(arg : word) : integer;
return natural logarithm (base e) rounded to 2 decimal places

function F_log(arg : word) : integer;
return common logarithm (base 10) rounded to 2 decimal places

function F_sqrt(arg : word) : integer;
square root of a number from 0 to 4294 using abacus method

function F_sin(arg : integer) : integer;
return sine of angle given in degrees

function F_cos(arg : integer) : integer;
return cosine of angle given in degrees

function F_tan(arg : integer) : integer;
return tangent of angle given in degrees

Add It to Your Library Collection


Go fetch the library unit "FIXED_MATH.pas" for your setup now and store it in your Common directory.


Be sure to read over the source code for additional details on how to use it.

Try It Out


If you'd like to see it in operation, then head over to the exercises to find the mathematical functions exercise. Click here to go to it.

No comments:

Post a Comment