• Keine Ergebnisse gefunden

DATA CONVERSION PROCEDURES AND FUNCTIONS

Im Dokument INTRODUCTION AND FEATURES (Seite 168-176)

You should use the following procedures and functions to convert data from one type to another:

Function CHR (X: ORDINAL): CHAR;

This function converts any ordinal type to CHAR. The ASCII code for the result is ORO (X). This is an extension to the ISO Pascal, which requires X to be of type INTEGER. An error occurs if ORO (X) > 255 or ORO

(X) < 0. However, the error is caught only if the range

checking switch is on.

Function FLOAT (X: INTEGER): REAL;

This function converts an INTEGER value to a REAL value.

You normally don't need this function, since INTEGER-to-REAL is usually done automatically. However, because FLOAT is needed by the runtime package, it is included at the standard level.

Function FLOAT4 (X: INTEGER4): REAL;

This function converts an INTEGER4 value to a REAL value. This type conversion is also done automatically;

however, it is possible that you might lose precision.

Function ODD (X: ORDINAL): INTEGER;

This function tests the ordinal value X to see whether it is odd. ODD is TRUE only if ORD (X) is odd;

otherwise it is FALSE.

Function ORO (X: VALUE): INTEGER;

This function converts to INTEGER any value of one of the types shown below:

Type of X INTEGER

WORD <= MAXINT WORD > MAXI NT CHAR

Enumerated INTEGER4 Po inter

Return value

X X

X - 2

*

(MAXINT + 1) (i.e., same 16 bits as at start!)

ASCII code for X

position of X in the type definition, starting with 0 Lower 16 bits (i.e., same as ORD(LOWORD(INTEGER4))

Integer value of pointer

Procedure PACK (CONST A: UNPACKED; I: INDEX; VAR Z: PACKED);

This procedure moves elements of an unpacked array to a packed array. If A is an ARRAY [M •• N] OF T and Z is a PACKED ARRAY [U •• V] OF T, then PACK (A, I, Z) is the same as:

FOR J := U TO V DO Z [J] := A [J - U + I]

In both PACK and UNPACK, the parameter I is the initial index within A. The bounds of the arrays and the value of I must be reasonable; i.e., the number of components in the unpacked array A from I to M must be at least as great as the number of components in the packed array Z.

The range checking switch controls checking of the bounds.

Function PRED (X: ORDINAL): ORDINAL;

This function determines the ordinal "predecessor" to X.

The ORD of the result returned is equal to ORD (X) - 1.

An error occurs if the predecessor is out of range or overflow occurs. These errors are caught if appropriate debug switches are on.

Function ROUND (X: REAL): INTEGER;

This function rounds X away from zero. X is of type REAL4 or REAL8; the return value is of type INTEGER.

Examples:

ROUND (1.6) is 2

ROUND (-1.6) is -2

An error occurs if ABS (X + 0.5) )= MAXINT.

Function ROUND4 (X: REAL): INTEGER4;

This function rounds real X away from zero.

type REAL4 or REAL8; the return value is INTEGER4.

Examples:

ROUND4 (1.6) is 2

ROUND4 (-1.6) is-2

An error occurs if ABS (X + 0.5) )= MAXINT4.

Function SUCC (X: ORDI NAL): ORDI NAL;

X is of of type

This function determines the ordinal "successor" to X.

The ORD of the returned result is equal to ORD (X) + 1.

An error occurs if the successor is out of range or overflow occurs. These errors are caught if appropriate debug switches are on.

Function TRUNC (X: REAL): INTEGER;

This function truncates X toward zero. X is of type REAL4 or REAL8, and the return value is of type INTEGER.

EX,amples:

TRUNe (1.6) is 1

TRUNC (-1.6) is -1

An error occurs if ASS (X - 1.0) >= MAXINT.

Function TRUNC4 (X: REAL): INTEGER4;

This function truncates real X towards zero. X is of type REAL4 or REAL8, and the return val ue is of type INTEGER4.

Examples:

TRUNC4 (1.6) is 1 TRUNC4 (-1.6) i s - l

