Numeric Computation

Numeric computations deal exclusively with numeric values. Thus, adding two numbers and finding a sine or a logarithm are all numeric operations; while converting bases and converting a number to a string or a string to a number are not. (Converting bases and converting numbers to strings and strings to numbers are covered in the chapter on "String Manipulation.")

Numeric Data Types

There are three numeric data types available in BASIC: INTEGER, REAL and COMPLEX. Any numeric variable that is not declared an INTEGER or COMPLEX variable is a REAL variable. This section covers these data types.

REAL Data Type

The valid range for REAL numbers is approximately:

-1.797 693 134 862 315 × 10308 thru 1.797 693 134 862 315 × 10308

or

-MAXREAL     thru     +MAXREAL
 

which are the functions used to obtain the above range values.

The smallest non-zero REAL value allowed is approximately:

+/- 2.225 073 858 507 202 × 10-308

or

 
+/- MINREAL
 

A REAL can also have the value of zero.

Underflow Handling

On Series 700 computers, denormalized numbers may occur as a result of numeric operations if underflow_mode is set to IGNORE. (Refer to the "controlling Underflow Handling (underflow_mode) (Series 700 Only)" section in the "Creating Environment and Autostart Files" chapter of Using HP BASIC/UX for more information on underflow_mode). The minimum REAL number that will result from performing numeric computations in HP BASIC/UX 700 with underflow_mode set to IGNORE is 4.940 656 458 412 465 × 10-324

With underflow_mode=ignore it is recommended (for compatibility with HP BASIC/UX 300, HP BASIC/WS, and the other underflow_mode settings) that you check the absolute value of potentially denormalized numbers against MINREAL and set them to 0 if they are denormalized. For example,

 
IF ABS(Small_num)<MINREAL THEN Small_num=0
 

The IEEE 754 Math specification defines the values INF (infinity) NaN (not a number). These numbers cannot normally be created by BASIC, however may appear when data is read from interfaces, files, pipe, or other applications that can create these numbers. With HP BASIC/UX 700 computations using INF and NaN give these results:
+/-INF will produce +/-INF.
signalling NaN will produce a signalling NaN.
non-signalling NaN will produce ERROR 21 Exception in math operation

With HP BASIC/UX 700 printing INF and NaN give these results:
+INF will produce the text " ++"
-INF will produce the text "--–"
signalling NaN will produce the text "?"
non-signalling NaN will produce ERROR 21 Exception in math operation

Earlier versions of HP BASIC will give errors when you try to print or otherwise format INF or NaN as ASCII, but will usually not generate errors when computations are done with them.

INTEGER Data Type

An INTEGER can have any whole-number value from:

-32 768 thru +32 767

COMPLEX Data Type

A complex number is an ordered pair (x,y) denoted by Mathematicians as: x + iy

where:
x is the real part of the complex number
y is the imaginary part of the complex number. The i in front of the y forms the imaginary number iy and is the same as multiplying y by the square-root-of--1. For example, the square-root-of--9 could be written as: square-root-of--1×square-root-of-9 or 3i.

HP BASIC complex numbers are stored as two REAL numbers. This means that a complex number requires 16 bytes of memory (each REAL component takes 8 bytes).

REAL, INTEGER, and COMPLEX variables may be declared as arrays.

Numeric Variables

One of the most important principles of HP BASIC programming is the use of variables to keep track of data. Numeric variables are used for numeric data and string variables are used for string data. The use of numeric variables is covered in this section, while the use of string variables is covered in chapter 5, "String Manipulation."

Naming Variables

The first thing that you must do before assigning a variable is to choose a name for it. A numeric variable name must conform to the following rules:

  1. The maximum length is 15 characters.
  2. The first character must be a capital letter.
  3. All other characters in the name must be either lower-case letters, numerals, or the underscore (_). These characters can be arranged in any order.

For example, Income_1991 is a valid variable name, but Income-1991 is not. A1 is valid, but 1A is not. Location_1111_a is valid, but Location_1111_A is not.

