Previous pageNext pageTable of Contents Home page

Standard Corel SCRIPT functions and procedures

This section of the tutorial contains a list of most useful Corel SCRIPT functions and procedures. Some special functions are not listed here and will be included in the future advanced section of the tutorial.


Arithmetic functions

ABS(x)

Returns the absolute value of a number. Absolute value is the positive value of a number. This is:

ABS(x)=x, if x is greater then 0 and ABS(x)=-x, if x is less than 0.

y%=ABS(-4) ' y is assigned a value of 4.

ACOS(x)

Returns the inverse cosine (arc cosine) of a given value. The result is an angle measured in radians between 0 and p (where p is approximately 3.1415926). x is a numeric expression between -1 and 1.

Note. To convert the result from radians to degrees, use the ANGLECONVERT function or multiply the result by 180/3.141592.

ASC(x$)

Returns the numerical ANSI character value of the first character specified in a string x$. ASC is the opposite of the CHR function, which returns a character when the ANSI value is specified.

s%=ASC("Call") ' Here s% is assigned 67 being the ANSI code for character "C".

ASIN(x)

Returns the inverse sine (arc sine) of a given value. The result is an angle in radians bounded by -p/2 and p/2. x is a numeric expression between -1 and 1.

Note. To convert the result from radians to degrees, use the ANGLECONVERT function or multiply the result by 180/3.141592.

ATAN(x)

Returns the inverse tangent (arc tangent) of a given value. The result is an angle bounded by -p/2 (-90 degrees) and p/2 (90 degrees) measured in radians. To convert the result from radians to degrees, use the ANGLECONVERT function.

CHR(x)

Returns the ANSI character that occupies the specified position in the ANSI character set. CHR is the opposite of ASC, which returns the ANSI code value when the character is entered.

The return value is of string type. Example:

a$=CHR(64)

The string variable a$ is assigned a value of string containing the single character '@' which code is 64

COS(x)

Returns the cosine of an angle measured in radians.

DEC(x$)

Returns the conversion of a hexadecimal value into decimal notation. x$ is string expression representing a hexadecimal number. The result is a long integer thus the highest value you can convert is 7FFFFFFF. The HEX function performs the opposite conversion, from decimal to hexadecimal.

v&=DEC("5AF") 

In the above example the function DEC returns 1455 that is the decimal representation of hexadecimal 5AF. The returned value is assigned to variable v that is declared as long (by means of specifying '&' data type character).

Comment. Hexadecimal notation is a numeric system based on groups of 16 units. Like decimal notation that uses 10 units - digits from 0 to 9, hexadecimal numbers are comprised of decimal digits 0-9 with the extension of extra 6 units represented by letters A-F. Like in decimal notation, hexadecimal numbers are counted in the following order: 0 1 2 3 4 5 6 7 8 9 A B C D E F 10 11 12 ... 19 1A ... 1F 20 21 and so on. Hexadecimal constants can be represented in CorelSCRIPT as the numbers with &H prefix: &H1A equals to decimal 26:

A%=&H7FF
d&=&h20+&h7F
b$="7ABC0":c&=DEC(b$)+&H754

EXP(x)

Raises the mathematical constant e to a given exponent. E is the base of the natural logarithm which equals 2.718281828459. EXP is the inverse of the natural logarithm (see function LN). Both the x and the return value are real numbers (not integers) with double precision.

FIX(x)

Removes an argument's decimal or fraction. An integer is returned. The return value is the integer portion given, without any decimal points represented. As a result, -5.26 becomes -5 and 45.678 becomes 45.

HEX(x)

Converts a number to its corresponding hexadecimal string value. The function returns a string representing the argument in hexadecimal notation. x is a numeric expression specifying a decimal value to convert. This value is rounded to the nearest whole number before being converted. See the comment to DEC function to learn about hexadecimal notation

Example:

x$ = HEX(27)
y$ = HEX(27.25)

In the above example x and y are both passed the value "1B".

INT(x)

Removes an argument's decimal or fraction and rounds down to the nearest integer. An integer is returned.

Both INT and FIX return the integer portion of a given number. However, INT returns the greatest integer less than or equal to the number, while FIX returns the integer portion given, without any decimal points represented. As a result, -5.26 becomes -5 under the FIX function, and -6 under the INT function.

Examples:

x1 = INT(12.65)
y1 = INT(-47.29)

In the above example, x1 equals 12 and y1 equals -48.

x2 = FIX(12.65)
y2 = FIX(-47.29)

In the above example, x2 equals 12 and y2 equals -47.

LN(x)

Returns the natural logarithm (base e) of a number. x is any positive numeric expression. It can't be zero or negative number. If x is not positive, an error is issued. LN is the inverse of the EXP function; therefore LN(EXP(x)) equals x with the given machine precision. This means that all numbers are represented in computer with certain precision. Withing this precision the results may differ from the exact mathematical value.

LOG(x)

Returns the base-10 logarithm of a number.

RANDOMIZE x

Sets the random number generator seed to the integer portion of the argument value. This is not a function in fact - it doesn't return any value. x is any numeric expression. This is an optional parameter. RANDOMIZE uses the argument as a seed to start a new sequence of random numbers. Using RANDOMIZE without an argument initializes the seed to the system timer. If RANDOMIZE isn't used before calling RND, the same sequence of numbers will result every time the random number generator is used.

RND(x)