An error occurs if ASS (X - 1.0) >= MAXINT4.

Procedure UNPACK (CONSTS Z: PACKED; VARS A: UNPACKED; I: INDEX);

This procedure moves elements from packed array to an unpacked array. If A is an ARRAY [M •• N] OF T, and Z is a PACKED ARRAY [U •• V] OF T then the above call is the same as:

FOR J : = U TO V DO A [J - U + I] : = Z [J]

In both PACK and UNPACK, the parameter I is the initial index within A. The bounds of the arrays and the value of I must be reasonable; i.e., the number of components in the unpacked array A from I to M must beat least as great as the number of components in the packed array Z.

The range checking switch controls checking of the bounds.

See also PROCEDURE PACK.

Function WRD (X: VALUE): WORD;

This function converts to WORD any of the types shown below:

Type of X Return Value

WORD X

INTEGER >= 0 X

INTEGER < 0 X + MAXWORD + 1 (i.e., same 16 bits as at start!)

CHAR ASCII code for X

Enumerated particular preclslon, you must REAL4) and/or CNDRQQ (CONSTS

FUNCTION CNSRQQ (CONSTS A: REAL4): REAL4;

FUNCTION CNDRQQ (CONSTS A: REAL8): REAL8;

FUNCTION EXP (X: NUMERIC): REAL;

This function returns the exponential value of X (i.e., e to the X). Both X and the return value are of type REAL. To force a particular precision, you must declare EXSRQQ (CONSTS REAL4) and/or EXDRQQ (CONSTS REAL8) and use them instead.

FUNCTION EXSRQQ (CONSTS A: REAL4): REAL4;

FUNCTION EXDRQQ (CONSTS A: REAL8): REAt8;

FUNCTION LN (X: REAL): REAL;

This function returns the logarithm, base e, of X. Both X and the return val ue are of type REAL. To fo rce a particular precision, you must declare LNSRQQ (CONSTS REAL4) and/or LNDRQQ (CONSTS REAL8) and use them instead. An error occurs if X is less than or equal to zero.

FUNCTION LNSRQQ (CONSTS A: REAL4): REAL4;

FUNCTION LNDRQQ (CONSTS A: REAL8): REAL8;

FUNCTION SIN (X: NUMERIC): REAL;

This function returns the sine of X in radians. Both X and the return value are of type REAL. To force a particular preclslon, you must declare SNSRQQ (CONSTS REAL4) and/or SNDRQQ (CONSTS REAL8) and use them instead.

FUNCTION SNSRQQ (CONSTS A: REAL4): REAL4;

FUNCTION SNDRQQ (CONSTS A:· REAL8): REAL8;

FUNCTION SQR (X: NUMERIC): NUMERIC;

This function returns the square of X, where X is of type REAL, INTEGER, WORD, or INTEGER4.

FUNCTION SQRT (X): REAL;

This function returns the square root of X, where X is of type REAL. To force a particular precision, you must declare SRSRQQ (CONSTS REAL4) and/or SRDRQQ (CONSTS REAL8) and use them instead. An error occurs if X is less than 0.

FUNCTION SRSRQQ (CONSTS A: REAL4): REAL4;

FUNCTION SRDRQQ (CONSTS A: REALB): REALB;

REAL Functions

The Pascal runtime library provides several additional REAL4 and REALB functions. If you use them, all variable parameters must be passed as VARS and the functions must be declared with the EXTERN directive.

FUNCTION ACSRQQ (CONSTS A: REAL4): REAL4;

FUNCTION ACDRQQ (CONSTS A: REALB): REAL8;

These functions return the arc cosine of A. Both A and the return value are of type REAL4 or REALB.

FUNCTION AISRQQ (CONSTS A: REAL4): REAL4;

FUNCTION AIDRQQ (CONSTS A: REAL8): REAL8;

These functions return the integral part of A, truncated toward zero. Both A and the return value are of type REAL4 or REALB.

FUNCTION ANSRQQ (CONSTS A: REAL4): REAL4;

FUNCTION ANDRQQ (CONSTS A: REALB): REALB;

Like AISRQQ and AIDRQQ, these functions return the truncated integral part of A, but round away from zero.

Both A and the return value are of type REAL4 or REALB.

FUNCTION ASSRQQ (CONSTS A: REAL4): REAL4;

FUNCTION ASDRQQ (CONSTS A: REAL8): REAL8;

These functions return the arc sine of A. Both A and the return value are of type REAL4 or REALB.

FUNCTION A2SRQQ (A, B: REAL4): REAL4;

FUNCTION A2DRQQ (A, B: REAL8): REAL8;

These functions return the arc tangent of (A/B). Both A and B, as well as the return value, are of type REAL4 or REAL8.

FUNCTION CHSRQQ (CONSTS A: REAL4): REAL4;

FUNCTION CHDRQQ (CONSTS A: REAL8): REAL8;

These functions return the hyperbolic cosine of A. Both A and the return value are of type REAL4 or REAL8.

FUNCTION LDSRQQ (CONSTS A: REAL4): REAL4;

FUNCTION LDDRQQ (CONSTS A: REAL8): REAL8;

These functions return the logarithm, base 10, of A.

Both A and the return value are of type REAL4 or REAL8.

FUNCTION MDSRQQ (CONSTS A, B: REAL4): REAL4;

FUNCTION MDDRQQ (CONSTS A, B: REAL8): REAL8;

A modulo B, defined as:

MDSRQQ (A, B) MDDRQQ (A, B)

A - AISRQQ (A/B) * B A - AIDRQQ (A/B) * B Both A and B are of type REAL4 or REAL8.

FUNCTION MNSRQQ (CONSTS A, B: REAL4): REAL4;

FUNCTION MNDRQQ (CONSTS A, B: REAL8): REAL8;

These functions return the value of A or B, whichever is smaller. Both A and B are of type REAL4 or REAL8.

FUNCTION MXSRQQ (CONSTS A, B: REAL4): REAL4;

FUNCTION MXDRQQ (CONSTS A, B: REALS): REAL8;

These functions return the value of A or B, whichever is larger. Both A and B are of type REAL4 or REAL8.

FUNCTION PIDRQQ (CONSTS A: REAL8; CONSTS B: INTEGER4): REAL8;

FUNCTION PISRQQ (CONSTS A: REAL4; CONSTS B: INTEGER4): REAL4;

These functions return the value is A**B (A to the INTEGER power of B). A is of type REAL4 or REAL8.

FUNCTION PRSRQQ (A, B: REAL4): REAL4;

FUNCTION PRDRQQ (A, B: REALS): REAL8;

These functions return the value A**B (A to the REAL power of B}. Both A and B are of type REAL4 or REAL8.

FUNCTION SBSRQQ (CONSTS A: REAL4): REAL4;

FUNCTION SHDRQQ (CONSTS A: REAL8): REAL8;

These functions return the hyperbolic sine of A. A is of type REAL4 or REAL8.

FUNCTION THSRQQ (CONSTS A: REAL4): REAL4;

FUNCTION THDRQQ (CONSTS A: REAL8): REAL8;

These functions return the hyperbolic tangent of A.

Both A and the return value are of type REAL4 or REAL8.

FUNCTION TNSRQQ (CONSTS A: REAL4): REAL4;

FUNCTION TNDRQQ (CONSTS A: REAL8): REAL8;

These functions return the tangent of A. Both A and the return value are of type REAL4 or REAL8.

Some common mathematical functions are not standard in pascal, but are relatively simple to accomplish with program statements or to define as functions in a program. Some typical definitions are as follows:

SIGN (X) is POWER (X, Y) is

ORO (X > 0) - ORO (X < 0) E XP (Y

*

LN (X))

You could also wri te your' own functions to do the same thing.

For example:

FUNCTION POWER (A, B: REAL): REAL [PURE];

BEGIN

IF A <= 0 THEN

ABORT ('Nonplus real to power', 24, 0);

POWER := EXP (B

*

LN (A));

END;

Im Dokument INTRODUCTION AND FEATURES (Seite 168-176)