Skip to main content

Friday quiz problem 11 Mar 2022 - floating point in Forth

T

odays' Friday Quiz maths problem is as follows:

 At the Animal Olympics, Charlie Cheetah ran at 90 kilometres per hour, whilst Sid Snail slithered along at 20 hours per kilometre.  Charlie kept it up for 18 seconds.  How long would it take Sid to cover the same distance as Charlie?

This is not very difficult to work out as long as you make sure to take account of all of the units; hours and seconds, kph vs hpk. I won't spoil it in case you want to work it out for yourself.

despite being a simple calculation, it occurred to me that it would be fun to find the answer by running a simulation - ie show the race graphically, with a clock, and stop the clock when the snail reaches the cheetah.  And that it would be fun to run that simulation in real-time (slight spoiler, the answer is a number of hours).

My Minstrel (Jupiter Ace clone) is on my desk at the mo, and I really am getting a lot out of working in Forth. What makes this project particularly interesting is that it needs floating-point numbers. 

Forth and specifically Forth on the Ace has floating-point capability. But I've not used it to date. The default type is 16-bit signed integer, taking 1 cell (2 bytes) on the stack. A floating point number takes 4 bytes, 2 cells.

Using floating point is reasonably straightforward - instead of +, -, *, /  you use F+, F-, F*. F/  To print a float you use F.   There are a few things to remember.

  • The floating point arithmetic words expect two floats on the stack. If one or both is an integer then you'll get an unexpected result or stack underflow.  It's easy to convert an int to a float (UFLOAT) and float to int (INT).
  • To put a float on the stack, you have to remember to include a decimal point;  0.0 instead of 0  
  • Words like DROP and DUP won't work if you have a float on the stack. @ and ! won't work for a variable which is a float. The manual gives some suggested word definitions for 2DROP, 2DUP, 2@ and 2!  

Here's a video of the start of the program, not in real time. Here you can see the cheetah's fast but short sprint. 


Now running in real time, it takes around 38 minutes for the snail to move one character space (the whole width of the screen, 32 character spaces, represents 1km)


And after ~9hrs (the timing isn't perfect) it stops at the correct answer.



Here is my Forth program.  It's not particularly well-optimised but works fine and I have now commented it well.  If left to run for enough hours, finishes at exactly the right answer. To make the program run as fast as possible, and arrive at the answer much more quickly, take out the '50 pause' in the run loop.


Comments

Popular posts from this blog

RC2024/10 - my entry

A while ago I made this MIDI module for RC2014: It works but a better design would have its own serial chip and port decoding.  As it is, it provides the MIDI interface and a clock signal for the second SIO2 serial port. This means that it requires a little setting up and will only work for RC2014s with an SIO2 (and port B not already used). I think people might reasonably expect it to be plug-and-play and self-contained, ie do all the serial itself. My challenge to myself is to:  learn how to connect a serial chip (probably 68B50 ACIA) to receive the incoming MIDI and to serialise outgoing MIDI design the module, including the port decoding write a library so that it can easily be used on any RC2014. Potential applications include a MIDI sequencer and using incoming MIDI to trigger notes on the AY or SID sound chips. Entering the Retro Challenge 2024 (aka RC2024/10)  has given me an incentive to get on with this! I'm happy to see several more entries in the RC2014 catego...

IM53 8080 birthday cake

 Each year I've been trying to get more creative with ideas for Spencer's birthday cake. The plan this year was to incorporate LEDs in place of candles. I eventually settled on an Altair / IMSAI / PDP -style computer since those are the type of computers that inspired his RC2014. The IMSAI 8080 has the most colourful switches as well as a name that I could twist. The thought that it could show randomly flashing lights (as if the computer were running) and that it could also play a game of 'kill the bit' was very appealing. A plan formed to use a capacitive touch pad on the cake itself. The first job is to bake the fruitcake. I often use a 7" square tin and one of those cut in half and rearranged makes a cake of suitable proportions.  Even after taking a slice off the faces to make them nice and square, there are still some rounded corners, so after putting on the marzipan, I used more marzipan as a filler to flatten the whole thing. Even though I wanted to end up w...

How to convert images for TMS9918A graphics on the RC2014

For me, graphics capability is essential for an 8-bit computer. My graphics chip of choice for the RC2014 is the very capable TMS9918A. It has 15 colours, sprites, several modes and a max resolution of 256x192. It makes arcade-style games possible, such as Tut-Tut above.  I enjoy simply displaying images and have a bunch on my CF card (my 'hard drive') and have written image viewer and slideshow apps to display them. Some useful links: Convert9918 Tutorial of Convert9918's settings Multipaint J B Langston's TMS9918A video module my own TMSEMU video module my respository of TMS9918A software, games and .s2/.sc3 images Image conversion I did dabble in writing my own utility to convert .png images but then settled on the Multipaint app which can open a png in a MSX 'screen 2', allow you to tidy it up with paint tools and save as a .sc2 file. (An sc2 file is little more than a video-memory dump and so it's easy to blast that back into vram to display the image....