More on problems with integers.

See all articles from Update July 1995
Author(s): Timothy Swenson
Publication: Update Magazine
Pub Details: Update July 1995
Date: July 1995
Pages: 19

18 March 1995

Frank,

In regards the the programming puzzle put forth by Basil (no last name) in the last issue of UPDATE, I decided to take on the problem and see what I could make of it.

For reference here is the short program he had:

 100 LET page=1
110 FOR f=353 TO 375
150 LET page=page+.1
160 LET panel=page-INT(page)
165 LET panel=.1*INT(10*panel)
250 PRINT page, panel
1000 NEXT f

The problem he was getting was skips in the value of Panel. Panel should be equal to what’s to the right of the decimal point of Page. If Page equals 2.3 then Panel should equal .3. With the above program (running on the 2068), the problem was this:

2.2      0.2
2.3      0.2
2.4      0.3

Not having my 2068 functioning, I decided to try the program on the QL and see if the problem was specfic to the 2068. The results I got were different. Panel always equaled .1 less than it should (page = 2.2, panel = .1). But, I did not get the skip that the original program did. I did notice that when Page equaled 3.1, Panel equaled 9.9999E-2.

Without doing any more research, I think I can guess the general nature of the problem. If I remember correctly from my computer courses, computers have a problem with .1. It’s the way it’s converted to binary and back to decimal. If the program was to use integers and then divide by 10 to get the final result, there should no be a problem. Let page = 22, print out (page/10) and let panel = (page MOD 10)/10. Or the print routine can be played with to put in the decimal place manually.

Well, I hope this helps to solve the problem.

Happy Hacking,

Tim Swenson