String variable names follow the same rules, but must have a trailing dollar sign ($) appended. Up to 15 characters, plus the dollar sign, are allowed.

Declaring Variables

It is good programming practice to declare the data type of all variables used in a program. The INTEGER, REAL, and COMPLEX statements have been provided to accomplish this task. However, HP BASIC is forgiving and implicitly assumes a variable is REAL if its type is not explicitly declared. Here are some examples of explicitly declaring variables:

 

INTEGER I, J, Days(5), Weeks(5:17)
REAL X, Y, Voltage(4), Hours(5,8:13)
COMPLEX S, T, Phasor_1(10), Phasor_2(10)
 

Each of the above statements declares two scalar and two array variables. A scalar is a variable which can, at any given time, represent a single value. An array is a subscripted variable that may contain multiple values accessed by subscripts. It is possible to specify both the lower and upper bounds of an array or to specify the upper bound only, and use the existing OPTION BASE as the lower bound. Details on declarations of arrays and how to use them are provided later in this chapter when arrays are dealt with in detail. The DIM statement may also be used to declare a REAL array.

 
DIM R(4,5)
 

An ALLOCATE statement can be used to declare REAL, INTEGER, and COMPLEX arrays. The ALLOCATE and DEALLOCATE statements allow you to dynamically allocate memory in programs which need tight control over memory usage.

 

ALLOCATE REAL Co_ords(2,1:Points), INTEGER Status(1:Points)
ALLOCATE COMPLEX Poles(2,1:Points), REAL Location(2,1:Points)

DEALLOCATE Co_ords(*), Status(*)
DEALLOCATE Poles(*), Location(*)
 

Assigning Variables

The following statements are equivalent:

LET A = A + 1
A = A + 1
 

To assign values to COMPLEX variables, the variables must first be declared as COMPLEX.

 
10  COMPLEX B,C,D
20  B=3.0        ! Real part = 3.0; imaginary part = 0.0.
30  C=CMPLX(3,4) ! Creates a COMPLEX value and assigns it to C.
40  D=CMPLX(Real_part,Imaginary_part)
50  B=D ! Assigns both the real and imaginary parts of D to B.
60  .
70  .
 

Implicit Type Conversions

The computer will automatically convert between REAL, INTEGER, and COMPLEX values in assignment statements and when parameters are passed by value in function and subprogram calls. The type conversion rules are:

When a value is assigned to a variable, the value is converted to the data type of that variable.

For example, the following program shows a REAL value being converted to an INTEGER:

 

100 REAL Real_var
110 INTEGER Integer_var
120 Real_var = 2.34
130 Integer_var = Real_var ! Type conversion occurs here.
140 DISP Real_var, Integer_var
150 END
 

Executing this program returns the following result:

 
2.34     2
 

INTEGER and REAL data types are converted to COMPLEX data types by adding an imaginary part of 0.

 

100 COMPLEX Complex_var1, Complex_var2
110 REAL Real_var
120 INTEGER Integer_var
130 Real_var=1.22
140 Integer_var=4
150 Complex_var1=Real_var
160 Complex_var2=Integer_var
170 DISP Complex_var1, Complex_var2
180 END
 

Executing this program produces the following results:

 
1.22  0      4  0
 

COMPLEX data types are converted to INTEGER and REAL data types by dropping the imaginary part.

 

100 COMPLEX Complex_var
110 REAL Real_var
120 INTEGER Integer_var
130 Complex_var=CMPLX(1.22,4.11)
140 Real_var=Complex_var
150 Integer_var=Complex_var
160 DISP Real_var, Integer_var
170 END
 

Executing this program produces the following results:

 
1.22     1
 

Conversions that occur within expression convert to the "highest" or most complicated data type before the operation occurs. For example:

 
CMPLX(3,-1) + 4.56
 

converts the REAL data type 4.56 to a COMPLEX value before the addition operation is performed.

When parameters are passed by value, the type conversion is from the data type of the calling statement's parameter to the data type of the subprogram's parameter. This type conversion occurs automatically. When parameters are passed by reference, the type conversion is not made and a type mismatch error will be reported if the calling parameter and the subprogram parameters are of different types.

