NUMERIC PROGRAMMING EXAMPLES
FLOATING-POINT TO ASCII CONVERSION EXAMPLES
Numeric programs must typically format their results at some point for presentation and inspection by
the program user. In many cases, numeric results are formatted as ASCII strings for printing or display.
This example shows
how
floating-.point values can be converted to decimal ASCII character strings.
The function shown in figure
4-6
can be invoked from PL/M-286, Pascal-286, FORTRAN-286, or
ASM2lS6 routines.
Shortness, speed, and accuracy were chosen rather than providing the maximum number of significant
digits possible. An attempt
is
made to keep integers
in
their
own
domain to avoid unnecessary conver-
sion errors.
Using the extended precision real number format,
this.
routine achieves a worst case accuracy of three
units in the 16th decimal position for a noninteger value or integers greater than
10
'8
• This
is
double
precision accuracy. With values having decimal exponents less than
100
in
magnitude, the accuracy
is
one unit in the 17th decimal position.
Higher precision can be achieved with greater care
in
programming, larger program size, and lower
performance.
iAPX2B6
MACRO
ASSEMBLER
80287
Floating-Point
to
18-Digit
ASCII
Conversion
10: 12:
38
09/25/83
PAGE
SERIES-III
iAPX286
MACRO
ASSEMBLER
XIC8
ASSEMBLY
OF
MODULE
FLOATING
TO
ASCII
ODJECT
MODULE
PLACED
IN
:
F3:
FPASC. DBJ - -
ASSEMBLER INVOKED BY:
ASM286.86:
F3:
FPASC. AP2
LOC
DB')
LINE
1 +1
2
,
4
•
•
7
8
•
10
11
12
13
14
I.
,.
17
18
I.
20
21
2.
23
24
2.
2.
27
28
2.
'0
"
,.
"
,.
35
,.
37
'8
,.
40
41
42
.,
44
4.
4.
47
48
4.
50
SOURCE
$title("B0287
Floating-Point
to
lS-Digit
ASCII
Conversion")
public
floatin9_to_8Scii
..
xtrn
get-pollllll'
_10:
neaT'.
tos_st.tus:
near
This
subroutine
will
t'onvert
the
floating
poiflt
numbeT'
in
tne
top
of
the
80287
"tack
to
<IiIn
ASCII
string
and
sepiilrate
power
oP
10
,,:aling
value
(in
binaf'':!).
The
maximum
width
of
the
ASCII
string
POf'med
is.
controlled
by a
paf'smeter
IIIhich
must
be
:>
1.
Unnof'mal
values.
denormal
values.
and
psuedo
zeroes
will
be
correctl..,
converted.
A
returnli!d
value
lIIill
indicate
hOIll
many
bin"r..,
bits
0'
precision
"'aT'e
lost
in
an
unnormal
OT'
denormal
value.
The
Iftagnituda
(in
terms
of
binary
pOlliaf')
of
iii
psuedo
zero
will
al.o
be
indicated.
Integers
les.
than
10
....
18
in
magnitude
are
accuratell,!
converted
if
the
dntinstion
ASCII
string
'ield
is
wide
enough
to
hold
all
the
digits.
Otherlllis.e
the
valull'
i5
converted
to
scientific
notation.
The
status
of
the
convlI'T'sion
is
identified
bl,!
the
retUT'n
value.
it
can
be:
o
conversion
complete.
string_size
is
defined
1
invalid
arguments
2
exact
integer
conversion.
!ltring_size
is
defined
3
indehnite
4 +
NAN
(Not
A
Number)
5 -
NAN
6 +
InfinitlJ
7 -
lnfinit..,
8
psuedo
zero
found,
string_size
is
defined
The
PLM/286
calling
convention
is:
Iloating_to_asc
ii:
procedure
(number.
denormalJltr,
stT'ingJltr,
sizeJltr.
field_5iH"
power
Jltr)
lUord
external;
dec:
lare
(d
anormalJl
tr.
stringJl
tr.
power
Jltr.
s i z
eJltr)
poi
nter;
declare
field_Size
word,
string_size
based
sizeJltr
wordi
declare
number
reali
declare
denoT'mal
integar
based
denormal_ptri
declare
power
integer
basad
power_ptT'i
and
'loatin9_to_asc
i
ii
The
floating
point
value
is
expected
to
bll'
on
the
top
of
tha
NP)C
.tack.
This
.ubroutini'
••
peets
3
free
ent1'ies
on
the
NP)c
stack
and
!d~ll
pop
the
passed
value
0'"
when
done.
The
geneT'ated
ASCII
stT'ing
lull
have
a
leading
character
eitha1'
'-'
or
'+'
indicilting
the
sign
of
the
value.
Ttle
ASCII
decimal
digits
liJill
immediately
Pollolil.
The
numeric
viillue
of
the
ASCII
string
is
(ASCII
STRINQ. )*10**POWER.
Figure 4-6. Floating-Point
to
ASCII Conversion Routine
4-7