Monday, 6 September 2021
205.5 : It's not just about integers
This number is halfway between the number of supporters I had when I started to write this, and the number I had when finishing it! It is rather an amusing number, as it forms the foundation of the (in)famous Commodore 64 BASIC program:
10 PRINT CHR$(205.5+RND(1));
20 GOTO 10
It does this by relying on a number of implicit facts about the implementation of Commodore BASIC. Firstly, it uses the fact that all floating point numbers (i.e. those with a fractional part) will be rounded down to the nearest integer. Down. Never up. It is a process known as 'truncation' and is in contrast with the various rounding techniques used elsewhere, such as 'half round up' (or common rounding), 'symmetric' rounding, or 'banker's rounding.' However, it doesn't perform this truncation just yet. The BASIC interpreter only performs truncation when it needs to. And it needs to do that in the third step.
The second part, RND(1), indicates that we want a random number between 0 and 1. Again, this is a fraction, making the result somewhere between 205.5 and 206.5. And because we use 0.5, the ratio of each symbol is 50:50. If we used 205.1, for example, this would be closer to 90:10. The RND(1) instruction on other versions of BASIC might instead use RND(0) to achieve this result. (But we'll cover that in the book!)
The third step passes the sum, of 205.5 and the random number between 0 and 1, to CHR$ which converts a numeric integer into a character. Every computer has a set of these characters where one number maps directly to one symbol. Most of the time these symbols are recognisable letters of the alphabet, or single digits. Sometimes they're graphical images. Since all character sets are referenced by integers, the BASIC interpreter running this program spots the floating point number and truncates it, to either 205 or 206, so that CHR$ can generate a sensible character.
By good fortune, the two characters we need for this maze (\ and /) happen to be next to each other in the character set, and so a maze is formed with characters 205 and 206. Other systems weren't so lucky, and need constructs like RND(1)*4 to make the magic work. (We'll cover that in the book, too!)
But back to the program! If you run this code on your Commodore 64 you'll notice that my maze is exactly the same as yours! Try it on an emulator. It's the same. It's as if I'd magically predicted what your random numbers would be! I could also show this by asking you to boot up a Commodore 64 emulator right now and typing:
I should not be able to predict this random number, but I think it will be 0.185564016! However, this is one magic trick where I'm happy to tell you the secret...
...it'll be in the book!
Finally, please share this update with your friends and social media contacts to remind them of the content they're missing, and get us closer to our target so we can all have the book with this (and many other) secrets from the retro computing era.