Chapter
51
Basic
Concepts
When converting
double
to
single precision,
BASIC rounds the
number
to
7
significant digits. For example:
A!
=
1.2345678901234567
BASIC stores as
1.234568
A!
=
1.3333333333333333
BASIC stores as
1.333333
When converting
single
to
double
precision,
BASIC
adds
trailing
zeroes
to
the right of the original value. If the original value has
an exact binary representation in single precision format, the re-
sulting value
is
accurate.
For
example:
A#
=
1.5
BASIC stores as
1.50000000000000
However, if a number does not have an exact binary representa-
tion, the conversion creates an erroneous value. For example:
A#
=
1.3
BASIC stores as
1.299999952316284
You should not use such conversions in your program because
most fractional numbers do not have exact binary representa-
tions. You can avoid this by forcing the constant
to
be double pre-
cision, such as:
A#
=
1.3#
or
A#
=
1.3D
which BASIC stores as
1.3.
If you must convert from single
to
double precision, the following
programs show a special technique.
Type and run the following program:
10
A!
=
1.3
20
A#
=
A!
30
PRINT
A#
BASIC prints
1.299999952316284
Now type and run this program:
10
A!
=
1.3
20
A#
=
VAL(STRS(A!))
30
PRINT
A#
BASIC prints
1.3.
Converting the single precision number
to
a
string before converting
it
to
a double precision value causes
BASIC
to
store the value accurately.
Note:
BASIC cannot automatically convert numeric
data
to
string data or vice versa. This results in a
“Type mismatch” error. Use the
VAL
and STR$ func-
tions
to
accomplish this kind
of
conversion.
50