clearing the keyboard buffer

Quote:

> DO: LOOP WHILE LEN(INKEY$)

This reminded me of a paradox which I've been meaning to raise with this

group in the hope of getting a logical explanation of apparently illogical

logic, if you see what I mean ;-)

Try running the following little test program. In GWBASIC, QBasic, QB4.5

and PDS 7.1 you find that numbers can be both true and false - or maybe

I'm bonkers #-)

If you enter *exactly* zero then "X is false!". If you enter a number

that *rounds off* to minus 1 then "X is true!". Any other number is

both "true" and "false".

What really intrigued me was the behaviour of numbers like:

-1.4999...

These round off to -1 and are therefore "true" *until* you get enough 9s,

in which case you reach the stage where "-1.5 is true!" then, with another

9 added, you get "-1.5 is true!" *and* "-1.5 is false!" - somewhat odd

don't you think?

No doubt there are perfectly good explanations for this based on binary

arithmetic, but the result did have me wondering what on earth was going

on for awhile :)

The program and a copy of a test run are appended below. (Note that TRUE.BAS

will APPEND to an output file named LOGICDAT.ANS to store test results.)

Cheers, Ian S.

10 '********************* TRUE.BAS ************************

20 'Program by Ian Staples 21 Aug 94 to demonstrate the IF

30 'and IF NOT logical operators in BASIC. Line numbers

40 'ensure the program will work in GW-Basic as well as QBasic,

50 'QuickBasic, etc. :-)

60 '

70 OPEN "A",1,"LOGICDAT.ANS"

80 A$="":INPUT "Enter a number";A$

90 IF A$<>"" THEN LET X=VAL(A$) ELSE CLOSE:SYSTEM

100 IF X THEN PRINT X;" is true!"

110 IF NOT X THEN PRINT X;" is false!"

120 PRINT #1,"Number tested = ";A$

130 IF X THEN PRINT #1,SPACE$(15);X;" is True!

140 IF NOT X THEN PRINT #1,SPACE$(15);X;" is False!

150 PRINT #1,

160 GOTO 80

170 END

The following output is from TRUE.BAS run under QBasic distributed with

MSDOS 6.0. The results with GWBASIC and PDS 7.1 are similar, at least

with respect to the behaviour of -1.4999...

*** LOGICDAT.ANS [run under MSDOS 6.0 QBasic, 25 Oct 94] ***

Number tested = 1

1 is True!

1 is False!

Number tested = 0

0 is False!

Number tested = 0.0000000000001

1E-13 is True!

1E-13 is False!

Number tested = -0.0000000000001

-1E-13 is True!

-1E-13 is False!

Number tested = -1

-1 is True!

Number tested = -1.5

-1.5 is True!

-1.5 is False!

Number tested = -1.49999999

-1.5 is True!

-1.5 is False!

Number tested = -1.4999999

-1.5 is True!

Number tested = -1.499999

-1.499999 is True!

Number tested = -0.5

-.5 is True!

-.5 is False!

Number tested = -0.4999999999

-.5 is True!

-.5 is False!

Number tested = -0.50000001

-.5 is True!

-.5 is False!

Number tested = -0.5000001

-.5000001 is True!

*************** End of message & appendix ************

--

c/- P.O. Box 1054 MAREEBA Phone : +61 (0)70 921 555 Home 924 847

Queensland Australia 4880 Fax : +61 (0)70 923 593 " " "