Download Tekniques - Textfiles

Transcript
SEARCH
Similar parsimony can be obtained with the
new SEARCH functioq for string operations.
SEARCH accepts as an argument a series of
character pairs, and returns the position of
the first character in a string that falls within the range of one of the pairs. The following example shows this in the context of a
routine to validate 4909 file specifiers.
Such specifiers must begin with a letter, and
may contain only letters, digits, the dollar
sign and the underscore. On the traditional
4050, it was necessary to segment out each
character one at a time and subject it to a
Rounding Functions
Several readers have recently sent in functions for rounding. The variety of techniques
used in the functions suggests a need for clarification of the binary nature of computers
and the INT function.
In general, base 10 (decimal) integers can be
represented precisely with another number
base, but most decimal fractions can only be
represented approximately. For example,
1110 cannot be exactly represented in binary
notation regardless of how many significant
digits* are used, just as 113 can't be represented exactly in decimal notation.
Because of the "almost, but not quite" binary internal representation of decimal fractions, arithmetic operations may result in
close approximations but not exact. For instance, translating 0.1 and 10 into binary representation in the 4050 and multiplying them
results in .FFFFFFFFFFFF'6 which is close
to the integer 1 but not close enough for the
INT function; e.g., key INT(IO*O.l) into the
4050 to see what you get back.
Therefore, when testing a fraction for rounding, you must take into consideration its internal representation. A previous programming tip in TEKniques Vol. 2 No.6 told how
to set the internal value to agree with the
value displayed on the screen, since the
screen always rounds at the 12th place.
First, set EO (or some variable) to lE-12.
Then use INT(A + ABS(A)*EO) which results
in a "fuzzy" integer.
long series of logical comparisons. With
SEARCH, you check if the first character
is in the range AZ or az, and then check the
remainder of the string for any character in
the prohibited ranges. It requires fewer statements and eliminates the FOR/NEXT loop.
Also, SEARCH seems more readable than
a long list of ADEs (ASCII decimal
equivalents).
Upcoming issues of TEKniques will carry
more hints on using the new BASIC language
capabilities of the 40~2A and 4054A.
Tekniques
Vol. 6 No.4
REM
REM
REM
FOR
---------------------------------------------- CHECK SYNTAX OF ANV PASSijORO OR SPECIFIER
(CODE FOR CHECKING LEHGTH, ETC. OMITTED)
1=1 TO LEH(Z$)
7428 X$=SEG(Z$, I, 1)
7430
7440
7459
746B
7478
74BB
7498
75BB
7510
C=ASC(X$)
REM - FIRST CHARACTER MUST BE ALPHABETIC
EI-El+(l-l AHD (C(65 OR (090 AHD C(96) OR C>122»
IF El THEN 7588
REM -- NO~ CHECK FOR UNDERSCORES, ETC
El=EI+(C<36)
El=El+(C>J6 AND c(46)
El=El+(C>57 AHD C(65)
El=El+(C>90 At-ID C<9S)
7520 El=El+(C=9G)
75JB
7548
7550
7568
7578
7588
El=EI+(0122)
El=EI>B
IF EI THEN 7588
NEXT I
REM (ADDITIONAL ERROR CHECKING OMITTED)
REM - PRIHT AM ERROR MESSAGE
7390
7398
7408
7418
7428
REM ----------------------------------------------.
REM - CHECK SYNTAX OF AHV PASSMORO OR SPECIFIER
REM (CODE FOR CHECKING LEHGTH, ETC. OMITTED)
REM - FIRST CHARACTER MUST BE ALPHABETIC
X'=SEG(Z$,I,1)
4050A
7439 El=SEARCHO($,"AZaz",l)
7448 IF El=8 THEN 7498
7450 REM - HOW CHECII: FOR PROHIBITED CHARACTERS
7460 El=SEARCH(Z$, "i*% . . : Ut~ '{ .. II J 2)
7478 IF EPB THEN 7498
7480 REM (ADDITIOHAL ERROR CHECKING OMITTED)
7498 REM - PR INT At~ ERROR MESSAGE
Incorporating this routine into a function for
rounding results in the following:
or
(P is the number of digits to the right of the
decimal, not digit of precision.)
For further clarification, we'll take a look
at these two functions plus some others.
Four functions have been defined, each with
two different values for determining the round
off, namely, 0.49 and 0.50.
:1.30
140
150
160
170
180
190
200
ItEF
ItEF
ItEF
ItEF
ItEF
ItEF
ItEF
ItEF
FNAIA)=INTll0~P*IA-l.0E-8)tO.49)/l0~P
FNB(A)=INTll0~P*AtO.49)/l0~P
FNCeA)=INTll0~P*IAtl0~-P*0.49»/l0~P
FNIteA)=INTll0~P*(Atl0~-P*0.49)tABSIA)*EO)/l0~P
FNE(A)=INTel0~p*IA-l.0E-8)tO.5)/l0~P
FNF(A)=INTll0~P*AtO.5)/l0~P
FNGIA)=INT(10~p*eAtl0~-P*0.5»/l0~P
FNH(A)=!NTel0~p*eAtl0~-P*0.5)tABSIA)*EO)/l0-P
A small program allows us to put in the number to be rounded and how many places to
the right or left of the decimal.
lOO
:1.:1.0
:I.?O
130
140
150
160
170
180
190
200
210
;220
230
240
~!~50
*See TEKniques Vol. 5 No.4, page.14, for the discussion of internal representation in the 4050 of
numeric data.
4050
7399
7399
7499
7410
260
270
280
290
:~OO
INIT
Dl=41
EO=1.0E-·12
ItEF FNAIA)=INT(10-P*IA-1.0E-8)tO.49)/10~P
DEF FNBIA)=INT(10~P*AtO.49)/l0-P
DEF FNceAI=INTl10-P*IAt10~-P*0.49»/10-P
DEF FNDeA)=INT(10-P*IAtl0~-P*0.49)tABSeA)*EO)/10-P
DEF FNEeA)=INTelo-p*IA-l.0E-8)tO.5)/l0-P
DEF FNF(A)=INTll0-P*AtO.5)/l0~P
ItEF FNGIA)=INTll0-P*IAtlO--P*0.5»/l0-P
DEF FNHeA)=INT(10-p*eA+l0--P*0.5)tABS(A)*EO)/l0~P
pRINT 'INPUT XI ' j
INPUT X
IF X=O THEN 300
PRINT 'INPUT PI ';
INPUT F'
PRINT @Itll'NUMBER I'IX,'POSITION I'IP
PRINT @DlIFNAIX),FNBIX),FNC(X),FNDeX)
PRINT @DlIFNE(X),FNF(X),FNG(X),FNH(X)
GO TO 210
END
21