Whenever numbers are converted from REAL to INTEGER representations, information can be lost. There are two potential problem areas in this conversion: rounding errors and range errors.

HP BASIC will automatically convert between types when an assignment is made. This presents no problem when an INTEGER is converted to a REAL. However, when a REAL is converted to an INTEGER, the REAL is rounded to the closest INTEGER value. When this is done, all information about the number to the right of the radix (decimal point) is lost. If the fractional information is truly not needed, there is no problem, but converting back to a REAL will not reconstruct the lost information -- it stays lost.

Another potential problem with REAL to INTEGER conversions is the difference in ranges. While REAL values range from approximately -10308 to +10308, the INTEGER range is only from -32 768 to +32 767 (approximately -104 thru +104). Obviously, not all REAL values can be rounded into an equivalent INTEGER value. This problem can generate INTEGER Overflow errors.

While the rounding problem is important, it does not generate an execution error. The range problem can generate an execution error, and you should protect yourself from crashing the program by either testing values before assignments are made, or by using ON ERROR to trap the error, and making corrections after the fact.

The following program segment shows a method to protect against INTEGER overflow errors (note that the variable X is REAL):

 

200  IF X > 32767 THEN X= 32767
210  IF X < -32768 THEN X = -32768
220  Intx = X
 

It is possible to achieve the same effect using MAX and MIN functions:

 
200 Y = MAX(MIN(X, 32767), -32768)
 

Both these methods avoid the overflow errors, but lose the fact that the values were originally out of range. If out-of-range is a meaningful condition, an error handling trap is more appropriate.

 

200 IF (-32768<=X) AND (X<=32767) THEN
210    Y = X
220 ELSE
230    GOSUB Out_of_range
240 END IF
 

Precision and Accuracy: The Machine Limits

Your computer stores all REAL variables with a sign, approximately 15 significant digits, and the exponent value. For most engineering and other applications, rounding errors are not a problem because the resolution of the computer is well beyond the limitations of most scientific measuring devices. However, when high-resolution numerical analysis requires accuracy approaching the limits of the computer, round-off errors must be considered.

Rounding errors should be considered when conversions are made between decimal digits and binary form (the form used by the computer internally to store the values). Input and output operations are occasions when this can occur. Given the format used for REALs, the conversion REAL —> decimal —> REAL will yield an identity only if the REAL —> decimal conversion produces a 17-decimal-digit mantissa and the calculations for the conversions are done in extra precision. This is not the case for HP BASIC, however the following things can be said about these conversions:

There are references at the end of this chapter to documents that contain further information on the subject of representing real numbers.

Note that when incrementing a value repeatedly by a fraction, the value may become inexact in the last decimal places. For example, the following program will print some values for FRACT(I) that are not divisible by .1. This is an unavoidable consequence of the efficient binary notation used in the computer.

 
REAL I
I=0
LOOP
  I=I+.1
  PRINT I,FRACT(I)
END LOOP
 

Internal Numeric Formats

The storage format for REAL and INTEGER numbers in memory are as follows:

REAL

INTEGER

Note that COMPLEX values are stored as 2 REAL numbers with the real part first and the imaginary part following.

Evaluating Scalar Expressions

This section covers the following topics as they relate to evaluating scalar expressions.

The Hierarchy

If you look at the expression 2+4/2+6, it can be interpreted several ways:

Computers do not deal well with ambiguity, so a hierarchy is used for evaluating expressions to eliminate any questions about the meaning of an expression. Six items can appear in a numeric expression:

The following table defines the hierarchy used by the computer in evaluating numeric expressions.
Math Hierarchy
Precedence Operator
Highest Parentheses; they may be used to force any order of operation Functions, both user-defined and intrinsic

Exponentiation: ^

Multiplication and division: * /

MOD DIV MODULO

Addition, subtraction, monadic plus and minus: + -

Relational Operators: = <> < > <= >= NOT AND

Lowest OR, EXOR

