Previous | Contents | Index |
The RND function returns a number greater than or equal to zero and less than 1. The RND function always returns a floating-point number of the default floating-point data type. The RND function generates seemingly unrelated numbers. However, given the same starting conditions, a computer always gives the same results. Each time you execute a program with the RND function, you receive the same results.
PRINT RND,RND,RND,RND END |
.76308 .179978 .902878 .88984 |
.76308 .179978 .902878 .88984 |
With the RANDOMIZE statement, you can change the RND function's starting condition and generate random numbers. To do this, place a RANDOMIZE statement before the line invoking the RND function. Note that the RANDOMIZE statement should be used only once in a program. With the RANDOMIZE statement, each invocation of RND returns a new and unpredictable number.
RANDOMIZE PRINT RND,RND,RND,RND END |
.403732 .34971 .15302 .92462 |
.404165 .272398 .261667 .10209 |
The RND function can generate a series of random numbers over any open range. To produce random numbers in the open range A to B, use the following formula:
(B-A)*RND + A |
The following program produces 10 numbers in the open range 4 to 6:
FOR I% = 1% TO 10% PRINT (6%-4%) * RND + 4 NEXT I% END |
5.52616 4.35996 5.80576 5.77968 4.77402 4.95189 5.76439 4.37156 5.2776 4.53843 |
BASIC provides built-in functions that can perform the following:
The following sections describe some of these functions.
11.1.2.1 ASCII Function
The ASCII function returns the numeric ASCII value of a string's first character. The ASCII function returns an integer value from 0 to 255, inclusive. For instance, in the following example, the PRINT statement prints the integer value 66 because this is the ASCII value equivalent of an uppercase B, the first character in the string:
test_string$ = "BAT" PRINT ASCII(test_string$) END |
66 |
Note that the ASCII value of a null string is zero.
11.1.2.2 CHR$ Function
The CHR$ function returns the character whose ASCII value you supply. If the ASCII integer expression that you supply is less than zero or greater than 255, BASIC treats it as a modulo 256 value. BASIC treats the integer expression as the remainder of the actual supplied integer divided by 256. Therefore, CHR$(325) is equivalent to CHR$(69) and CHR$(-1) is equivalent to CHR$(255).
The following program outputs the character whose ASCII value corresponds to the input value modulo 256:
PRINT "THIS PROGRAM FINDS THE CHARACTER WHOSE" PRINT "VALUE (MODULO 256) YOU TYPE" INPUT value% PRINT CHR$(value%) END |
THIS PROGRAM FINDS THE CHARACTER WHOSE VALUE (MODULO 256) YOU TYPE ? 69 E |
THIS PROGRAM FINDS THE CHARACTER WHOSE VALUE (MODULO 256) YOU TYPE ? 1093 E |
Numeric strings are numbers represented by ASCII characters. A numeric string consists of an optional sign, a string of digits, and an optional decimal point. You can use E notation in a numeric string for floating-point constants.
The following sections describe some of the BASIC numeric
string functions.
11.1.3.1 FORMAT$ Function
The FORMAT$ function converts a numeric value to a string. The output string is formatted according to a string you provide. The expression you give this function can be any string or numeric expression. The format string must contain at least one PRINT USING format field. The formatting rules are the same as those for printing numbers with PRINT USING. See Chapter 15 for more information about the PRINT USING statement and formatting rules.
A = 5 B$ = "##.##" Z$ = FORMAT$(A, B$) PRINT Z$ END |
5.00 |
The NUM$ function evaluates a numeric expression and returns a string of characters formatted as the PRINT statement would format it. The returned numeric string is preceded by one space for positive numbers and by a minus sign (-) for negative numbers. The numeric string is always followed by a space. For example:
PRINT NUM$(7465097802134) PRINT NUM$(-50) END |
.74651E+13 -50 |
The NUM1$ function translates a number into a string of numeric characters. NUM1$ does not return leading or trailing spaces or E format. The following example shows the use of the NUM1$ function:
PRINT NUM1$(PI) PRINT NUM1$(97.5 * 30456.23 + 30385.1) PRINT NUM1$(1E-38) END |
3.14159 2999870 .00000000000000000000000000000000000001 |
NUM1$ returns up to 6 digits of accuracy for single-precision and SFLOAT real numbers, up to 16 digits of accuracy for double-precision numbers, up to 10 digits of accuracy for LONG integers, and up to 19 digits of accuracy for QUAD integers. NUM1$ returns up to 15 digits of accuracy for GFLOAT and TFLOAT numbers and up to 33 digits of accuracy for HFLOAT and XFLOAT numbers.
The following example shows the difference between NUM$ and NUM1$:
A$ = NUM$(1000000) B$ = NUM1$(1000000) PRINT LEN(A$); "/"; A$; "/" PRINT LEN(B$); "/"; B$; "/" END |
8 / .1E+07 / 7 /1000000/ |
Note that A$ has a leading and trailing space.
11.1.3.3 VAL% and VAL Functions
The VAL% function returns the integer value of a numeric string. This numeric string expression must be the string representation of an integer. It can contain the ASCII characters 0 to 9, a plus sign (+), or a minus sign (-).
The VAL function returns the floating-point value of a numeric string. The numeric string expression must be the string representation of some number. It can contain the ASCII characters 0 to 9, a plus sign (+), a minus sign (-), or an uppercase E.
The VAL function returns a number of the default floating-point data type. BASIC signals "Illegal number" (ERR=52) if the argument is outside the range of the default floating-point data type.
The following is an example of VAL and VAL%:
A = VAL("922") B$ = "100" C% = VAL%(B$) PRINT A PRINT C% END |
922 100 |
In BASIC, string arithmetic functions process numeric strings as arithmetic operands. This lets you add (SUM$), subtract (DIF$), multiply (PROD$), and divide (QUO$) numeric strings, and express them at a specified level of precision (PLACE$).
String arithmetic offers greater precision than floating-point arithmetic or longword integers and eliminates the need for scaling. However, string arithmetic executes more slowly than the corresponding integer or floating-point operations.
The operands for the functions can be numeric strings representing any integer or floating-point value (E notation is not valid). Table 11-1 shows the string arithmetic functions and their formats, and gives brief descriptions of what they do.
Function | Format | Description |
---|---|---|
SUM$ | SUM$(A$,B$) | B$ is added to A$. |
DIF$ | DIF$(A$,B$) | B$ is subtracted from A$. |
PROD$ | PROD$(A$,B$,P%) | A$ is multiplied by B$. The product is expressed with precision P%. |
QUO$ | QUO$(A$,B$,P%) | A$ is divided by B$. The quotient is expressed with precision P%. |
PLACE$ | PLACE$(A$,P%) | A$ is expressed with precision P%. |
String arithmetic computations permit 56 significant digits. The functions QUO$, PLACE$, and PROD$, however, permit up to 60 significant digits. Table 11-2 shows how BASIC determines the precision permitted by each function and if that precision is implicit or explicit.
Function | How Determined | How Stated |
---|---|---|
SUM$ | Precision of argument | Implicitly |
DIF$ | Precision of argument | Implicitly |
PROD$ | Value of argument | Explicitly |
QUO$ | Value of argument | Explicitly |
PLACE$ | Value of argument | Explicitly |
The SUM$ and DIF$ functions take the precision of the more precise argument in the function unless padded zeros generate that precision. SUM$ and DIF$ omit trailing zeros to the right of the decimal point.
The size and precision of results returned by the SUM$ and DIF$ functions depend on the size and precision of the arguments involved:
In the QUO$, PLACE$, and PROD$ functions, the value of the integer expression argument explicitly determines numeric precision. That is, the integer expression parameter determines the point at which the number is rounded or truncated.
If the integer expression is between -5000 and 5000, rounding occurs according to the following rules:
Note that when rounding numeric strings, BASIC returns only part of the number.
If the integer expression is between 5001 and 15,000, the following rules apply:
The PLACE$ function returns a numeric string, truncated or rounded according to an integer argument you supply.
The following example displays the use of the PLACE$ function with several different integer expression arguments:
number$ = "123456.654321" FOR I% = -5% TO 5% PRINT PLACE$(number$, I%) NEXT I% PRINT FOR I% = 9995 TO 10005 PRINT PLACE$(number$, I%) NEXT I% |
1 12 123 1235 12346 123457 123456.7 123456.65 123456.654 123456.6543 123456.65432 1 12 123 1234 12345 123456 123456.6 123456.65 123456.654 123456.6543 123456.65432 |
The PROD$ function returns the product of two numeric strings. The returned string's precision depends on the value you specify for the integer precision expression.
A$ = "-4.333" B$ = "7.23326" s_product$ = PROD$(A$, B$, 10005%) PRINT s_product$ END |
-31.34171 |
BASIC supplies functions to return the date and time in numeric or string format. The following sections discuss these functions.
Note that you can also use certain system services and Run-Time Library
routines for more sophisticated date and time functions. See the
OpenVMS System Services Reference Manual and the VMS Run-Time Library Routines Volume for more information.
11.1.5.1 DATE$ Function
The DATE$ function returns a string containing a day, month, and year in the form dd-Mmm-yy. The date integer argument to the DATE$ function can have up to six digits in the form yyyddd, where yyy specifies the number of years since 1970 and ddd specifies the day of that year. If the numeric expression is zero, DATE$ returns the current date.
PRINT DATE$(0) PRINT DATE$(126) PRINT DATE$(6168) END |
15-Jun-85 06-May-70 16-Jun-76 |
If you supply an invalid date (for example, day 370 of the year 1973), the results are undefined.
See Section 11.1.5.2 for the recommended replacement for DATE$, which has
a two-digit year field in the result string.
11.1.5.2 DATE4$ Function
The DATE4$ function is strongly recommended as replacement for the DATE$ function to avoid problems in the year 2000 and beyond. It functions the same as the DATE$ function except that the year portion of the result string contains two more digits indicating the century. For example:
PRINT 32150, DATE$ (32150), DATE4$ (32150) |
Produces the following output:
32150 30-May-02 30-May-2002 |
See the description of the DATE$ function for more information.
11.1.5.3 TIME$ Function
The TIME$ function returns a string displaying the time of day in the form hh:mm AM or hh:mm PM. TIME$ returns the time of day at a specified number of minutes before midnight. If you specify zero in the numeric expression, TIME$ returns the current time of day. For example:
PRINT TIME$(0) PRINT TIME$(1) PRINT TIME$(1440) PRINT TIME$(721) END |
03:53 PM 11:59 PM 12:00 AM 11:59 AM |
The TIME function requests time and usage information from the operating system and returns it to your program. The information returned by the TIME function depends on the value of the argument passed to it. The values and the information they return are as follows:
Value | Information Returned |
---|---|
0 | Returns the number of seconds elapsed since midnight |
1 | Returns the current job's CPU time in tenths of a second |
2 | Returns the current job's connect time in minutes |
3 | Returns zero |
4 | Returns zero |
All other arguments to TIME are undefined and cause BASIC to
signal "Not implemented" (ERR=250).
11.1.6 Terminal Control Functions
BASIC provides several terminal control functions. These functions let you:
The CTRLC function enables Ctrl/C trapping, and the RCTRLC function disables Ctrl/C trapping. When Ctrl/C trapping is enabled, control is transferred to the program's error handler when Ctrl/C is detected at the controlling terminal.
Ctrl/C trapping is asynchronous. The trap can occur in the middle of an executing statement, and a statement so interrupted leaves variables in an undefined state. For example, the statement A$ = "ABC", if interrupted by Ctrl/C, could leave the variable A$ partially set to "ABC" and partially left with its previous contents.
For example, if you type Ctrl/C to the following program when Ctrl/C trapping is enabled, an "ABORT" message prints to the file open on channel #1. This lets you know that the program did not end correctly.
WHEN ERROR USE error_handler Y% = CTRLC . . . END WHEN HANDLER error_handler IF ERR = 28 THEN PRINT #1%, "Abort" . . . END HANDLER |
When you trap Ctrl/C with an error handler, your program might be in an inconsistent state; therefore, you should handle the Ctrl/C error and exit the program as quickly as possible. |
Previous | Next | Contents | Index |