Converting From Real [ DATA TYPES CONVERSION Programmer's Guide ] MPE/iX 5.0 Documentation
DATA TYPES CONVERSION Programmer's Guide
Converting From Real
This section offers some suggestions for converting real numbers in
floating-point notation to character, integer, other real, and decimal
data types.
To Character
Within languages, there is usually a command or function like WRITE or
STRWRITE that will take a real value and print, display, or store it as a
string of ASCII characters.
The compiler utility procedures INEXT' and HPINEXT convert a real number
to a byte array for an output string of ASCII digits. The resulting
ASCII string can be represented in several formats. You can choose
options for representing the sign character, decimal points, and the
exponent.
To Integer
As mentioned in the section above, most languages contain an internal
assignment function to format a real value as an integer. Rounding and
truncation rules differ.
The compiler functions INT, INT', IFIX, and IFIX' all accept a 32-bit
real number and return it, truncated, as an 16-bit integer.
Similarly, DFIX or DFIX' accept a 64-bit real number, truncate it, and
return it the result as a 32-bit integer.
If you round a number equidistant from two adjacent integers, like 1.5 or
2.5, you may find that IEEE and HP3000 return different results. In
IEEE, a midpoint number rounds to the integer that has a least
significant bit of zero; in other words, the even integer. For example,
-1.5 rounds to -2, and 2.5 rounds to 2. HP3000 rounds to the integer of
greatest magnitude. For example, -1.5 rounds to -2 and 2.5 rounds to 3.
Rounding directives within a language behave in language-specific ways;
consult the language manual, or test a mid-point number if you are
doubtful.
To Other Real
As discussed in Chapter 2, there are two formats for real floating-point
numbers in MPE XL: IEEE and HP3000. Conversions between the two can be
done by choosing a particular compiler, or by calling the intrinsic
HPFPCONVERT. In addition, there are system procedures that will truncate
a fractional real number.
Real floating point numbers in this manual are assumed to be in IEEE
format, which is the default representation in NM. In CM data files or
programs, floating point real numbers default to HP3000 format, an MPE XL
emulation of the MPE V/E format.
If do not want the default IEEE real number format for a particular
application, you can force the HP3000 format by specifying the HP3000_16
compiler directive in HP FORTRAN 77/XL and HP Pascal. HP3000_16 selects
MPE V/E alignment and HP3000 real number format. As mentioned in Chapter
2, this also changes the alignment.
Although you can use different formats for separate external procedures,
you can only use one real number format within an executable module.
IEEE and HP3000 single-precision and double-precision real numbers have
different accuracies and ranges. You can convert between binary
floating-point formats with the intrinsic HPFPCONVERT.
You can specify any binary floating-point real number for input to
HPFPCONVERT, and ask for your output in any legal format. Acceptable
legal formats for source and destination are:
* HP3000:
32-bit
64-bit
* IEEE:
32-bit
64-bit
The conversion is performed by regarding the source number as infinitely
precise and with unbounded range, and then rounding it to fit the
designated destination format. You have some choice in the rounding
mode.
The method of rounding and the way exceptions are signalled depends
entirely on the destination format, not the source.
Conversion is performed as if all arithmetic traps are disabled. No
trapping to user-supplied or system-supplied arithmetic trap routines is
done.
You may encounter two types of errors:
* Underflow or Overflow
* Inexact
Overflow and Underflow. Conversion between formats can present a range
problem, when the target range is smaller. Thus, overflow and underflow
can occur in performing either of the following conversions:
* From a HP3000 single-precision real number to an IEEE
single-precision number.
* From an IEEE double-precision real number to an HP3000
double-precision.
You may have to develop new error handling code to prevent this.
Accuracy. The mantissa of an HP3000 double-precision real number
provides enough bits for 16 digits of accuracy. The mantissa of an IEEE
double-precision real number provides for 15.9 digit of accuracy.
Conversion from HP3000 format to IEEE double-precision may cause the
least significant digit of a 16-digit real number to be lost.
The loss of numeric precision is extremely small. However, if the
requirements of an application depend on the ASCII representation of
floating-point results, the effect could be important. For example, if a
program assumed 16-digit accuracy and requested 16 digits for formatting
output, with trailing zero suppression, the number 64.4 would appear
as 64.4 when the system was running in CM, but would appear as
64.40000000000001 when the system was running in NM.
Truncating. The compiler function AINT or AINT' accepts a 32-bit real
number and truncates it to return an integer-like number in 32-bit real
representation.
The compiler function DDINT or DDINT' truncates a 64-bit longreal number
to return an integer-like number in 64-bit longreal representation.
To Packed Decimal
In languages other than COBOL and RPG, follow these steps to convert from
an input real to a packed decimal:
1. Multiply or divide the real number by an appropriate power of 10.
2. Convert the resulting value to an base-ten integer.
3. Convert that integer to a decimal.
(See the previous sections about making these conversions.)
If your conversion is taking place within COBOL or RPG, you cannot
operate on a real number, as required in step 1 above. Instead, follow
these steps:
1. Convert the real number into a character.
2. Convert the resulting character to a decimal.
(See the previous sections about making these conversions.)
MPE/iX 5.0 Documentation