Zero

From Ninerpedia
Revision as of 15:11, 19 February 2021 by Stefan Haubenthal (talk | contribs) (→‎Comparing variables against zero (0))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Comparing variables against zero (0)

There is a bug which causes a problem if you program in TI BASIC:

If you use the split keyboard as follows:

100 CALL KEY(1,A,B)
110 IF A<>0 THEN 100
120 PRINT "YOU PRESSED KEY X"

If you press key X, using the split keyboard as above (the same is true with keyboard 2 and key M), you should expect the value of A to be zero and the program to go to line 120.

It doesn't.

To check the value of A really does go to zero try inserting another line:

105 PRINT A

When you press X now you see the value zero appear on screen but the comparison still fails.

Now amend line 110 to be:

110 IF A+1<>1 THEN 100

Mathematically the two lines 110 are equivalent, but in TI BASIC they do not behave the same.

This is not a problem in Extended BASIC (any version).

The difficulty is caused by the multiple ways that the radix-100 floating point system can represent a zero value. In particular, internally the comparison to zero (and only zero) should be made with only the first two bytes of a number. By changing the comparison to be with something other than 0, all bytes are compared and we do not have a problem.

Call it a bug or a feature, but if you program in TI BASIC you need to be aware of this. It also has the potential to cause a problem if you use radix 100 floating point in any other language.