It
has
nothing
to
do with the INT
function. Back
in Chapter 8
we
talked
a
little
about
the
Computer's
accuracy (you always have
to
watch the accuracy
of
the
last
decimal place
or two). TRS-80
users who have LEVEL II Basic will not notice this routine
"rounding
error".
If
we
solved all the world's problems with the bottom-of-the-line machine
you
might
not
want
to upgrade to the higher power models, and one
doesn't
stay
in
business
long
that way,
does
one?
There
is
a way to control the accuracy of your results in LEVEL I
BASIC.
It involves
artificially
rounding your fraction to the desired number
of
decimal
places,
and then forc-
ing the Computer to print out
only those
digits which are "properly rounded".
For example, suppose you
only
need n to three places. (Of course, you can enter
it
as
3.142,
but that's
not the
point.) Type
NEW
,
then
enter
and RUN
the following
program:
10
X=3
.14159
20 X=X+.0005
30
X=INT{X*1000>/1000
40
PRINT
X
Try
using
other
values
for
X (just make sure X*1000
isn't too large for the INT
function to
handle).
It's
easy
to
change the program
to
accomplish rounding at a different
point.
For
example,
to
round
X off
at
the hundredths -place
(2
digits
to
the right
of
the decimal
point), change
lines
20
and
30
to read:
20 X=X+.005
30
X=INT(X*100
J/100
and
RUN,
using
several
values
for
X.
HmmmmH!
Do you
suppose
there is
any way
to separate each of the digits in 3,14159, or
in
any
other
number? Do you suppose we
would have brought it up if there wasn't? After
all
. . .
(mum-
ble, mumble .
,
.).
It's really your
turn
to
do
some
creative
thinking,
but we'll get
you
started
and see if you
can
finish this
idea. First, wipe
out
the resident
program
and retype the program that splits
X
into an
integer and fractional
part
(the
first program
in
this
Chapter).
We clearly
can't
just
go
on
taking
the INT value of
X
over
and
over to
try and
split
down
decimal
value.
Let's try it with Z.
Adding
.0005
gives
our fraction a
"push
te
the
: right
direction".
If
this fraction has
a
digit
greater
than
4 in
its
10-th
ousasdths-plaee, then
adding
.9995
will effectively increase the
thousandths-
place digit
by
1. Otherwise,
the
added
.$$05
will
have
no effect
on
the Qnal result. This
results
m.\\---
what's
called
"4/5
rounding."
This
is
useful when you're printing
otlt
dollars-and-
cerits
—
1 t prevents $39.
99
5-type
prices.
71