When an expression is being evaluated it is read from left to right and operations are performed as encountered, unless a higher precedence operation is found immediately to the right of the operation encountered, or unless the hierarchy is modified by parentheses.

The Delayed Assignment Surprise

BASIC delays assigning a value to a variable as long as possible. This means that when a variable is in an area of COM accessible to both the main program and a user-defined function is used in an expression that also calls the user-defined function--and is modified in the function--the value of the expression can be surprising, although not unpredictable. For example, if we define a function FNNeg1 that returns a minus 1, we would expect the following lines to print 2.

 

10 COM X
20 X = 3
30 Y = X + FNNeg1
40 PRINT Y
 

However, if the user-defined function looks like this:

 

1000  DEF FNNeg1
2000     COM X
1020     X = 500
1030     RETURN -1
1040  FNEND
 

The actual result will be 499. Therefore, don't use a user-defined function to modify values of variables. They are designed for returning a single value, and are best reserved for that.

Operators

There are three types of operators in BASIC:

NOTE
The only relational operators allowed with COMPLEX values are: = and <>. The only dyadic operators allowed with COMPLEX values are: ^, +, -, *, /, <>, and =. The only monadic operators allowed with COMPLEX values are: + and -.

While the use of most operators is obvious from the descriptions in the language reference, some of the operators have uses and side-effects that are not always apparent.

Expressions as Pass Parameters

All numeric expressions are passed by value to subprograms. Thus 5+X is obviously passed by value. Not quite so obviously, +X is also passed by value. The monadic operator makes it an expression.

For more information on pass parameters, read the chapter titled "Subprograms and User-Defined Functions."

Comparisons Between Two REAL or COMPLEX Values

If you are comparing INTEGER numbers, no special precautions are necessary since these values are represented exactly. However, if you are comparing REAL or COMPLEX values, especially those which are the results of calculations and functions, it is possible to run into problems due to rounding and other limits inherent in the system. For example, consider the use of relational operators in IF..THEN statements to check for equality in any situation resembling the following:

 
1220   DEG
1230   A=25.3765477
1240   IF SIN(A)^2+COS(A)^2=1 THEN
1250     PRINT "Equal"
1260   ELSE
1270     PRINT "Not Equal"
1280   END IF
 

You may find that the equality test fails due to rounding errors or other errors caused by the inherent limitations of computers. For additional information on rounding errors read the subsequent section titled "Rounding Functions."

Numerical Functions

Intrinsic functions are built into BASIC. This section discusses various intrinsic numerical functions in the following categories. For a list of all the numerical functions, see the "Keyword Summary" in the HP BASIC Language Reference and HP BASIC Condensed Reference.

Arithmetic Functions

It is not always convenient to write a program segment or subprogram to perform these numeric operations. To eliminate this inconvenience, BASIC provides you with the following functions:
ABS Returns the absolute value of an expression. Takes a REAL, INTEGER, or COMPLEX number as its argument.
FRACT Returns the "fractional" part of the argument.
INT Returns the greatest integer that is less than or equal to an expression. The result is of the same type (INTEGER or REAL) as the original number.
MAXREAL Returns the largest positive REAL number available in BASIC. Its value is approximately1.797 693 134 862 32E+308.
MINREAL Returns the smallest positive REAL number available in BASIC. Its value is approximately2.225 073 858 507 24E-308.
SQRT or SQR Return the square root of an expression. Takes a REAL, INTEGER, or COMPLEX number as their argument.
SGN Returns the sign of an expression: 1 if positive, 0 if 0, -1 if negative.

Array Functions

These functions are available when the MAT binary is loaded. They return specific information about numeric arrays.
BASE Returns the lower subscript bound of a dimension of an array.
DET Returns the determinant of a matrix.
DOT Returns the inner (dot) product of two numeric vectors.
RANK Returns the number of dimensions in an array.
SIZE Returns the number of elements in a dimension of an array.
SUM Returns the sum of all the elements in a numeric array.

For more information on numeric array functions, read the "Numeric Arrays" chapter.

Exponential Functions