Returns a random number. x is a numeric expression that determines the bounds of the random number. See the table below.

x's value Random Number Bounds
Greater than 0 A number between 0 (lower bound) and x (upper bound).
Less than 0 A number between x (lower bound) and 0 (upper bound).
Omitted A number between 0 and 1.

Example:

RANDOMIZE
a=RND(5)
b=RND(-6)
c=RND( )

Note. RND had a bug in all versions (maybe except for ver. 6) up to 8. It returns numbers from -x/2 to x/2 instead of returning them from 0 to x. Fortunately, this problem is fixed in CorelSCRIPT 8 (that shipped with CorelDRAW 8 Revision B and Corel VENTURA 8).

SGN(x)

Determines the sign of a number. Returns -1 if the number is negative, 1 if the number is positive, and 0 if the number is 0.

SIN(x)

Returns the sine of an angle measured in radians.

SQR(x)

Returns the positive square root of a number. x is any non-negative numeric expression.

STR(x)

Returns a string representation of a numeric data type. The STR function is useful when you want to manipulate a number as a string (for example, you want to check whether the number contains '0' character).

The first character of the resulting string is a sign position. If the number converted is positive, the first character is a space, if it is negative, the first character is the negative sign character ("-"). You can use only the period as a decimal separator with the STR function. If you're not using a period (.) as a decimal separator, the CSTR function can be used to convert a number to a string. If you're converting dates, they must be in the standard International format (yy/MM/dd hh:mm:ss).

Examples:

s1$ = STR(72)
s2$ = STR(-.34)

The first example assigns " 72" to the variable s1$. The second statement "-0.34" to s2$.

TAN(x)

Returns the tangent of an angle measured in radians.

VAL(x$)

Converts a string to a number. The return value type is double. This function is the opposite of the STR function. x$ is the string expression to be converted. If the string does not begin with a number, VAL returns 0.

The VAL function converts the string up to the first non-number character it encounters, from left to right in the string. Spaces are ignored.

You can use "&H" preffix in the string being converted to specify the number in hexidecimal notation:

MESSAGE VAL("&H1F")

The above example will display the number 31 being the decimal representation of hexadecimal 1F.

You can use only the period as a decimal separator with the VAL function. If you're not using a period (.) as a decimal separator, the CDBL or CSNG function can be used to convert a string to a number.


Data type conversions

A set of standard CorelSCRIPT functions is designated for data type conversions. Converting types can be sometimes very useful. For example, the following expression is evaluated:

A%=SQR(4)+5

Here we are trying to assign the value of the square root of 4 (=2) added to 5 to the variable A. As seen from the data type of the variable A, the result should be integer (7). However due to the CorelSCRIPT language conventions, calculations are performed with the precision of the most precise argument. In our example, 5 is integer but the function SQR returns a double precision number therefore 5 is converted to Double data type prior to addition and actually the formula is the following:

A%=2.000000000+5.000000000

A computer doesn't make difference between digits. It doesn't care wheter the digit is zero or nine - it just adds or multiplies them. In our case, 5.0000... is added to 2.0000... taking into account even the least significant digit. Certainly this will take up some time and more than if two integers were multiplied.

It may be reasonable to make the result of SQR function integer before adding to 5. In this case the two arguments would be integer and added more quickly. Of course, adding two double precision numbers is very fast and can't be noticed, but if you need to sum a set of 1 million numbers?

The following functions convert data types:

CBOL(x)

Converts an expression to a Boolean data type. x is a numeric or string expression. If x evaluates to 0, the resulting value will be FALSE (0). All other numeric expressions are converted to TRUE (-1).

CCUR(x)

Converts an expression to a Currency data type. x is a numeric or string expression.

CDAT(x)

Converts an expression to a Date data type. This function denotes a base date of December 31, 1899 at 12:00:00 A.M. as 1. Each additional whole number is one additional day. Each additional fraction is a portion of a day. When Corel SCRIPT converts the value to a date, it rounds off the value, rather than truncating it.

CDBL(x)

Converts an expression to a Double data type. When Corel SCRIPT converts the value to a double, it rounds off the value, rather than truncating it. If the result of this function lies outside the Double data type range, an error occurs.

CINT(x)

Converts an expression to a Integer data type. In converting the value to an integer, Corel SCRIPT rounds off the value, rather than truncating it. If the decimal portion of the number is 0.5, CINT rounds to the nearest even number. For example, 8.5 rounds to 8, and 9.5 rounds to 10. If the result of this function lies outside the Integer data type range, an error occurs. You can also use INT and FIX to remove the fractional portion of a number.

CLNG(x)

Converts an expression to a Long data type. In converting the value to a long integer, Corel SCRIPT rounds off the value, rather than truncating it. If the result of this function lies outside the Long data type range, an error occurs. You can also use INT and FIX to remove the fractional portion of a number.

CSNG(x)

Converts an expression to a Single data type. In converting the value to a single, Corel SCRIPT rounds off the value, rather than truncating it. If the result of this function lies outside the Single data type range, an error occurs.

CSTR(x)

Converts an expression to a String data type.

The CSTR function is locale aware; that is, it uses the decimal character specified in the user system's regional settings. In Windows 95, click Start, Setting, Control Panel, Regional settings to view and change your system settings.


What next?

On the next page you will learn about CorelSCRIPT string functions.


Previous pageNext pageTable of Contents Home page