Numeric and String Operations [ HP Business BASIC/XL Migration Guide ] MPE/iX 5.0 Documentation
HP Business BASIC/XL Migration Guide
Numeric and String Operations
This section lists many of the differences in numeric operations, plus
some small hints for performance in HP Business BASIC/XL. Most string
operations are the same, but the compiler can influence coding style.
The types of changes desirable will be mentioned briefly.
Data Type Considerations in Numeric and String Operations
BASIC/260 data types translate automatically to HP Business BASIC/XL.
However, simple translation usually does not produce the best code
(either in compiled code size or performance). Be aware of the types
available so you can take advantage of HP Business BASIC/XL. Table 17-1
shows the corresponding BASIC/260 and HP Business BASIC/XL data types:
Table 17-1. Corresponding BASIC/260 and HP Business BASIC/XL Data Types
----------------------------------------------------------------------------------------------
| | | |
| # bits | BASIC/260 Data Type | HP Business BASIC/XL Data |
| | | Type |
| | | |
----------------------------------------------------------------------------------------------
| | | |
| 16 | INTEGER | SHORT INTEGER |
| | | |
----------------------------------------------------------------------------------------------
| | | |
| 32 | SHORT | SHORT DECIMAL |
| | | |
----------------------------------------------------------------------------------------------
| | | |
| 64 | REAL | DECIMAL |
| | | |
----------------------------------------------------------------------------------------------
| | | |
| 32 | no direct correspondence | INTEGER |
| | | |
----------------------------------------------------------------------------------------------
| | | |
| 32 | no direct correspondence | SHORT REAL |
| | | |
----------------------------------------------------------------------------------------------
| | | |
| 64 | no direct correspondence | REAL |
| | | |
----------------------------------------------------------------------------------------------
HP 260 hardware supports every BASIC/260 data type. HP 3000 hardware
supports only the integer and real data types; the decimal data types are
supported by software. Refer to the HP Business BASIC/XL Reference
Manual for information about the the range and accuracy of the data types
in Table 17-1.
Accuracy and Range of Data Types. The two decimal data types correspond
to the data types of BASIC/260. The DECIMAL data type is accurate to 12
digits, with an exponent range of -511 to +511. This accuracy can be
necessary for financial applications.
The HP Business BASIC/XL REAL and SHORT REAL types are binary
representations. The REAL type has 15.9 digits of accuracy. The extra
.9 digit can cause rounding errors; programs can have problems because of
rounding. For example, a program that adds 1.0 to a sum 50 times can
report an answer of 49.99999999999999. Be aware of this rounding problem
when you are comparing real numbers; that is where the problem usually
occurs. In many applications, this does not matter; decide what is
appropriate for each application.
Speed of Data Types. Accuracy should be only one consideration in
choosing data types. The user must also decide how much performance is
required. This is particularly important for CPU-bound programs (as
opposed to I/O or IMAGE bound). While the DECIMAL types are accurate,
they are implemented in software and therefore much slower than other
types.
WARNING Mixing floating-point data of different types affects speed.
Adding a DECIMAL and a REAL repeatedly can result in worse
performance than indicated. In addition, the DECIMAL type works
much faster with integral numbers than it does with fractional
numbers.
Code Size of Data Types. Compiled applications can produce large amounts
of code for the decimal data types relative to other HP Business BASIC/XL
data types. This is particularly noticeable in certain control
statements, such as SELECT and FOR-NEXT. Therefore, try to use HP
Business BASIC/XL SHORT INTEGER or INTEGER data types for these kinds of
statements. Because DECIMAL is a software type, expect larger code sizes
in compiled code in general.
As an example, suppose a program stores a date as a number for sorting.
Storing the year, month, and day requires six digits. Six digit numbers
must be REAL numbers on the HP 260:
REAL Datesort
INTEGER Day, Month, Year
Day=25
Month=2
Year=59
Datesort=Day+100*Month+10000*Year ! for example, 590225 is Feb 25, 1959
When translated, the program looks like this:
DECIMAL Datesort
SHORT INTEGER Day, Month, Year
Day=25
Month=2
Year=59
Datesort=Day+100*Month+10000*Year ! for example, 590225 is Feb 25, 1959
The HP Business BASIC/XL INTEGER type can support six digits without the
costs of a DECIMAL (or even a REAL) variable.
To make your migration easier, declare every variable, particularly
variables that can be INTEGER in BASIC/260. This is especially important
for loops and SELECT statements. Use the GLOBAL OPTION DECLARE statement
to point out undeclared variables in your HP Business BASIC/XL programs.
Document which BASIC/260 SHORT and REAL variables are actually
floating-point numbers and which variables are only declared as
floating-point numbers because the range of BASIC/260 INTEGER data type
is too small. This can help you change variables to HP Business BASIC/XL
INTEGER data type or to another appropriate data type.
NOTE No arithmetic is ever done in SHORT DECIMAL. To perform operations
on this type, all SHORT DECIMAL operands are changed to DECIMAL
first. This is why SHORT DECIMAL operations are slower.
Other Subsystems and Data Types. DECIMAL and SHORT DECIMAL data types
are not supported by every subsystem. Table 17-2 shows the subsystems
and types of support provided for these types. Only HP Business BASIC/XL
recognizes DECIMAL and SHORT DECIMAL data types.
Table 17-2. Types of Support Provided for Decimal and Short Decimal Data Types by Subsystems
---------------------------------------------------------------------------------------------
| | |
| Subsystem | Level of support |
| | |
---------------------------------------------------------------------------------------------
| | |
| IMAGE | Can read and write using K2 and K4 formats |
| | |
---------------------------------------------------------------------------------------------
| | |
| SORT | Will correctly sort both decimal types |
| | |
---------------------------------------------------------------------------------------------
| | |
| QUERY | Refer to your QUERY manual for details. |
| | |
---------------------------------------------------------------------------------------------
POS Function in Numeric and String Operations
The HP Business BASIC/XL POS function returns one when the search string
is null. The BASIC/260 POS function returns zero when the search string
is null. To avoid problems caused by this difference, check for the null
string first. The example below shows a BASIC/260 program segment that
uses the POS function:
Search_string$=""
...
IF POS(Search_in$, Search_string$) THEN
<do stuff>
ENDIF
The following example shows an HP Business BASIC/XL program segment that
uses AND to check for the null string before using the POS function. POS
does not execute when the search string is null. This program segment
obtains the same result as the BASIC/260 program segment above.
Search_string$=""
...
IF LEN(Search_string$) AND POS(Search_in$, Search_string$) THEN
<do stuff>
ENDIF
DATE$ in Numeric and String Operations
The DATE$ function returns the United States date format by default. To
display the date in a native language, send the native language number
for the format you want to the DATE$ function. The example below returns
the German date format (DD.MM.YY):
DATE$(8) ! German date format
Alternatively, you can change the default date format in the
configuration file. This allows DATE$ (without parameters) to return the
format you want. However, this is difficult to support because any user
can override this default with another configuration file.
RPT$ in Numeric and String Operations
HP Business BASIC/XL evaluates RPT$ parameters in a different order than
BASIC/260. If either parameter causes other effects with function calls,
the program might not act the same.
HP Business BASIC/XL evaluates the second (numeric) parameter first. The
first (string) parameter is evaluated only if the repetition factor is
greater than zero. If the function determines that the string should be
repeated zero times, the string is not evaluated. This order of
evaluation is followed because it increases the efficiency of compiled
programs.
To avoid this problem, move any calls that cause other effects out of the
RPT$ function. This provides a clearer program on both machines and
corrects the translation.
Substrings and Concatenation in Numeric and String Operations
Although substring access and concatenation are compatible in HP Business
BASIC/XL and BASIC/260, compiled code for these operations can be large.
You can reduce the amount of code produced by restricting substring
access and concatenation. A few simple techniques are mentioned here.
First, use substring assignment instead of concatenation where possible.
An example of this follows:
A$=A$+B$ ! Append a string
Temp$=VAL$(USRID)
Temp$=Temp$[1;1]
File$="WR"+Temp$+"K" ! build a filename
Replace the code above with the following:
A$[LEN(A$)+1]=B$ ! Append string
Temp$=VAL$(USRID)
Temp$=Temp$[1;1]
File$="WR K"
File$[3;1]=Temp$ ! Insert digit into the string
Second, do not use concatenation in PRINT or DISP statements. The ";"
separator in these statements (and IMAGE in PRINT USING) provides the
same output without concatenation. The separator produces less code.
Last, investigate the CONCAT compiler option for subunits that use many
concatenations. This option can reduce the code while maintaining
relatively good performance.
Enhancement Strings in Numeric and String Operations
HP 260 video enhancement strings are not compatible with most HP
terminals. These enhancement strings are not translated by the migration
aid. In particular, CHR$(128) ends enhancements on the HP 260; the
escape sequence '27"&d@" ends enhancements on the HP 3000.
Deleted Functions Used by Numeric and String Operations
Listed below are some functions that are not allowed in HP Business
BASIC/XL. Many of these functions either do not have equivalents on the
HP 3000, or are contained in another subsystem. Some specialized
functions are mentioned in other sections.
Table 17-3. Deleted Functions Used by Numeric and String Operations
---------------------------------------------------------------------------------------------
| | |
| Function | Comments |
| | |
---------------------------------------------------------------------------------------------
| | |
| RES | |
| | |
---------------------------------------------------------------------------------------------
| | |
| SIZE(-1) | other file numbers work |
| | |
---------------------------------------------------------------------------------------------
| | |
| DET | argument required |
| | |
---------------------------------------------------------------------------------------------
| | |
| SYSID$ | |
| | |
---------------------------------------------------------------------------------------------
| | |
| AVAIL | |
| | |
---------------------------------------------------------------------------------------------
| | |
| HOLE | |
| | |
---------------------------------------------------------------------------------------------
| | |
| CATLINE | |
| | |
---------------------------------------------------------------------------------------------
| | |
| CATFILE | |
| | |
---------------------------------------------------------------------------------------------
| | |
| All TASK statements | |
| | |
---------------------------------------------------------------------------------------------
| | |
| All PERFORM statements | |
| | |
---------------------------------------------------------------------------------------------
| | |
| All TIO statements | |
| | |
---------------------------------------------------------------------------------------------
| | |
| All MEDIA statements | |
| | |
---------------------------------------------------------------------------------------------
MPE/iX 5.0 Documentation