These functions are used for determining the natural and common logarithm of an expression, as well as the Napierian e raised to the power of an expression. Note that all exponential functions take REAL, INTEGER, or COMPLEX numbers as their argument.
EXP Raise the Napierian e to a power. e ~ 2.718 281 828 459 05.
LGT Returns the base 10 logarithm of an expression.
LOG Returns the natural logarithm (Napierian base e) of an expression.

Trigonometric Functions

Six trigonometric functions and the constant PI; are provided for dealing with angles and angular measure. Note that all trigonometric functions take REAL, INTEGER, or COMPLEX numbers as their argument.
ACS Returns the arccosine of an expression.
ASN Returns the arcsine of an expression.
ATN Returns the arctangent of an expression.
COS Returns the cosine of an angle.
SIN Returns the sine of an angle.
TAN Returns the tangent of an angle.
PI Returns an approximate value for PI;.

Trigonometric Modes: Degrees and Radians

The default mode for all angular measure is radians. Degrees can be selected with the DEG statement. Radians may be re-selected by the RAD statement. It is a good idea to explicitly set a mode for any angular calculations, even if you are using the default (radian) mode. This is especially important in writing subprograms, as the subprogram inherits the angular mode from the context that calls it. The angle mode is part of the calling context. If it is changed in a subprogram, it is restored when the calling context is restored.

Hyperbolic Functions

Six hyperbolic functions are available with the BASIC system when the COMPLEX binary is loaded:
SINH returns the hyperbolic sine of a number.
COSH returns the hyperbolic cosine of a number.
TANH returns the hyperbolic tangent of a number.
ASNH returns the hyperbolic arcsine of a number.
ACSH returns the hyperbolic arccosine of a number.
ATNH returns the hyperbolic arctangent of a number.

Binary Functions

BASIC provides these functions to deal with binary numbers:
BINAND Returns the bit-by-bit logical and of two numbers.
BINCMP Returns the bit-by-bit complement of its numbers.
BINEOR Returns the bit-by-bit exclusive or of two numbers.
BINIOR Returns the bit-by-bit inclusive or of two numbers.
BIT Returns the value (0, 1) of a specified bit of a number.
ROTATE Returns the value obtained by shifting a number a specific number of bit positions, with wraparound.
SHIFT Returns the value obtained by shifting a number a specific number of bit positions, without wraparound.

When any of these functions are used, the arguments are first converted to INTEGER (if they are not already in the correct form) and then the specified operation is performed. It is best to restrict bit-oriented binary operations to declared INTEGERs.

Limit Functions

It is sometimes necessary to limit the range of values of a variable.
MAX Returns a value equal to the greatest value in the list of arguments.
MIN Returns a value equal to the least value in the list of arguments.

These functions work with INTEGER and REAL values.

Rounding Functions

Sometimes it is necessary to round a number in a calculation to eliminate unwanted resolution. There are two basic types of rounding, rounding to a total number of decimal digits and rounding to a number of decimal places.
DROUND Rounds a numeric expression to the specified number of digits. If the specified number of digits is greater than 15, no rounding takes place. If the number of digits specified is less than 1, zero is returned.
PROUND Returns the value of the argument rounded to a specified power of ten.

Rounding Errors Resulting from Comparisons

Equality errors occur when multiplying or dividing data values and comparing their result to another non-integer data value. This happens because the product of two non-integer values nearly always results in more digits beyond the decimal point than exists in either of the two numbers being multiplied. Any tests for equality must consider the exact variable value to its greatest resolution. If you cannot guarantee that all digits beyond the required resolution are zero, here are three methods that can be used to eliminate equality errors which could occur as a result of this:

The following example shows the DROUND technique:

 
1050   A=32.5087
1060   B=31.625
1070   C=A*B         ! Product is 1028.08763750
1080   D=32.5122
1090   E=31.621595509
1100   F=D*E         ! Product is 1028.08763751
1110   IF C=F THEN 1130
1120   PRINT "C is not equal to F"
1130   C=DROUND(C,7)
1140   F=DROUND(F,7)
1150   IF C=F THEN
1160     PRINT "C equals F after DROUND"
1170   ELSE
1180     PRINT "C not equal to F after DROUND"
1190   END IF
1200   END
 

Here is an example of the absolute value method of testing equality. In this case, a difference of less than 0.001 is assumed to be evidence of adequate equality. Using the previous example, we change methods starting at line 1130.

 

1130   IF ABS(C-F)<.001 THEN
1140     PRINT "C is equal to F within 0.001"
1150   ELSE
1160     PRINT "C is not equal to F within 0.001"
1170   END IF
1180   END
 

Finally, here is an example of the relative difference method. Once again, we change methods starting at line 1130.

 

1130   IF ABS((C-F)/C)< 10^(-3) THEN
1140     PRINT "Relative difference between C and F less than 10^-3"
1150   ELSE
1160     PRINT "Relative difference between C and F greater than 10^-3"
1170   END IF
1180   END
 

Random Number Function

BASIC provides a pseudorandom number generator:
RND Returns a pseudo-random number greater than 0 and less than 1.  

Since many modeling systems require random numbers with arbitrary ranges, it is necessary to scale the numbers.

 
200   R= INT(RND*Range)+Offset
 

Note that the above statement will return an integer between Offset and Offset + Range.

The random number generator is seeded with the value 37 480 660 at power-on, SCRATCH, SCRATCH A, and prerun. The pattern period is 231 - 2. You can change the seed with the RANDOMIZE statement, which will give a new pattern of numbers.

Complex Functions

These functions are available when the COMPLEX binary is loaded. Topics which are covered in this section are:

Creating COMPLEX Values

The CMPLX function lets you create a complex value from a pair of REALS.

 
10  COMPLEX B,C
20  C=CMPLX(3.5,.5)
30  B=C
40  PRINT C,B
50  END
 

Executing the above program produces these results:

 
3.5  .5
 

Evaluating COMPLEX Numbers

The BASIC expression evaluation uses two separate routines for dealing with REAL, INTEGER, and COMPLEX data types. There is a routine for dealing with REAL and INTEGER numbers and one for COMPLEX numbers. For example, taking the square root of a negative INTEGER or REAL number will produce an error. For instance, executing this statement:

SQRT(-1)
 

results in this error:

 
ERROR 30
 

The square root of a COMPLEX value whose real part is negative is defined so the operation is allowed. For example, executing this statement:

 
SQRT(CMPLX(-1,0))
 

returns the value:

 
0   1
 

where 0 is the real part and 1 is the imaginary part of the complex number.

COMPLEX Arguments and the Trigonometric Mode

When a trigonometric function call is made using a COMPLEX value as its parameter, BASIC will evaluate that call using the radian mode regardless of the current trigonometric mode setting (DEG or RAD). After the function call has been evaluated, the system returns to the current trigonometric mode. For example, enter and run this program:

10 DEG
20 PRINT SIN(30)
30 PRINT
40 PRINT SIN(CMPLX(30,0)) ! Always evaluated in the RAD mode.
50 PRINT
60 PRINT SIN(30)
70 END
 

The results from executing this program are as follows:

 
.5  degree mode

-.988031624093   0 radian mode

.5                degree mode
 
NOTE
Any complex function whose definition includes a sine or cosine function will be evaluated in the radian mode (RAD) regardless of the current angle mode (RAD or DEG).

Determining the Parts of COMPLEX Numbers

In many cases, such as network design, it is useful to be able to determine the real and imaginary parts of complex numbers, and the conjugate of a complex number.
REAL(C) returns the real part of a complex number.
IMAG(C) returns the imaginary part of a complex number.
CONJG(C) returns the complex conjugate of a complex number. That is:
 
CONJG(CMPLX(3,4))
 

is the same as

 
CMPLX(3,-4)
 

For example, executing the following statement:

 
DISP REAL(CMPLX(10,-3))

produces this result:

 
10

This next statement:

 
DISP IMAG(CMPLX(10,-3))

produces:

 
-3

This last example:

 
DISP CONJG(CMPLX(10,-3))

produces:

 
10  3 

Converting from Rectangular to Polar Coordinates

BASIC stores and uses complex numbers in a representation called rectangular coordinates. The rectangular coordinate representation of the complex plane is a Cartesian coordinate system where the X axis represents the real part of the complex number and the Y axis represents the imaginary part of the complex number. An alternate representation is polar coordinates. Polar coordinates consist of a magnitude and an argument (angle). The representation for polar coordinates is given as follows:

where M is the magnitude and Theta is the argument. The BASIC function used to obtain the magnitude is ABS, and the function used to obtain the argument is ARG.

The following program converts the rectangular coordinates 5 and 6 of the complex number 5 + i6 to polar coordinates.


140 RAD
150 DISP "The magnitude of 5 + i6 is: ";ABS(CMPLX(5,6))
160 DISP "The argument of 5 + i6 is: ";ARG(CMPLX(5,6))
170 END
 

Executing this program produces the following:

 

The magnitude of 5 + i6 is: 7.81024967591   in RAD mode
The argument of 5 + i6 is: .876058050598
 

Time and Date Functions

The following functions return the time and date in seconds:
TIMEDATE Returns the current clock value (in Julian seconds).

(If there is no battery-backed clock, the clock value set at power-on is2.086 629 12E+11, which represents midnight March 1, 1900. If the computer is connected to an SRM system, the SRM clock's value is read from the SRM System's clock when the SRM binary is loaded.)

The time value accumulates from its initial value unless it is changed by SET TIME or SET TIMEDATE. For example, executing this function

 
TIMEDATE
 

returns a value in seconds similar to the following:

 
2.11404868285E+11
 
TIME converts a formatted time-of-day string into a numeric value of seconds passed midnight. For example, executing this statement:
 
TIME("8:37:30")
 

returns the following numeric value in seconds:

 
31050
 
DATE converts a formatted date string into a numeric value in seconds. For example, executing this statement:
 
DATE("26 OCT 1986")
 

returns the following numeric value in seconds:

 
2.11397472E+11
 

For more information on this subject read the chapter in this manual titled "The Real-Time Clock." Also included in this chapter are the DATE$ and TIME$ string functions.

Base Conversion Functions

The two functions IVAL and DVAL convert a binary, octal, decimal, or hexadecimal string value into a decimal number.
IVAL returns the INTEGER value of a binary, octal, decimal, or hexadecimal 16-bit integer. The first argument is a string and the second argument is the radix or base to convert from. For example, executing this statement
 
IVAL("12740",8)
 

returns the following numeric value:

 
5600
 
DVAL returns the decimal whole number value of a binary, octal, decimal, or hexadecimal 32-bit integer. The first argument is a string and the second argument is the radix or base to convert from. For example, executing this statement
 
DVAL("11111111111111111111111111111100",2)
 

returns the following numeric value:

 
-4
 

For more information and examples of these functions, read the section "Number-Base Conversion" found in the "String Manipulation" chapter.

General Functions

When you are specifying select code and device selector numbers, it is more descriptive to use a function, such as KBD (returns the select code of the keyboard), to represent that device as opposed to a numeric value. For example, the following command allows you to enter a numeric value from the keyboard.

 
ENTER 2;Numeric_value
 

The above statement used in a program is not as easy to read as this one is:

 
ENTER KBD;Numeric_value
 

where you know the function KBD must stand for keyboard. Therefore, you know the statement is asking you to enter a numeric value from the keyboard.

Functions which return a select code or device selector are listed below:
CRT Returns the INTEGER 1. This is the select code of the internal CRT.
KBD Returns the INTEGER 2. This is the select code of the keyboard.
PRT Returns the INTEGER 701. This is the default (factory set) device selector for an external printer (connected through the built-in HP-IB interface at select code 7).
SC Returns the interface select code associated with an I/O path name.

Another function which fits in the general function category is the RES function. This function returns the last live keyboard numeric result (same as [RECALL] key).

Floating-Point Math

BASIC supports various floating-point coprocessors to provide faster computation.

For a complete description of floating-point math hardware support, refer to HP BASIC Advanced Programming Techniques.